What You’ll Need
- A server or virtual machine with Docker installed.
- Basic understanding of networking and file permissions.
- An SSH client for testing SFTP connections.
Step 1: Install Docker (If Not Installed)
-
Update the system packages:
sudo apt update && sudo apt upgrade -y
-
Install Docker:
sudo apt install -y docker.io
-
Enable and start Docker:
sudo systemctl enable docker && sudo systemctl start docker
Step 2: Set Up the SFTP Directory
-
Create a directory structure for SFTP users:
mkdir -p ~/sftp/shared mkdir -p ~/sftp/user1
-
Set the appropriate permissions:
chmod 700 ~/sftp/user1 chmod 755 ~/sftp/shared
Step 3: Deploy the SFTP Server Using Docker
-
Pull the SFTP Docker image:
docker pull atmoz/sftp
-
Run the SFTP container:
docker run -d --name sftp-server \ -p 2222:22 \ -v ~/sftp:/home \ atmoz/sftp \ user1:password:1001
- Replace
user1
andpassword
with your desired username and password. 1001
represents the user’s UID.
- Replace
-
Verify the container is running:
docker ps
Step 4: Test the SFTP Server
-
Connect to the server using an SFTP client:
sftp -P 2222 user1@<server_ip>
-
Navigate to the home directory:
cd /home/user1
-
Upload and download files to test functionality:
put testfile.txt get testfile.txt
Step 5: Configure Additional Users
-
Stop the SFTP container:
docker stop sftp-server
-
Edit the
docker run
command to add more users:docker run -d --name sftp-server \ -p 2222:22 \ -v ~/sftp:/home \ atmoz/sftp \ user1:password:1001 \ user2:password:1002
- Replace
user2
and1002
with the new user’s details.
- Replace
-
Restart the container:
docker start sftp-server
Step 6: Secure Your SFTP Server
-
Change the default password for users regularly:
- Stop the container, modify the run command, and restart.
-
Limit external access to SFTP:
- Configure your firewall to allow only specific IPs to connect to port
2222
.
- Configure your firewall to allow only specific IPs to connect to port
-
Monitor container logs for suspicious activity:
docker logs sftp-server
FAQs
Q: Can I use a different port for SFTP?
A: Yes, change -p 2222:22
to another port (e.g., -p 2223:22
).
Q: How do I add more shared directories?
A: Bind additional host directories to the container:
bash docker run -d --name sftp-server \ -p 2222:22 \ -v ~/sftp:/home \ -v ~/additional:/additional \ atmoz/sftp
Q: Is password-based authentication secure for SFTP?
A: It’s secure if you use strong passwords, but SSH key-based authentication is recommended for better security.
Q: How do I back up user data?
A: Use tar
or rsync
to back up the ~/sftp
directory:
bash tar -czvf sftp-backup.tar.gz ~/sftp
Q: Can I restrict users to their home directories?
A: Yes, the Docker image automatically restricts users to their specified home directories.
Q: How do I update the SFTP server?
A: Pull the latest image and recreate the container:
bash docker pull atmoz/sftp docker stop sftp-server docker rm sftp-server docker run -d --name sftp-server \ -p 2222:22 \ -v ~/sftp:/home \ atmoz/sftp
Q: Can I use SFTP without Docker?
A: Yes, you can set up SFTP natively with OpenSSH, but Docker simplifies deployment and management.
By deploying an SFTP server with Docker, you can create a secure and efficient file transfer system for your homelab. Enjoy fast, private, and flexible file sharing!