How to Set Up SSH on Ubuntu and Use SCP for File Transfers

Jobin J
3 min readJun 19, 2024

--

Introduction

Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. SSH is typically used to log into a remote machine and execute commands. It also supports tunneling, forwarding TCP ports, and transferring files using the associated SSH file transfer protocols (SCP and SFTP).

In this blog post, we’ll walk you through the steps to install and configure SSH on an Ubuntu machine, set up access, and use SCP for transferring files.

Prerequisites

  • An Ubuntu machine (local or remote)
  • Basic knowledge of the terminal
  • Sudo privileges on the Ubuntu machine

Step 1: Install SSH

1. Update the package list: Open a terminal and update your package list to ensure you have the latest information on the newest versions of packages and their dependencies.

sudo apt update

2. Install OpenSSH Server: Install the OpenSSH server package to enable SSH on your Ubuntu machine.

sudo apt install openssh-server

3. Start and Enable SSH Service: Ensure the SSH service is running and enable it to start on boot.

sudo systemctl start ssh
sudo systemctl enable ssh

4. Verify SSH Service: Check the status of the SSH service to ensure it’s running correctly.

sudo systemctl status ssh

Step 2: Accessing Your Ubuntu Machine via SSH

1. Find Your Machine’s IP Address: Determine the IP address of your Ubuntu machine. You can find this using the ip command.

ip a

2. SSH into Your Ubuntu Machine: Use an SSH client (such as OpenSSH on Linux/Mac or PuTTY on Windows) to connect to your Ubuntu machine.

ssh username@ip_address

Replace username with your Ubuntu username and ip_address with your machine's IP address.

Step 3: Secure Your SSH Setup

1. Change the Default SSH Port: Edit the SSH configuration file to change the default port (optional but recommended for security).

sudo nano /etc/ssh/sshd_config

Find the line #Port 22 and change it to a port number of your choice, e.g., Port 2222. Save the file and exit.

2. Disable Root Login: It’s recommended to disable root login over SSH for security reasons. In the same configuration file, find the line PermitRootLogin and change it to no.

3. Restart SSH Service: Apply the changes by restarting the SSH service.

sudo systemctl restart ssh

Step 4: Using SCP for File Transfers

Secure Copy Protocol (SCP) is used for securely transferring files between machines. SCP uses SSH for data transfer, ensuring the same level of security.

1. Copy a File from Local to Remote:

scp /path/to/local/file username@remote_ip:/path/to/remote/directory

2. Copy a File from Remote to Local:

scp username@remote_ip:/path/to/remote/file /path/to/local/directory

3. Copy a Directory Recursively: To copy entire directories, use the -r flag.

scp -r /path/to/local/directory username@remote_ip:/path/to/remote/directory

Conclusion

By following these steps, you’ve set up SSH on your Ubuntu machine, secured the SSH access, and learned how to use SCP for secure file transfers. SSH and SCP are powerful tools that enhance your ability to manage and transfer files securely between machines.

Feel free to reach out if you have any questions or run into any issues!

--

--