In this tutorial, we are going to explain about SSH as well as 20 basic and 10 advanced commands. About whom you should know. By learning them, you will be able to understand how to navigate and manage your VPS or server using the command line.
What is SSH used for?
SSH stands for Secure Shell. It is a cryptographic network protocol that provides a secure way to access and communicate with remote servers or devices over an unsecured network, such as the internet.
SSH is widely used in the field of system administration and allows users to securely log in to a remote server and execute commands remotely. It provides a secure channel for data transmission by encrypting the connection between the client and server, thereby protecting sensitive information from unauthorized access or eavesdropping.
Some key features and benefits of SSH include:
- Encryption: SSH uses encryption algorithms to secure the communication between the client and server, ensuring that data sent over the network is encrypted and cannot be easily intercepted.
- Authentication: SSH supports various authentication methods, including password-based authentication, public key authentication, and more advanced methods like multi-factor authentication. This helps verify the identity of the user accessing the remote server.
- Secure Remote Access: SSH allows users to securely access and manage remote servers from anywhere, making it a valuable tool for system administrators and remote developers.
- Secure File Transfer: SSH includes protocols like SFTP (SSH File Transfer Protocol) and SCP (Secure Copy) that enable secure file transfer between local and remote systems.
- Port Forwarding: SSH supports port forwarding, which allows users to securely access services or applications running on remote servers through an encrypted tunnel.
- Tunneling: SSH can create secure tunnels for forwarding other protocols (such as HTTP, database connections, or VNC) through an SSH connection, providing an added layer of security.
Overall, SSH is a fundamental tool for secure remote access and administration, offering encryption, authentication, and secure data transfer capabilities.
List of Basic SSH Commands
In this part, we will go through popular SSH commands, complete with their syntaxes and useful options. Here’s a quick look of the basic SSH commands that we’ll cover in this article:
ls Command
The SSH command ls
is used to list files and directories in the current directory on the remote server. It functions similarly to the ls command used in a local terminal.
Here is the basic usage of the ls command in SSH:
ls [options] [directory]
-l
: This option enables the long format listing, providing detailed information about each file or directory, including permissions, ownership, size, modification date, and more. It displays the output in columns.
ls -l
-a
: The-a
option shows all files and directories, including hidden files that start with a dot (.
). By default, hidden files are not displayed when using thels
command.
ls -a
cd Command
In SSH, the cd
command is used to change the current directory on the remote server. It functions similarly to the cd
command used in a local terminal.
The basic usage of the cd command in SSH is as follows:
cd [directory] //For Example: ssh user@hostname# cd /path/to/directory
To go back one level in the directory hierarchy within an SSH session using the cd command, you can use the following command:
cd .. //For Example: ssh user@hostname# cd ../.. two dir back using this
mkdir Command
The mkdir
command is used to create a new directory.
mkdir [new_directory] //For Example: ssh user@hostname# mkdir myfolder
rm Command
The rm
command is used to remove or delete files and directories.
rm file_name
rm -r directory_name (use the -r flag to remove a directory and its contents)
cp Command
The cp
command is used to copy files and directories from one location to another. Here’s how you can use the cp
command with its parameters
cp source_file destination_file (copying a file)
cp -r source_directory destination_directory (use the -r flag to copy a directory and its contents)
cp source_file1 source_file2 destination_file (copying multiple files)
Parameters for the cp
command:
-r
or-R
: Enables recursive copying, allowing the copy of directories and their contents.-i
: Prompts for confirmation before overwriting an existing file.-u
: Copies only when the source file is newer than the destination file or when the destination file does not exist.-v
: Verbose mode. Displays detailed output of the files being copied.-p
: Preserves the file attributes, such as permissions and timestamps, during the copy.-a
: Archives the files while preserving permissions, timestamps, and other attributes. It is equivalent to using-dpR
.
mv Command
The mv
command is used to move or rename files and directories. Here’s how you can use the mv
command with its parameters
mv [options] source destination
Parameters for the mv
command:
-i
: Prompts for confirmation before overwriting an existing file.-u
: Moves only when the source file is newer than the destination file or when the destination file does not exist.-v
: Verbose mode. Displays detailed output of the files being moved.-n
: Does not overwrite an existing file.-b
: Creates a backup of the destination file if it already exists.
For Examples:
## 1- Move a file to a different location on the remote system:
mv source.txt /path/to/destination/
## 2- Rename a file on the remote system:
mv old_name.txt new_name.txt
## 3- Move a directory to a different location on the remote system:
mv source_directory /path/to/destination/
## 4- Move a directory and its contents to a different location on the remote system:
mv -r source_directory /path/to/destination/
pwd Command
The pwd
command is used to display the current working directory. Here’s how you can use the pwd
command with its parameters
pwd
cat Command
The cat
command is used to concatenate and display the contents of files. Here’s how you can use the cat
command with its parameters
cat [options] file
Parameters for the cat
command:
-n
: Displays line numbers along with the file contents.-b
: Displays line numbers for non-empty lines only.-s
: Squeezes multiple adjacent empty lines into a single empty line.-E
: Displays a dollar sign ($) at the end of each line.-T
: Displays tab characters (^I) instead of actual tab characters.-A
: Combines the effects of-v
,-E
, and-T
options.
For Examples:
## 1- Display the contents of a file on the remote system:
cat filename.txt
## 2- Display the contents of a file with line numbers on the remote system:
cat -n filename.txt
## 3- Display the contents of a file with line numbers for non-empty lines only:
cat -b filename.txt
nano Command
The nano
command is used to open a file in the Nano text editor. Here’s how you can use the nano
command with its parameters
nano [options] filename
Parameters for the nano
command:
-B
or--backup
: Creates a backup file before saving changes to the original file.-H
or--historylog
: Enables the write history log feature.-E
or--tabstospaces
: Converts typed tabs into spaces.-I
or--ignorercfiles
: Ignores the nanorc configuration files.-K
or--rebindkeypad
: Rebinds the keypad keys to their default functions.-O
or--morespace
: Uses the blank line to indicate more scrolling.-R
or--restricted
: Runs nano in restricted mode, disabling some potentially dangerous commands.-S
or--smooth
: Enables smooth scrolling.-T
or--tabsize=
: Sets the tab size to the specified number of spaces.-V
or--version
: Displays the version of nano.-W
or--wordbounds
: Enables word boundaries when searching.-X
or--noconvert
: Disables automatic conversion of files from DOS/Mac format to Unix format.-Z
or--zap
: Clears the undo buffer when a file is read.
For Examples:
## 1- Edit a file on the remote system using nano:
nano filename.txt
## 2- Edit a file with a specific tab size (e.g., 4 spaces):
nano -T 4 filename.txt
wget Command
The wget
command is used to download a file from the internet. Here’s how you can use the wget
command with its parameters
wget [options] URL
Parameters for the wget
command:
-q
or--quiet
: Runs wget in quiet mode, suppressing output.-O
or--output-document=
: Specifies the filename to save the downloaded file as.-P
or--directory-prefix=
: Specifies the directory where the downloaded file will be saved.-r
or--recursive
: Downloads recursively, following links within the web page.-N
or--timestamping
: Downloads only if the remote file is newer than the local file.-c
or--continue
: Resumes a previous download if it was interrupted.-np
or--no-parent
: Does not ascend to the parent directory when downloading recursively.-A
or--accept=
: Specifies a comma-separated list of file name suffixes or patterns to accept during recursive downloading.-R
or--reject=
: Specifies a comma-separated list of file name suffixes or patterns to reject during recursive downloading.--user=
: Specifies the username for authentication if the download requires authentication.--password=
: Specifies the password for authentication if the download requires authentication.--no-check-certificate
: Disables SSL certificate validation.
chmod Command
The chmod
command is used to change the permissions of a file or directory. Here’s how you can use the chmod
command with its parameters
Parameters for the chmod
command:
mode
: The permissions to be set, represented as a three-digit octal number or symbolic notation.file
: The file or directory on which the permissions should be modified.
chmod [options] mode file
Options for the chmod
command:
mode
: The permissions to be set, represented as a three-digit octal number or symbolic notation.file
: The file or directory on which the permissions should be modified.- Options for the
chmod
command: - -c or
--changes
: Prints a line for each file that is changed. -f
or--silent
or--quiet
: Suppresses most error messages.-v
or--verbose
: Prints a line for each file that is processed.-R
or--recursive
: Modifies the permissions recursively for files and directories within a directory.
chown Command
The chown
command is used to change the ownership of files and directories. Here’s how you can use the chown
command with its parameters
chown [options] user:group file
user
: The username or user ID to set as the owner.group
: The group name or group ID to set as the group owner.file
: The file or directory on which the ownership should be modified.
Options for the chown
command:
-c
or--changes
: Prints a line for each file that is changed.-f
or--silent
or--quiet
: Suppresses most error messages.-v
or--verbose
: Prints a line for each file that is processed.-R
or--recursive
: Modifies the ownership recursively for files and directories within a directory.
For Examples:
## 1- Change the ownership of a file to a specific user and group:
chown john:users filename.txt
## 2- Change the ownership of a directory and its contents recursively to a specific user and group:
chown -R john:users directory
## 3- Change the ownership of a file using numeric user and group IDs:
chown 1001:1001 filename.txt
ssh-keygen Command
The ssh-keygen
command is used to generate SSH key pairs for authentication in Linux-based systems. Here’s how you can use the ssh-keygen
command with its parameters:
ssh-keygen [options]
Parameters for the ssh-keygen
command:
-t
: Specifies the type of key to generate. Common types includersa
,dsa
,ecdsa
, anded25519
.-b
: Specifies the number of bits in the key. For example,-b 2048
generates a 2048-bit key.-C
: Adds a comment to the key.-f
: Specifies the filename of the key file.-N
: Provides a passphrase to encrypt the private key.-q
: Runs ssh-keygen in quiet mode, suppressing most output.-y
: Outputs the public key of an existing private key.-R
: Removes a host key or multiple host keys associated with a given hostname.-E
: Specifies the hash algorithm for the fingerprints of the key.
For Examples:
## 1- Generate an RSA key pair with default settings:
ssh-keygen
## 2- Generate an ECDSA key pair with a specific number of bits:
ssh-keygen -t ecdsa -b 256
## 3- Generate an Ed25519 key pair with a passphrase and a custom filename:
ssh-keygen -t ed25519 -N "myPassphrase" -f mykey
## 4- Display the public key of an existing private key:
ssh-keygen -y -f private_key
ssh-copy-id Command
The ssh-copy-id
command is used to install your public key on a remote serve. Here’s how you can use the ssh-copy-id
command with its parameters:
[user@]hostname
: The username and hostname (or IP address) of the remote server.
ssh-copy-id [options] [user@]hostname
Options for the ssh-copy-id
command:
[user@]hostname
: The username and hostname (or IP address) of the remote server.- Options for the
ssh-copy-id
command: -i
: Specifies the identity file (public key) to be installed on the remote server. By default,ssh-copy-id
uses the~/.ssh/id_rsa.pub
file.-p
: Specifies the port number to use for the SSH connection. By default, it uses port 22.-f
: Forces the copy and overwrites any existing authorized keys on the remote server.-o
: Provides additional SSH options to be passed to the underlying ssh command.
For Examples:
## 1- Copy the default public key to the remote server:
ssh-copy-id user@hostname
## 2- Copy a specific public key file to the remote server:
ssh-copy-id -i ~/.ssh/my_key.pub user@hostname
## 3- Copy the public key to a specific port on the remote server:
ssh-copy-id -p 2222 user@hostname
## 4- Copy the public key and force overwrite any existing authorized keys on the remote server:
ssh-copy-id -f user@hostname
scp Command
The scp
command is used to securely transfer files between a local system and a remote system or between two remote systems using the SSH protocol. Here’s how you can use the scp
command with its parameters:
source
: The file or directory to be copied. It can be a local file or a remote file specified with the[user@]host:file
syntax.destination
: The target location where the file or directory should be copied. It can be a local directory or a remote directory specified with the[user@]host:directory
syntax.
scp [options] source destination
Options for the scp
command:
-r
: Recursively copies directories and their contents.-p
: Preserves the modification times, access times, and modes of the source files.-v
: Displays verbose output, showing the progress and additional information during the file transfer.-P
: Specifies the port number to use for the SSH connection. By default, it uses port 22.-i
: Specifies the identity file (private key) to use for authentication.-C
: Enables compression during the file transfer.-q
: Runs scp in quiet mode, suppressing output except for error messages.-F
: Specifies an alternate SSH configuration file to use.
For Examples:
## 1- Copy a local file to a remote server:
scp localfile.txt user@hostname:/path/to/destination/
## 2- Copy a remote file to the local system:
scp user@hostname:/path/to/remote/file.txt /path/to/destination/
## 3- Copy a directory and its contents to a remote server:
scp -r sourcedir/ user@hostname:/path/to/destination/
## 4- Copy a file using a specific SSH private key for authentication:
scp -i ~/.ssh/my_key.pem file.txt user@hostname:/path/to/destination/
## 5- Copy a remote file to a local directory:
scp user@hostname:/path/to/remote/file.txt /path/to/local/directory/
sftp Command
To perform file transfers using the SFTP (SSH File Transfer Protocol) within an SSH session, you can use the sftp
command. It provides an interactive FTP-like interface to navigate, transfer, and manage files on remote systems securely. Here’s how you can use the sftp
command with its parameters:
sftp [user@]host[:port]
Parameters for the sftp
command:
[user@]host
: The username and hostname (or IP address) of the remote server.port
: (Optional) The port number to use for the SSH connection. By default, it uses port 22.
After executing the sftp
command, you’ll enter the SFTP interactive mode, where you can execute various commands for file transfer and management. Some commonly used commands within sftp
are:
get [local_file]
: Downloads a file from the remote system to the local system.put [remote_file]
: Uploads a file from the local system to the remote system.cd
: Changes the current remote directory.lcd
: Changes the current local directory.ls [directory]
: Lists files and directories in the current remote or local directory.lls [directory]
: Lists files and directories in the current local or remote directory.pwd
: Prints the current remote directory.lpwd
: Prints the current local directory.mkdir
: Creates a directory on the remote system.rmdir
: Removes a directory from the remote system.rm
: Removes a file from the remote system.rename
: Renames a file or directory on the remote system.quit
orexit
: Exits the sftp session.
top Command
The top
command is used to monitor system processes and resource usage in Linux-based systems. When used with SSH, it allows you to remotely monitor the system’s processes and resource utilization. However, it’s important to note that the top
command is an interactive command-line tool and may not work well within an SSH session.
That being said, if you want to run the top
command through SSH, you can use the following syntax:
top [options]
Options for the top
command:
-d
: Specifies the delay between updates in seconds.-n
: Specifies the number of iterations to run before exiting.-p
: Monitors specific process IDs.-u
: Monitors processes owned by a specific user.-H
: Enables thread-level monitoring.-s
: Specifies the sort column for the process display.-i
: Shows idle tasks.-b
: Runs top in batch mode, suitable for parsing the output programmatically.-c
: Shows the command line instead of the process name.-w
: Enables wide output, which avoids truncation of long command lines.-o
: Specifies the field(s) to display.
For Examples:
top -d 5 -n 10
df Command
The df
command is used to show disk space usage of file systems usage on Linux-based systems. Here’s how you can use the df
command with its parameters:
df [options]
Options for the df
command:
-a
or--all
: Includes all filesystems, including those with a size of 0 blocks.-B
or--block-size=
: Sets the block size for displaying sizes.-h
or--human-readable
: Prints sizes in human-readable format (e.g., 1K, 1M, 1G).-H
or--si
: Prints sizes in human-readable format using SI units (e.g., 1K, 1M, 1G).-i
or--inodes
: Displays inode information instead of block usage.-l
or--local
: Limits the output to local filesystems.-P
or--portability
: Uses the POSIX output format.-T
or--print-type
: Adds a header line indicating the filesystem type.-x
or--exclude-type=
: Excludes filesystems of the specified type.
For Examples:
ssh user@hostname df -h
free Command
The free
command is used to display information about the system’s memory usage on Linux-based systems. Here’s how you can use the free
command with its parameters:
ssh user@hostname free [options]
Options for the free
command:
-b
or--bytes
: Displays output in bytes.-k
or--kilo
: Displays output in kilobytes (default).-m
or--mega
: Displays output in megabytes.-g
or--giga
: Displays output in gigabytes.-h
or--human
: Displays output in human-readable format.-s
or--seconds=
: Continuously refreshes the output every .-t
or--total
: Shows a summary line with the total memory.-w
or--wide
: Wide output mode, shows additional details.
For Examples:
ssh user@hostname free -h
ping Command
The ping
command is used to test network connectivity between your local system and a remote system. Here’s how you can use the ping
command with its parameters:
ping [options] target
Options for the ping
command:
-c
: Specifies the number of packets to send.-i
: Specifies the interval between sending packets in seconds.-w
: Specifies the timeout (deadline) in seconds before stopping the ping.-s
: Specifies the size of the ICMP packets to send.-q
: Quiet mode, only displays summary statistics.-v
: Verbose mode, displays detailed output.-t
: Specifies the Time-To-Live value for the ping packets.-I
: Specifies the network interface to use for sending ping packets.
For Examples:
ssh user@hostname ping -c 5 example.com
List of Advanced SSH Commands
Here are some advanced SSH commands with their parameters that you can use for various purposes:
sshfs Command
The sshfs
command is used to mount a remote file system locally using SSH, allowing you to access and interact with remote files and directories as if they were on your local system. Here’s how you can use the sshfs
command with its parameters:
sshfs [user@]host:[dir] mountpoint [options]
Parameters for the sshfs
command:
[user@]host:[dir]
: The username, hostname, and directory path on the remote system that you want to mount.mountpoint
: The local directory where you want to mount the remote file system.
Options for the sshfs
command:
-o
: Specifies additional options for the SSHFS mount. Multiple options can be separated by commas.-p
: Specifies the SSH port to use for the connection.-C
: Enables compression during data transfer.-F
: Specifies an alternate SSH configuration file.-o IdentityFile=
: Specifies the private key file to use for authentication.-o password_stdin
: Allows entering the password interactively.
For Examples:
sshfs user@hostname:/remote/dir /local/mount/point -o IdentityFile=/path/to/private_key
ssh-agent Command
The ssh-agent
command is used to manage SSH authentication agents. It is typically used to store private keys used for SSH authentication and provide them to the SSH client when needed. Here’s how you can use the ssh-agent
command with its parameters:
ssh-agent
ssh-add /path/to/private_key
For Examples:
## 1- Start the SSH agent:
eval $(ssh-agent)
## 2- Add a private key to the agent:
ssh-add <private_key>
## 3- List the keys loaded in the agent:
ssh-add -l
## 4- Remove a key from the agent:
ssh-add -d <private_key>
## 5- Kill the SSH agent:
ssh-agent -k
screen Command
The screen
command is a powerful terminal multiplexer that allows you to create and manage multiple virtual terminals within a single SSH session. Here’s how you can use the screen
command with its parameters:
screen
Options for the screen
command:
## 1- Detach from a screen session:
Ctrl+a d
## 2- Reattach to a detached screen session:
screen -r
## 3- List active screen sessions:
screen -ls
## 4- Attach to a specific screen session:
screen -r <session_id>
## 5- Create a new named screen session:
screen -S <session_name>
## 6- Attach to a named screen session:
screen -r <session_name>
## 7- Close a screen session:
exit
rsync Command
The rsync
command is a powerful utility for efficiently transferring and synchronizing files between local and remote systems. When used with SSH, rsync
securely transfers data over the network. Here’s how you can use the rsync
command with its parameters:
rsync [options] source_file(s)/directory(s) username@remote_host:destination_directory
Options for the rsync
command:
-a
,--archive
: This option preserves various attributes of the files being transferred, including permissions, ownership, timestamps, and recursive copying.-v
,--verbose
: The-v
option enables verbose output, displaying detailed information about the progress of the rsync transfer.-z
,--compress
: This option enables compression during the transfer, reducing the size of data being transmitted. It can improve transfer speed over slow network connections.-P
: The-P
option combines the--partial
and--progress
options. It allows you to resume interrupted transfers and shows the progress of each file being transferred.-r
,--recursive
: The-r
option enables recursive copying, allowing rsync to traverse directories and copy their contents and subdirectories.--delete
: This option ensures that files in the destination directory are deleted if they no longer exist in the source directory. It helps to keep the destination directory in sync with the source directory.
For Examples:
##1 -a, --archive:
rsync -a source_directory username@remote_host:destination_directory
##2 -v, --verbose:
rsync -av source_directory username@remote_host:destination_directory
##3 -z, --compress:
rsync -az source_directory username@remote_host:destination_directory
##4 -P:
rsync -aP source_directory username@remote_host:destination_directory
##5 -r, --recursive:
rsync -avzr source_directory username@remote_host:destination_directory
##6 --delete:
rsync -avz --delete source_directory username@remote_host:destination_directory
ssh tunneling Command
SSH tunneling, also known as SSH port forwarding, allows you to create secure connections between local and remote hosts by forwarding network traffic through an encrypted SSH connection. Here are some commonly used SSH tunneling commands with their parameters:
########For Examples with parameters###########
## 1- Local Port Forwarding:
ssh -L <local_port>:<destination_host>:<destination_port> <user>@<jump_host>
## 2- Remote Port Forwarding:
ssh -R <remote_port>:<destination_host>:<destination_port> <user>@<jump_host>
## 3-Dynamic Port Forwarding (SOCKS proxy):
ssh -D <local_socks_port> <user>@<jump_host>
## 4- Tunneling through a jump host:
ssh -L <local_port>:<final_destination>:<destination_port> -J <user>@<jump_host> <user>@<intermediate_host>
ssh-keyscan Command
The ssh-keyscan
command is used to retrieve the public keys of SSH servers and store them in the known_hosts file. Here’s how you can use the ssh-keyscan
command with its parameters:
########For Examples with parameters###########
## 1- Scan a single SSH server and display the public key:
ssh-keyscan <host>
## 2- Scan multiple SSH servers and display their public keys:
ssh-keyscan <host1> <host2> ...
## 3- Save the public keys to the known_hosts file:
ssh-keyscan <host> >> ~/.ssh/known_hosts
## 4- Scan a server using a specific port number:
ssh-keyscan -p <port> <host>
## 5- Specify the SSH protocol version to use:
ssh-keyscan -t <protocol> <host>
## 6- Scan hosts listed in a file:
ssh-keyscan -f <filename>
sshfs Command
The sshfs
command is used to mount a remote file system over SSH, allowing you to access files and directories on a remote server as if they were on your local machine. The basic syntax for the sshfs
command with parameters is as follows:
sshfs [user@]host:[directory] [mountpoint] [options]
Parameters for the sshfs
command:
[user@]host
: This specifies the username and hostname or IP address of the remote server you want to connect to.[directory]
: This is the directory on the remote server that you want to mount.[mountpoint]
: This is the local directory where you want to mount the remote file system.[options]
: These are optional parameters that you can use to customize the behavior ofsshfs
. Some common options include-p
for specifying a non-default SSH port,-o
for specifying additional SSH options, and-o
reconnect to automatically reconnect if the connection is interrupted.
For Examples
sshfs john@194.167.0.100:/home/user/files /mnt/remote
ssh-key forwarding Command
To enable SSH
key forwarding, you can use the -A
option with the ssh
command. This option allows the authentication agent on your local machine to forward your SSH keys to the remote server, enabling you to use your local keys for authentication on the remote server.
Here’s the basic syntax for the ssh
command with SSH key forwarding:
ssh -A [user@]hostname [command]
The -A
option tells ssh
to enable SSH agent forwarding.
ssh config Command
The ssh
command itself does not have a specific “config” command, but it reads its configuration from the ~/.ssh/config
file.
ssh -F /path/to/config/file [user@]hostname [command]
Conclusion
By understanding and utilizing these SSH commands for managing a Linux server or VPS, you can effectively manage remote servers, securely transfer files, configure SSH settings, and enhance your overall remote administration experience.
Thankfully, you’ve learned 20 basic and 9 advanced with its parameters essential SSH commands every webmaster should know. Now you can easily perform basic tasks on your remote machine, such as creating files, deleting them, jumping between directories, etc.