Introduction
In this article, we will guide you through the process of setting up Secure File Transfer Protocol (SFTP) on a Linux machine for multiple users. SFTP is a secure version of File Transfer Protocol (FTP) that encrypts data during transfer, making it an ideal choice for transferring sensitive files over the internet. This setup is essential for organizations and individuals who need to share files between team members or collaborate on projects.
Prerequisites
Before we begin, ensure that your Linux machine meets the following requirements:
- A Linux distribution (Ubuntu, CentOS, Debian, etc.) installed and updated
- Root access or sudo privileges
- OpenSSH server installed
Existing Folder Structure
Let's assume that you have an existing folder structure in place:
/var/home/outbound/ ├── user1 ├── user2 └── user3
Configuring SSH for Multiple Users
To enable SFTP access for multiple users, we need to configure their SSH keys and set up their home directories.
Generating SSH Keys for Users
Each user needs to generate an SSH key pair on their local machine:
user1@localmachine:~$ ssh-keygen -t rsa -b 4096 -C "user1@localmachine"
user1@localmachine:~$ mv ~/.ssh/id_rsa.pub ~/.ssh/id_rsa_user1.pub
Repeat the process for all other users:
user2@localmachine:~$ ssh-keygen -t rsa -b 4096 -C "user2@localmachine"
user2@localmachine:~$ mv ~/.ssh/id_rsa.pub ~/.ssh/id_rsa_user2.pub
user3@localmachine:~$ ssh-keygen -t rsa -b 4096 -C "user3@localmachine"
user3@localmachine:~$ mv ~/.ssh/id_rsa.pub ~/.ssh/id_rsa_user3.pub
Copying SSH Keys to the Linux Machine
Now, we need to copy each user's public key to the Linux machine:
root@linuxmachine:~# mkdir /etc/ssh/users
root@linuxmachine:~# touch /etc/ssh/users/user1.conf
root@linuxmachine:~# touch /etc/ssh/users/user2.conf
root@linuxmachine:~# touch /etc/ssh/users/user3.conf
Add the following content to the respective user configuration files:
Host user1
HostName linuxmachine
User user1
IdentityFile /home/user1/.ssh/id_rsa_user1.pub
Repeat the process for user2 and user3:
Setting Up Home Directories
Create home directories for each user:
root@linuxmachine:~# useradd -m user1
root@linuxmachine:~# useradd -m user2
root@linuxmachine:~# useradd -m user3
Set the correct group ownership:
root@linuxmachine:~# chown -R user1:user1 /var/home/outbound/user1
root@linuxmachine:~# chown -R user2:user2 /var/home/outbound/user2
root@linuxmachine:~# chown -R user3:user3 /var/home/outbound/user3
Enabling SFTP
Finally, we need to enable SFTP for each user:
root@linuxmachine:~# vi /etc/ssh/sshd_config
Add the following lines at the end of the file:
Match User user1
ForceCommand internal-sftp
AllowTcpForwarding no
Repeat the process for user2 and user3:
Match User user2
ForceCommand internal-sftp
AllowTcpForwarding no
Save and exit the file.
Testing the Setup
Each user can now log in using their SSH key:
user1@localmachine:~$ ssh user1@linuxmachine
They will be dropped into their home directory and can use SFTP to transfer files:
In this article, we have covered the process of setting up SFTP on a Linux machine for multiple users. We have explained the prerequisites, existing folder structure, and steps to configure SSH keys, copy keys to the Linux machine, set up home directories, and enable SFTP. This setup ensures secure file transfer between team members or collaborators while maintaining data privacy and access control.