If you have been in the IT world for quite some time you probably have heard about SSH, how great a tool it is and all its cool security features. In this tutorial you will learn how to use SSH in a few minutes and login to your remote computers seamlessly and securely.
If you have no clue what SSH is you can visit Wikipedia before proceeding.
The simplest usage of SSH is where you specify a user and the hostname. The hostname could be an IP address or a domain name an in the following format.
$ ssh user@hostname
For example to login to a Raspberry Pi on my LAN, I would simply type the command in the terminal as follows:
$ ssh email@example.com
Where pi is the user and 10.42.0.47 is the IP of the Raspberry Pi on my LAN. Change this accordingly to reflect your LAN configuration or your remote computer's IP address.
If you have logged in successfully then the rest of the guide shall be a breeze for you.
Using A Different Port
By default ssh uses port 22, but for various reasons you may want to connect to another port.
$ ssh -p 10022 user@hostname
This will connect to ssh via port 10022 instead of port 22.
Execute Commands Remotely
At times its convenient to execute a command on the remote host and get the output and continue working on the local machine. Well SSH has catered for this need,
$ ssh firstname.lastname@example.org ls -l
Mounting remote filesystems
Another great tool based on ssh is sshfs. With sshfs you can mount remote filesystems and have the remote files on the local machine.
$ sshfs -o idmap=user user@hostname:/home/user ~/Remote
For example this command can be used as:
$ sshfs -o idmap=user email@example.com:/home/pi ~/Pi
This will mount pi's home directory to a folder on the local machine called Pi.
For more details on sshfs look at our sshfs tutorial.
Suppose now you want to run a GUI program on your remote computer? SSH had you in mind! Login to the remote machine with the basic SSH command but -X option. This will allow X11 forwarding. After you login you might not see any difference, but once you invoke a GUI based program you notice the difference.
$ ssh -X firstname.lastname@example.org
Now you may want to do other stuff on the command line while running the GUI program. Simply suffix the command with &.
There are various escape sequences provided by SSH. To view them, SSH to any remote machine then type tilde(~) followed by a question mark. You will see a couple of other supported escape sequences. In this example you can the output of ~# and ~C.
Edit SSH Configuration
If you need to change SSH configuration, open the file /etc/ssh/sshd_config with your favourite text editor and edit whatever you need to. For example we might need to change the banner. In your text editor find the following line:
Uncomment the line by deleting the # then add a path to the file with the message you want displayed. The line should now read as:
In this /etc/ssh/sshd_config file you will also find the options of changing the port number, idle logout timeout e.t.c . These are fairly straight forward, but refer to the ssh manual for anything that might not be familiar before attempting to make changes.
Generate SSH Key Pair
To generate a new key pair run the command as follows:
$ ssh-keygen -t dsa
Finding A Hostkey
Now before you add that key pair it does no harm to see if it exists already.
$ ssh-keygen -F 10.42.0.47
Removing A Hostkey
Sometimes its necessary to remove a key pair you had generated, for example when the host has changed or perhaps when you need to remove keys that are no longer used.
$ ssh-keygen -R 10.42.0.47
With the above commands you will be able to use SSH with ease. There is more to explore and your imagination is your limitation.