How to SSH Into an External Server
2020-07-15 12:30:45 |
- Linux Ubuntu 20.04
The following guide will teach you how to use the terminal to access a remote computer, server, or virtual machine over SSH, eliminating the need to enter a password, every single time.
This is extremely convenient when you find yourself needing to log in to an external server, frequently. For a server to qualify for SSH access, you must already be able to remotely access it with a username and password. You must also have the proper permissions to upload a public key.
Generating the Private and Public SSH Key Pair
Run the following command on your local shell/terminal, substituting in your own email address.
ssh-keygen -t rsa -C "email@example.com"
This will generate a series of prompts. For tutorial purposes, just hit enter for all the default values that are presented. This will generate a key pair in your /home/user/.ssh/ directory.
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 id_rsa. Your public key has been saved in id_rsa.pub. The key fingerprint is: 52:e6:74:09:89:96:13:80:c4:41:5f:a9:1f:14:cd:33 myuser@My-Computer The key's randomart image is: +--[ RSA 2048]----+ | ++o..+O.. | | o. .B E. . | | .+ .+oo | | . .= . | | ...S | | .. | | | | | | | +-----------------+
Accessing the Server
Next, you'll need the server's IP address and the credentials of a user on the server in order to sign in to the machine for the first time. If you were to try to SSH into the machine, now, without having uploaded the public key yet, you'd receive a prompt to enter the user's password.
Let's sign in by running ssh user@serverip (substitute your username on the server and the server's IP address) and set up the necessary files on the server. Next, create a user for yourself with the following commands. Enter a secure password when prompted.
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, log out of the server with the exit command. Once you've run the following command which will append your local public key to the remote server's authorized_keys file:
cat ~/.ssh/id_rsa.pub | 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.