Skip to content

3. Secure Shell (SSH) and Secure File Transfer Protocol (SFTP)

Scott Campit edited this page Feb 21, 2020 · 1 revision

This section covers logging onto remote machines using SSH and transferring files between your local and remote machines using SCP.

What is Secure SHell (SSH)?

SSH is a program that enables you to connect your local machine to a remote machine. This connection will allow you to transfer files between two machines and execute commands in the remote.

Installing SSH

To install SSH for both client and server side, type in the following command:

> sudo apt-get install openssh-client
> sudo apt-get install openssh-server

To see if the installation was successful, you should type in the following command to determine if it was a successful installation and activation:

> sudo systemctl status ssh 

If you find that ssh is installed, but is not active on your machine, you can start the ssh server by typing in the following comand:

> sudo systemctl start ssh 

Generating your SSH keys

Before we can log onto a remote machine, we should set up our SSH keys. These are a secure way of logging onto a remote device with SSH than using a password.

Generating a key pair provides you with two strings of characters:

  • A public key, that you should place on a server
  • A private key, that you should use to unlock the server from a local machine

To generate your keys:

  1. Type in the following command to start the key generation prompt:
> ssh-keygen -t rsa 
  1. Once you've entered this command, you can follow the next prompt shown below:
Enter file in which to save the key (/home/demo/.ssh/id_rsa): ``` 

The path in parentheses is the default path where the key will be saved in if you don't specify a specific place to store your keys.

3. Finally, we can set a password for your key, providing another layer of security. The prompt will look something like this:

``` bash
Enter passphrase (empty for no passphrase): 

You have created your key! Next, we will place the key on the server we want to use.

Copy the public key to the remote device and logging onto a remote machine

  1. To copy the public key into the new machine, we can use the ssh-copy-id command. To use this command, you will need to know the user name you want this key associated with, and the IP address of the remote machine.

Note: I used the localhost IP address as an example in the following code. If you actually try to log on using this IP address, you will return an error.

> ssh-copy-id name@127.0.0.1 

You should pull up a message that will ask you to continue connecting to the machine. Continue to follow the prompts to finish the transfer.

  1. Finally, to log onto the remote machine, you can type in the command with the following format:
> ssh remote_user@remote_ip_address 

What is Secure File Transfer Protocol (SFTP)?

Sometimes we need to transfer files from one computer to a remote machine (ie a server). Secure file transfer protocol or SFTP allows us to securely transferring computer files between two machines and is packaged with SSH.

Opening up an SFTP session

To open up an SFTP session to a remote machine, we can use the sftp command. Note that you must have established your ssh key before being able to do this:

# Establish a SFTP connection
> sftp remote_user@remote_ip_address 

Transferring files with SFTP

Receiving files from a remote machine

Suppose we want to download files from a remote server. We can do that by issuing the following command:

# Replace {remoteFile} with the name of the remote file you want to transfer
> get {remoteFile}

The get command downloads a remote file to a file with the same name on your local machine. We can also copy the remote file to a different name by specifying the name:

# Change the name of {remoteFile} to {localFile} name on your machine
> get {remoteFile} {localFile}

We can also copy whole directories using the following command:

# Get a whole directory recursively using the -r flag
> get ir {remoteDirectory}
Transferring files to a remote machine

Now that we know how to receive files from a remote machine, let's go over methods to transfer files to the remote machine. We can do this by using the put command.

# Use `put` to transfer files to a remote system
put {localFile}

# Use `put` and the `-r` flag to transfer directories to a remote system recursively
put -r {localDirectory}

Resources