Skip to main content
SSH

Getting Started With SSH in Linux

This tutorial aims to provide you with some basic knowledge about setting up and using ssh to interact with remote systems.

seeni

Thanks to the connected world we live in, you don’t need to have physical access to your server anymore. Your server can be anywhere in the world and you can connect to it from your local machine.

There are many protocols and tools which are developed for this purpose. Those include telnet and ssh. Telnet is not preferred due to security concerns. On the other hand, ssh is the popular means of securil connecting to remote systems.

In this article, I will cover some useful commands and tools a Linux user must know to use a remote system and its resources over ssh.

Note that most of the commands that you run on your personal local Linux system should be available on the remote system also (such as ls, cat, cd command etc). But running them depends on the permissions allotted to a remote user as with any Linux/UNIX system.

What is SSH?

The ssh or secure shell is a network protocol for operating networking services securely over a network. It uses encryption standards to securely connect and login to the remote system.

It stores a public key in the remote system and private key in the client system. Thes keys are produced as a pair mathematically. When both are applied to a bi-variable function, it will result in a value which will be used to check whether the pair is valid or invalid. This is the simplest explanation possible. To Learn more, please refer to this page.

Examples of using SSH

Let’s get started with setting up ssh and really cool use cases.

Generate ssh key

Websites such as GitHub and Heroku are asking for your ssh public key so that you can push/deploy code without entering a password and you don’t have such a key-pair? Don’t worry. You can generate such ssh key pair with this  command:

ssh-keygen

It will prompt for a key-location (where the key will be saved) and passphrase (i.e. password). The passphrase is optional.

By default, the ssh keys are stored in .ssh directory under your home directory.

If the key-location is DIR_PATH/keypairforssh, there will be two files

  1. DIR_PATH/keypairforssh
  2. DIR_PATH/keypairforssh.pub

1 is the private key file which you must not share with anyone
2 is the public key file which can be shared with remote systems (by means of other trusted communication such as mail, physical transfer, and other secured communication tools) and services such as Github, Heroku for the respective use cases. Be sure to check thoroughly about the service for which you are connecting.

Add private key to the key-agent

When the key pair is created, it justs exists as a set of two files. In order to connect to the remote system, it has to use the private key. So one should inform that this DIR_PATH/keypairforssh is the private key.

This is done by

ssh-add keylocation

In our case , it is

ssh-add DIR_PATH/keypairforssh

Connect to remote host via SSH

If the private key and public key are in the right places, then you can connect to the system in this way.

ssh [username]@hostname

Where username should be a valid user on the remote system and hostname is DNS-recognizable or an IP address so that ssh can contact the remote system and request for connection.

For example, to connect to the system named “linuxhandbook” with the username “seeni” , use:

ssh seeni@linuxhandbook 

As explained before, the above command uses the private key on the local system and public key on the remote system and verifies these are valid pairs. It allows login if and only if key pair is valid and spawns a shell (type depends on the configuration for the user on the remote system) for your use. You can use the remote system as you are using the local system.

Suppose the private key is not added to the key agent, then you can do ssh login as below.

ssh -i /path/to/private/key/file username@hostname

This checking of key pairs is usually done once. Ssh adds the remote host to the list of authorized hosts for future usage.

Copying files between client and remote systems

The scp command is a tool built on the top of ssh. It allows users to copy files and directories from remote to client and vice versa.

Since scp command uses ssh, it needs the same requirement as ssh. It means that the public key should be on the remote system and the private key should be on the local system.

scp DIR_PATH_1 DIR_PATH_2

Where DIR_PATH_1/DIR_PATH_2 are both paths that are either remote or local filesystem paths. For example, To transfer ~/Documents/documentForLinux.txt to ‘linuxHandbook‘s /home/seeni/Documents directory

scp ~/Documents/documentForLinux.txt seeni@linuxhandbook:~/Documents

To copy the same file in reverse direction,

scp  seeni@linuxhandbook:~/Documents/documentForLinux.txt ~/Documents

Mounting remote filesystem or directory

To mount remote system directories to the client, sshfs is the tool
developed for this specific purpose.

sshfs name@server:/path/to/remote/folder /path/to/local/mount/point

The above command is totally intuitive. Here, ‘name’ is the username accepted on remote system and server is the remote ‘hostname’.

In some systems, sshfs may not be available, install it if you need it.

Tip: The nohup command allows you to keep on running commands even after you disconnect your SSH connection.

Conclusion

Congratulations, you did make it to the end. I hope this article covered all the basic commands and tools related to ssh. These tools are just enough to get started with remote computing with ssh.

In a related article, you can learn about tmate. It’s a tool that allows you to share your terminal session over SSH.

Hope, you find this article useful. If you have any suggestions, feel free to drop them in the comment section below.

seeni