Linux SFTP Server

Greetings Dev!

If you are looking for a secure way to transfer files over the internet or network, you may have heard about the SFTP protocol. SFTP stands for Secure File Transfer Protocol, and it is based on the SSH (Secure Shell) protocol that provides secure remote access to a server. In this journal article, we will guide you on how to set up and configure an SFTP server using Linux.

What is Linux SFTP Server?

Before we dive into the installation and configuration process, let us first discuss what Linux SFTP Server is. SFTP server is a software that enables the secure transfer of files between the client and the server. Linux offers a variety of options for SFTP servers, and OpenSSH is one of the most popular and widely used protocols.

OpenSSH

OpenSSH is an open-source implementation of the SSH protocol and offers a suite of secure network connectivity tools that allows secure communication between two untrusted hosts over an insecure network. It includes an SFTP server, which provides secure file transfer functionalities with strong encryption.

To install OpenSSH SFTP server on your Linux machine, follow the steps provided below:

Step 1: Check for Existing OpenSSH Server

Before proceeding with the installation, check if your machine already has OpenSSH server installed. To do that, run the following command:

Command
Description
sudo systemctl status sshd
Checks the status of the sshd service.

If the output shows that the service is not installed, you can proceed with the installation process. Otherwise, skip to Step 3.

Step 2: Install OpenSSH Server

To install OpenSSH server, run the following command:

Distribution
Command
Debian and Ubuntu
sudo apt-get install openssh-server
CentOS and Fedora
sudo yum install openssh-server

Step 3: Configure OpenSSH Server

After installing OpenSSH server, you need to configure it to enable SFTP access to the server. Follow the steps below:

Step 3.1: Create SFTP Group

Create a new group for SFTP users. To create a new group, run the following command:

Command
Description
sudo groupadd sftpaccess
Creates a new group called sftpaccess.

Step 3.2: Create SFTP User

Create a new user who will be allowed to access the SFTP server. To create a new user, run the following command:

Command
Description
sudo useradd -m -g sftpaccess -s /usr/sbin/nologin devuser
Creates a new user called devuser and adds it to the sftpaccess group with restricted shell access.

Replace devuser with the username you want to use for your SFTP server.

Step 3.3: Configure SSHD Configuration File

Open the SSHD configuration file using your preferred text editor:

Distribution
Command
All
sudo nano /etc/ssh/sshd_config

Find the following line in the configuration file:

Line
Description
#Subsystem sftp /usr/lib/openssh/sftp-server
Specifies the SFTP subsystem.

Uncomment the line by removing the # at the beginning of the line, and change it to:

Line
Description
Subsystem sftp internal-sftp
Specifies the SFTP subsystem as internal-sftp.

Add the following lines to the end of the configuration file:

Line
Description
Match Group sftpaccess
Specifies the match criteria.
ChrootDirectory %h
Specifies the directory to be chrooted for SFTP access.
ForceCommand internal-sftp
Forces the command to be internal-sftp.
READ ALSO  SQL Server Date Cast

Save the changes and exit the text editor.

Step 3.4: Restart SSHD Service

Restart the SSHD service to apply the changes. Run the following command:

Command
Description
sudo systemctl restart sshd
Restarts the SSHD service.

Step 4: Test SFTP Connection

To test the SFTP connection, use an SFTP client such as FileZilla or WinSCP. Follow the steps below:

Step 4.1: Connect to SFTP Server

Open your SFTP client and enter the following details:

Field
Value
Host
Your server IP address or domain name
Port
22
Protocol
SFTP – SSH File Transfer Protocol
Username
The username you created in Step 3.2
Password
The password for the username you created in Step 3.2

Click Connect to connect to the SFTP server.

Step 4.2: Upload and Download Files

You can now upload and download files between your client and the server using the SFTP client.

FAQ

Q1. How do I create a new user for SFTP access?

A1. You can create a new user for SFTP access by running the following command:

Command
Description
sudo useradd -m -g sftpaccess -s /usr/sbin/nologin devuser
Creates a new user called devuser and adds it to the sftpaccess group with restricted shell access.

Replace devuser with the desired username.

Q2. How do I test SFTP connection?

A2. You can test SFTP connection by using an SFTP client such as FileZilla or WinSCP. Enter the server details and connect to the server. Once connected, you can upload and download files between the client and server.

Q3. How do I configure SFTP server?

A3. You can configure SFTP server by editing the SSHD configuration file. Follow the steps provided in this article to configure SFTP server using Linux.

Q4. What are the advantages of using SFTP?

A4. SFTP provides a secure way to transfer files over the internet or network. It uses strong encryption to protect data during transfer, and it is based on the SSH protocol, which provides secure remote access to a server.

Q5. What are some popular SFTP clients?

A5. Some popular SFTP clients include FileZilla, WinSCP, Cyberduck, and PuTTY.