Getting Started with SSH
SSH is an industry standard for remote management of Unix-like systems and many network devices. This article will walk you through its usage and configuration.
Over the last fifteen years, SSH has become a standard tool for remote management of Unix-like systems and many network devices. SSH stands for Secure Shell, and is one of the ways to get a command line (shell) access on a remote machine. It was designed to be a secure alternative to previous access methods such as telnet.
Unlike some previous techniques, SSH encrypts all the data that you send and receive from the remote server, and offers secure authentication using SSH keys. This article will describe its basics and help you get started with the following:
- Connecting to a remote host via SSH
- Generating and managing your SSH keys
- Executing commands over SSH
- Using SSH agent for managing multiple SSH keys
While the aim of this article is to cover the basics, it will still require some basic understanding of the command line and the structure of Unix-like systems.
It will also assume that you have an installed version of OpenSSH on your machine. This package comes preinstalled if you use any of the modern Linux distributions.
To make SSH really useful, you will need a pair of SSH keys. These keys serve as a way to identify yourself to a remote server using public-key cryptography.
An SSH key consists of two parts — a private key you should keep for yourself, and a public key that can be shared with anyone. The private key should always stay on your local computer, and you should take care not to lose it or let it fall into malicious hands. The public part is the part of the key that you upload to a remote machine you want to access via SSH.
By combining these two keys, a remote machine can determine your identity. When you connect to a remote machine to which you have already uploaded your public key, it will send a challenge-type response and ask your local machine to identify with its private key.
Generating an SSH Key
To generate an SSH key, run the following command in your local terminal:
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/igor/.ssh/id_dsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/igor/.ssh/id_dsa. Your public key has been saved in /home/igor/.ssh/id_rsa.pub. The key fingerprint is: 12:23:34:56:21:g3:g9:93:86:af:4r:bb:11:5d:f8:h9
If you accept all the default values, the above command will generate
two files on your system — the
files. The first file is your private SSH key, while the second one
represents your public key.
Setting Up Keys on Remote Machines
When you have an SSH key pair and a remote machine where you want to connect, you will usually give the public part of that key to a person who already has access to that remote machine.
Your public key should be placed inside the
the home directory of the user you want to use on the remote machine.
For example, if you have created your SSH keys as shown in the above example, you can display your public SSH key by executing the following command:
$ cat ~/.ssh/id_rsa.pub ssh-rsa BBBB3NzaC1yc2EAAAADAQABAAABAQCv9Gz2XJ9Rl6WzcFX0hvmm4Ipjwr2KoaX5i7SqUrn7hAg87cmCGVmvIarE5u8WJYADeRo1QN/ySNnxQkGq9h2qSYK9rnT4M5s280lg9R+YdEkAKf7HzQKSA+QVxnyvV0uLvZKtUHw13gjFCRBCd5BFCYQQ2jsz2DVpXbw58ZIJtlO6Ev3V9+HX3EqR6Q7IVNCjb9HgJql9yKZOvRk+IAlyjpIcVCgDoKYsTjsTA4aSIDqVevenmnYNsk4jFiqcJeHKoEyByoqEkt2NcU0EAG+Ff2pHg5du32Y+iSdF2d/hIMoYikXnX17hpFxXR1+9H02NP76cmzt9IxlEtsGJWYxh igor@devbox
You can copy the above key, and give it to a person who has access to the
remote machine. Let's say that the name of the remote user you want to use
when connecting to the machine is
john. The content of your public key should
then be appended to the
That task can be achieved with the following command on the remote machine:
echo "ssh-rsa BBBB3NzaC1yc2EAAAADAQABAAABAQCv9Gz2XJ9Rl6WzcFX0hvmm4Ipjwr2KoaX5i7SqUrn7hAg87cmCGVmvIarE5u8WJYADeRo1QN/ySNnxQkGq9h2qSYK9rnT4M5s280lg9R+YdEkAKf7HzQKSA+QVxnyvV0uLvZKtUHw13gjFCRBCd5BFCYQQ2jsz2DVpXbw58ZIJtlO6Ev3V9+HX3EqR6Q7IVNCjb9HgJql9yKZOvRk+IAlyjpIcVCgDoKYsTjsTA4aSIDqVevenmnYNsk4jFiqcJeHKoEyByoqEkt2NcU0EAG+Ff2pHg5du32Y+iSdF2d/hIMoYikXnX17hpFxXR1+9H02NP76cmzt9IxlEtsGJWYxh igor@devbox" >> /home/john/.ssh/authorized_keys
Connecting to a Remote Machine
Once you have properly set up your public SSH key on a remote host, you will be
able to access the machine by executing the
ssh command in your local
For example, if
18.104.22.168 is the IP address of your remote machine, you can
create an SSH connection to it using this command:
The first time you connect to the remote machine,
ssh will ask your permission
to put the fingerprint of the remote machine in your local
The authenticity of host '22.214.171.124' can't be established. RSA key fingerprint is 12:23:34:56:21:g3:g9:93:86:af:4r:bb:11:5d:f8:h9. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '126.96.36.199' (RSA) to the list of known hosts.
The first time you connect to the server you should type in
yes to proceed.
The above question is an integral part of the SSH mechanism, and it protects
you from malicious users who want to "sniff" your data over
the network. The fingerprint is a way to identify a remote machine.
If the fingerprint of the remote server changes, the next time
you connect to the machine,
ssh will print out a warning message
similar to the following:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: POSSIBLE DNS SPOOFING DETECTED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ The RSA host key for 188.8.131.52 has changed, and the key for the according IP address 184.108.40.206 is unchanged. This could either mean that DNS SPOOFING is happening or the IP address for the host and its host key have changed at the same time. Offending key for IP in /home/igor/.ssh/known_hosts:10 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that the RSA host key has just been changed. The fingerprint for the RSA key sent by the remote host is 12:23:34:56:21:g3:g9:93:86:af:4r:bb:11:5d:f8:h9. Please contact your system administrator. Add correct host key in /home/igor/.ssh/known_hosts to get rid of this message. Host key verification failed.
If you get the above message, make sure to contact your system administrator to determine if the fingerprint has really changed, or if someone is trying to listen in on your connection.
On the other hand, if everything goes smoothly, you should be able to enter the remote host via SSH:
$ ssh email@example.com firstname.lastname@example.org ~/ $
You can also use the hostname of the remote machine. For example, to connect to
ssh-test.com host, you can execute the following:
Running Commands over SSH
There are several ways to run commands on the remote machine via SSH. For example, to list the content of the remote user's home directory, you can run the following on your local machine:
ssh email@example.com "ls -lah"
The above command will create a connection to the remote server, and immediately execute the command. When the command stops, the connection will also be closed.
The above scheme will work in the majority of cases, but if you want to
vim or a similar interactive application on the remote host, you will
need to use the
-t command. For example, to edit
test.txt on the remote
machine, type the following on your local machine:
ssh -t firstname.lastname@example.org "vim test.txt"
When you close
vim, the connection to the remote host will also close.
On the other hand, if you want to open an interactive shell on the remote
machine, you can enter it by executing the following command:
$ ssh -t email@example.com firstname.lastname@example.org ~/ $ ls test.txt email@example.com ~/ $ cat test file
The above session won't be closed until you execute an
command on the remote machine.
If you have multiple SSH keys, or if the SSH keys are encrypted with a passphrase, SSH agents can help you manage them with ease.
Starting an SSH agent can be achieved by executing the following on your local machine:
After the above command, you will have the ability to add your private keys to
the agent using the
ssh-add ~/.ssh/id_rsa ssh-add ~/.ssh/github_private_key_rsa
In the above example, we added two private keys to the
SSH agents are most useful when we want to forward our keys to a remote machine. For example, they can be of great help if you have an SSH key that lets you access your GitHub repository from your local machine, but you want to clone the repository on the remote machine using that local key. Luckily, this can be achieved by starting an SSH agent and connecting to the remote machine.
For example, the following workflow lets you clone your repository on the remote machine with a local SSH key:
$ eval `ssh-agent` Agent pid 420 $ ssh-add ~/.ssh/github_private_key_rsa Identity added: /home/igor/.ssh/github_private_key_rsa (/home/igor/.ssh/github_private_key_rsa) $ ssh firstname.lastname@example.org email@example.com ~ $ git clone firstname.lastname@example.org:shiroyasha/squash.git Cloning into 'squash'... remote: Counting objects: 31, done. remote: Compressing objects: 100% (8/8), done. remote: Total 31 (delta 9), reused 4 (delta 4), pack-reused 19 Unpacking objects: 100% (31/31), done. Checking connectivity... done. email@example.com ~ $ exit Connection to 220.127.116.11 closed.
In this article we saw how to use SSH to get shell access to remote machines. We covered how to use and generate SSH keys, and introduced the SSH agent that gives us better control over our keys.
There is, of course, much more to learn about SSH. Here are some great resources to help you: