-
Notifications
You must be signed in to change notification settings - Fork 0
3. Secure Shell (SSH) and Secure File Transfer Protocol (SFTP)
This section covers logging onto remote machines using SSH and transferring files between your local and remote machines using SCP.
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.
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
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:
- Type in the following command to start the key generation prompt:
> ssh-keygen -t rsa
- 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.
- 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.
- Finally, to log onto the remote machine, you can type in the command with the following format:
> ssh remote_user@remote_ip_address
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.
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
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}
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}
Contributions are welcome! To contribute to this guide, please submit a pull request with a detailed description of:
- Which specific section you added or changed
- What you specifically added
- How does your contribution this make the tutorial better
Happy gitting!