Saturday, November 15, 2014

How to SSH into an External Server

Whether you're setting up a local machine or a cloud-based server, unless you really love typing in your username and password, setting up SSH keys is something you'll do time and time again. At least until you've implemented some sort of auto-provisioning system. Let's get right to it.

Prerequisites: This guide was written for Mac OS X and Linux Ubuntu users.

Generating the private and public SSH Keys

Run the following commands on the client machine; your local desktop.

ssh-keygen -t rsa -C ""

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in test.
Your public key has been saved in
The key fingerprint is:
52:e6:74:09:89:96:13:80:c4:41:5f:a9:1f:14:cd:33 roblayton@Robs-MacbookAir-3
The key's randomart image is:
+--[ RSA 2048]----+
| ++o..+O..       |
|  o. .B E. .     |
|    .+ .+oo      |
|    . .= .       |
|     ...S        |
|      ..         |
|                 |
|                 |
|                 |

The key's filename and passphrase are optional. We won't override the key or type in a passphrase. Unless you've indicated a path and filename, you'll find the key at /Users/<user>/.ssh/ on a Mac or /home/<user>/.ssh/ on Ubuntu. The private key will be located in the same directory as the public key.

Accessing the Server

Next, you'll need the server's IP address. You can make life easier by following this guide. You'll need the credentials of a user on the server in order to sign in to the machine for the first time. So if you were to try to ssh into the machine now, without having uploading the public key, you'd receive the following output and be prompted to enter the user's password.

ssh user@server
user@server's password:

Let's sign in anyway and set up some files on the server. Once you're in the server, create a user for yourself with the following commands.

sudo adduser user
[sudo] password for ubuntu:
Adding user `user' ...
Adding new group `test' (1002) ...
Adding new user `test' (1002) with group `user' ...
Creating home directory `/home/user' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for user
Enter the new value, or press ENTER for the default
 Full Name []: Name
 Room Number []:
 Work Phone []:
 Home Phone []:
 Other []:
Is the information correct? [Y/n] Y

Now, run the following command which will append your public to the authorized_keys file:

cat ~/.ssh/ | ssh user@server "cat >> ~/.ssh/authorized_keys"

Now, when you ssh into the server, you won't need to enter your password. Try it out yourself by running ssh user@server.

No comments:

Post a Comment