Essential Linux Shell Commands and SSH Basics
Introduction to Linux Shell
User Shell Prompts
-
Regular User:
$
-
Root User:
#
Logging into a Remote System
Using SSH with Password
[user@host ~]$ ssh remoteuser@remotehost
Using SSH with Private Key
- Ensure the private key has the correct permissions:
[user@host ~]$ chmod 600 privatekey.pem
- Connect using the private key:
[user@host ~]$ ssh -i privatekey.pem remoteuser@remotehost
Logging Out from a Remote System
- Use the
exit
command:
exit
- Alternatively, press
Ctrl + D
.
Executing Commands in the Bash Shell
Basic Commands
- Display current user:
whoami
- Display file contents:
cat
- Display the first few lines of a file:
head -n {number}
- Display the last few lines of a file:
tail -n {number}
Date Command Options
- Display date in RFC-2822 format:
date -R
- Display date as seconds since 1970-01-01 00:00:00 UTC:
date -s
- Display date in a specific format (customizable):
date -x
Running Multi-Line Commands
Use a backslash (\
) to continue a command on the next line:
echo "This is a long command" \
"that spans multiple lines."
Viewing Command History
- List previously executed commands:
[user@host ~]$ history
- Re-run a command by number:
!number
- Re-run the most recent command starting with a specific string:
!string
-
Insert the last word of the previous command:
-
Use
Esc + .
orAlt + .
Editing Commands on the Command Line
Useful Keyboard Shortcuts
Shortcut | Description |
---|---|
Ctrl + A | Move to the beginning of the line |
Ctrl + E | Move to the end of the line |
Ctrl + U | Clear from beginning to cursor |
Ctrl + K | Clear from cursor to the end |
Ctrl + Left Arrow | Move to the beginning of the previous word |
Ctrl + Right Arrow | Move to the end of the next word |
Ctrl + R | Search through command history |