SSH Tutorial With Command Examples
SSH is the one of the most popular tool in the Linux and Unix world. SSH or Secure Shell as its name suggests creates secure shell connections to the remote systems. Most of the Linux system administrators prefer SSH to manage remotely. SSH creates encrypted channels to the remote system and transmits data through this secure channels.
Basic ssh syntax is like below.
ssh [options] [user@]hostname [command]
Connect To Remote
Simple usage of ssh is just providing hostname or IP address of remote system and connect. User name is supplied from current user. Simply current users username is used for remote system.
$ ssh 192.168.122.22
Specify Username Explicitly
In previous example username is supplied by the session. What is we need to use different username for the remote connection? Adding username to the remote system IP address or hostname with @ sign like below can solve this.
$ ssh firstname.lastname@example.org
We can provide the username with -l parameter like below.
$ ssh -l root 192.168.122.22
Specify Port Number
Ssh protocol uses TCP port 22 by default. Ssh clients also assumes remote system uses default port number. In some cases the port number can be different than 22 so the remote port number should be provided to the ssh explicitly with -p parameter like below.
$ ssh -p 22 192.168.122.22
Create Public Private Keys With SSH Keygen
SSH is very advanced and feature-full protocol. SSH provides different protocols for authentication. Default one is password based authentication as we previously did. Key based authentication is more secure than password based authentication. To use key based security public and private keys should be created for the user.
During key generation process some questions are asked. Here are steps.
- By default keys are stored in users home directory. The exact path is /home/ismail/.ssh/ and keys are named id_rsa.pub, id_rsa . If we want to change default values we can provide them if not justpress enter and skip.
Enter file in which to save the key (/home/ismail/.ssh/id_rsa):
- Keys can be protected by encypting them. Especially private key named id_rsa is important. If we want to encrypt then enter password, if not just press enter and skip.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Key Based Authentication
We have previously created our keys. We can use these keys to automatically authenticate without providing any password. First we should deploy our public key to the remote system. Deployment is easy because of SSH tool named ssh-copy-id . Remote username and host provided like below. Our public key is deployed to the remote users authorized keys database. By default our public key is /home/ismail/.ssh/id_rsa.pub
$ ssh-copy-id email@example.com
Explicitly Specify Keys
SSH behaivour is by default using keys /home/ismail/.ssh/id_rsa.pub and /home/ismail/.ssh/id_rsa but this is not practical some times. We can provide keys explicitly with the -i parameter like below.
$ ssh -i .ssh/id_rsa firstname.lastname@example.org
Run Command Remotely
To run single line command on the remote hosts opening new shell and typing command may become a hard work for us. SSH provides ability to run commands remotely without opening interactive bash shell. This will run command ip address show on the remote system.
$ ssh 192.168.122.22 "ip address show"
SSH configuration generally resides on the /etc/ssh/ssh_config or ~/.ssh/config . Alternative configuration can be specified with -F parameter like below.
$ ssh -F ssh_config 192.168.122.22
Port forwarding is useful feature provided by SSH. General definition of port forwarding is tunneling local or remote system ports each other. For example I want to connect google.com from local system through remote system. Port forwarding should be enabled while connecting to the remote system.
Local Port Forwarding
In local port forwarding local port will forwarded to the remote system and then to the destination system host and port.
-L LOCALPORT : DESTINATIONHOST : DESTINATIONPORT
$ ssh -L 2222:poftut.com:2222 192.168.122.22
After we have connected to the remote system local port number 2222 is opened in the local system. We can check local port 2222 with the following command.
$ netstat -tl | grep 2222
Now if we try to connect local port 2222 this port will be forwarded to the host poftut.com and port 2222
Remote Port Forwarding
Remote port forwarding is the reverse of the local pot forwarding. Hostname provided for the forwarding will be tunelled from remote system through our local system.
-L REMOTEPORT : DESTINATIONHOST : DESTINATIONPORT
$ ssh -R 5900:localhost:5900 192.168.122.22
In the remote system with IP address 192.168.122.22 port 5900 is opened and this port is forwarded to the local systems port 5900.
Dynamic Port Forwarding
Dynamic port forwarding will use SOCKS which default port number is 1080. But another port number can be used. SOCKS generally used to proxy browsers like Chrome, Firefox, Opera. Proxy traffic will be forwarded to the remote system.
$ ssh -D 1080 192.168.122.22
Linux systems uses for GUI X11 server. One of the best feature of SSH is remote application with GUI can be run on local system. Application actually runs on remote system but GUI or X11 protocol is forwarded to the local system and shown like a local application. To enable forwarding provide -X parameter.
$ ssh -X 192.168.122.22
Just with a single shot
$ ssh -X 192.168.122.22 firefox
SSH can save from bandwidth and network usage by compressing its network traffic. Compression can provide benefits is the data is high compression rate like text. Compression can be enabled per session with -C parameter.
$ ssh -C 192.168.122.22
Specify Source Address
Some local systems may have multiple interfaces and/or multiple IP addresses. This can be a problem if remote system have IP address based network restrictions. Setting specific source IP address can over come this problem.
$ ssh -b 10.0.3.1 192.168.122.22
SSH client can produce logs. Normally these logs will be printed out to the console. But they can be written to a file with -E option.
$ ssh -E ssh.log 192.168.122.22
Verbose Mode and Debug
Some times we can have trouble to connect remote system with SSH. In this situations the best solution is to debug connection steps. SSH client can provide verbose and debug output with -v . Adding more like -vvv will increase debug level and output.
$ ssh -v 192.168.122.22
To display SSH client version and exit use option -V
$ ssh -V