Secure Shell (SSH) is an essential tool for system administrators, developers, and anyone managing remote servers. With SSH remote access, you can securely connect to a remote machine, execute commands, transfer files, and perform administrative tasks. In this article, we’ll explore 10 common SSH commands that you should be using today.
The most basic SSH command is used to connect to a remote server:
ssh user@remote_host
Replace user
with your username and remote_host
with the server’s IP address or domain name.
By default, SSH uses port 22, but you can specify a different port if necessary:
ssh -p 2222 user@remote_host
This is useful if the SSH server is configured to run on a non-standard port.
Instead of using a password, you can authenticate with an SSH key:
ssh -i /path/to/private_key user@remote_host
Generating an SSH key can be done with:
ssh-keygen -t rsa -b 4096
This improves security and eliminates the need to enter a password every time.
You can execute a single command on a remote server without opening an interactive session:
ssh user@remote_host "ls -l /var/www/html"
This is useful for quick administrative tasks.
Secure Copy Protocol (SCP) allows you to transfer files between local and remote machines:
scp file.txt user@remote_host:/remote/directory/
To copy a directory recursively:
scp -r /local/directory user@remote_host:/remote/directory/
An alternative to SCP is rsync
, which is more efficient for syncing files:
rsync -avz /local/directory user@remote_host:/remote/directory
It minimizes data transfer by sending only the differences between files.
SSH tunneling allows you to securely forward network traffic. To create a local tunnel:
ssh -L 8080:localhost:80 user@remote_host
This forwards traffic from your local port 8080 to the remote server’s port 80.
Reverse tunneling lets a remote server access your local machine:
ssh -R 9090:localhost:22 user@remote_host
This is useful for accessing a system behind a firewall.
If you frequently connect to the same server, you can speed up SSH by enabling multiplexing:
ssh -o ControlMaster=yes -o ControlPath=~/.ssh/socket user@remote_host
This reduces authentication overhead for multiple SSH sessions.
To close an SSH session, simply type:
exit
Or use the escape sequence:
~.
This is helpful when the connection becomes unresponsive.