December 22, 2024
Tutorials

How to Use SFTP to Securely Transfer Files with a Remote Server on Ubuntu 22.04?

How to Use SFTP to Securely Transfer Files with a Remote Server

SFTP (Secure File Transfer Protocol) is a tool provided by OpenSSH. The primary function of SFTP is to encrypt and secure data during network transfers. It is widely used to safeguard and secure sensitive data that only authorized users have access to the data.

This article aims to give you an insight into how to install, configure, and use SFTP to transfer files with a remote server on Ubuntu 22.04 securely. Also, check How To Use Rsync to Sync Local and Remote Files and Directories in Ubuntu 22.04.

How to Use SFTP to Securely Transfer Files with a Remote Server on Ubuntu 22.04?

Before initiating to securely transfer files with a remote server, make sure that the SFTP server is installed and configured on your Ubuntu 22.04 system.

To set up the environment for the SFTP server, follow these steps.

How to Install SFTP Server using OpenSSH?

These instructions can be followed to install SFTP on your Ubuntu 22.04 machine using the command line.

Step 1: Update System Files

Updating the system files is the necessary step to refresh the system repositories and packages. When the system has the latest versions of installed packages, the system’s performance will improve:

sudo apt update

The output shows that all the available packages have the latest versions on your system.

Step 2: Install OpenSSH Server via APT

The OpenSSH suite includes tools like SSH, SCP, and SFTP for performing remote operations. Therefore, utilize the APT package manager to install the complete package of OpenSSH on your Ubuntu 22.04 system:

sudo apt install openssh-server -y

The APT package manager has installed the OpenSSH along with its large suite, such as SSH and SFTP on your Ubuntu.

Step 3: Check SSH Status

You can easily check the status of the SSH service through the command:

sudo systemctl status ssh

The active status on your screen indicates that the OpenSSH is functional.

How to Create a Group and a New User for SFTP?

In this session, you will see how to create a secure group and a new user for SFTP using a terminal.

Step 1: Create a Group

Create a new group named mygroup to manage user permissions, especially when configuring an SFTP server:

sudo addgroup mygroup

As you can see, mygroup has been created successfully.

Step 2: Create a User within mygroup

After creating a new group, run the command to create and add the new user (user1) to the group:

sudo useradd -m user1 -G mygroup

The command returned an empty output which means that a new user was added to mygroup.

Step 3: Create User Password

To secure access to the user1 account, execute the command to set a password:

sudo passwd user1

The password against the user1 has been successfully created.

Step 4: Permit to a Particular User

Run the command to grant full control to the user1 over the home directory:

sudo chmod 700 /home/user1

Here, 700 means that only user1 has full access (read, write, execute) to the /home/ directory.

Step 5: Modify the Configuration Settings

Open the Nano text editor using the terminal:

sudo nano /etc/ssh/sshd_config

Within the SSH configuration file, add the following lines of code to restrict the file transfer to a specific group, such as mygroup:

Match group mygroup 
ChrootDirectory /home 
X11Forwarding no 
AllowTcpForwarding no 
ForceCommand internal-sftp

Make sure to replace mygroup with the name of your specified group.

How to Use SFTP to Securely Transfer Files with a Remote Server?

Follow these steps to establish the SFTP connection and securely transfer files with a remote server.

Step 1: Establish the SFTP Connection for user1

Run the sftp command followed by a username and IP address to connect to a remote server from your Ubuntu 22.04 machine:

sftp user1@127.0.0.1

The SFTP is now connected to localhost via the 127.0.0.1 IP address.

Step 2: Display Present Working Directory

Check your current directory location within the SFTP server by using the pwd command:

pwd

The present working directory is /home/user1.

Step 3: Make a New Directory

You can create a directory with mkdir command:

mkdir testFile

Step 4: List the Existing Files

Run the ls command to view the available files in the present path:

ls

The new directory named testFile has been created without encountering any error.

Step 5: Exit from the SFTP Terminal

You can exit from the SFTP server at any time using the command:

exit

How to Use a GUI Method to Securely Transfer Files via SFTP?

In the following, we will use a graphical interface to transfer files using SFTP.

Step 1: Locate Files > Other Locations

SFTP allows you to access the SFTP server via a graphical user interface (GUI):

  • Open Files of your Ubuntu 22.04 machine.
  • Select Other Locations.
  • Type the IP address of the SFTP server in the Connect to Server field.
  • Click on the Connect button.
sftp://127.0.0.1/

Step 2: Authenticate the SFTP Server

Enter the username created within the mygroup, type your password, and click the Connect button to establish the SFTP server connection:

You are now remotely connected to the SFTP server and can view the files within the directory:

Conclusion

In Ubuntu 22.04, you can securely transfer files to a remote server using the SFTP server. To create a secure server, install the OpenSSH package on your local machine.

The command sudo apt install openssh-server can be run from the terminal to install the complete package on your Ubuntu 22.04. You can create a group and user to manage permissions easily after installing the package. Establish SFTP connection using the command, such as sftp user@IP.

This article practically demonstrated the complete process of installing, configuring, and file transferring with a remote server on Ubuntu 22.04.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video