SSH is one of the most popular tools 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 these secure channels.
Basic ssh syntax is like below.
ssh OPTIONS USERNAME@HOSTNAME COMMAND
- `OPTIONS` is used to specify ssh command options which can change auth type, compression, etc. most of them explained in this tutorial
- `USERNAME` is the user name we want to use to authenticate on the remote system or host. USERNAME is optional where if it is not specified current user name is used.
- `HOSTNAME` is the hostname or IP address of the remote system or host we want to connect.
- `COMMAND` is optional where we can run command on the remote system without getting an interactive shell.
Connect To Remote
Simple usage of ssh is just providing hostname or IP address of remote system and connect. The 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 the 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 email@example.com
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/Certificates With SSH Keygen
SSH is very advanced and feature-full protocol. SSH provides different protocols for authentication. The default one is password-based authentication as we previously did. Key-based or certificate-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 the 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 just press enter and skip.
Enter file in which to save the key (/home/ismail/.ssh/id_rsa):
- Keys can be protected by encrypting them. Especially private key named id_rsa is important. If we want to encrypt then enter the 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 firstname.lastname@example.org
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 email@example.com
Run Command Remotely
To run single line command on the remote hosts opening new shell and typing command may become hard work for us. SSH provides the ability to run commands remotely without opening an 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. The 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 a remote system. Port forwarding should be enabled while connecting to the remote system.
Local Port Forwarding
In local port forwarding local port will be 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 the 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 tunneled 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 use for GUI X11 server. One of the best feature of SSH is a remote application with GUI can be run on the local system. Application actually runs on a 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 a specific source IP address can overcome 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 verbosely and debug output with -v . Adding more like -vvv will increase debug level and output.
$ ssh -v 192.168.122.22
To display the SSH client version and exit use option -V
$ ssh -V