How to connect to a Server via SSH

If your web hosting provider has given you access to your server via SSH, you can login via a terminal. In order, to get started, there are a few basic commands you need to know.

Note: Unmanaged Servers

If you signed up for an unmanaged server, you probably didn't receive any FTP details. Instead you will need to install and setup an FTP server via SSH or you could use SFTP to upload and download files.

Generally, the file path for your web directory can be found at “/var/www/”.

Standard SSH Connection

ssh [user]@[server]

Example#1 (connect via domain name)

ssh root@w3db.org

Example#2 (connect via IP address)

ssh root@107.170.153.130

SSH Connection via specified port number

Syntax

ssh [user]@[server] -[port-number]

Example

ssh root@w3db.org -32

Create an SSH key

SSH keys are a more secure way of logging on to a server than just using a password. It is possible for a password to be brute forced, whereas SSH keys (which uses public key encryption) are almost impossible to be broken by brute force. On the client machine we will create a public/private key pair, then copy the public key to the server. The private key can also be protected with a password for greater security.

Public key encryption (also known as asymmetric encryption) uses paired public and private keys to secure communications. The public key is used to encrypt data and the private key decrypts. The simplest way of understanding this is to think of the public key as a padlock and the private key as an actual key, there can be multiple public keys (padlocks) and they can be on different servers as long as the private key (actual key) remains secure it will be able to open these padlocks.

Create the keys

Syntax
ssh-keygen -t [encryption type]
Example
ssh-keygen -t rsa

Accept all defaults, this creates the two files below on the client. id_rsa is the private key, id_rsa.pub is the public key.
/home/$USER/.ssh/id_rsa.
/home/$USER/.ssh/id_rsa.pub.

Copy public key to the server

Syntax
ssh-copy-id [user]@[server]
Example
ssh-copy-id root@example.com

Add the private key to authentication agent

The authentication agent is a program that stores private keys. The ‘ssh-add' command copies the private key from the .ssh directory to the authentication agent, so that the SSH client can use it to authenticate with the public key that we have just copied to the server.

ssh-add

Logon to server

Syntax
ssh [user]@[server]
Example
ssh root@w3db.org

You should now be able to logon to the server without a prompt for a password.