How to Set Up FTP on the Raspberry Pi

Ben
Ben
@benjislab

File Transfer Protocol (FTP) provides a convenient way to transfer files between your Raspberry Pi and other devices on your network. This guide will walk you through the process of setting up an FTP server on your Raspberry Pi using vsftpd (Very Secure FTP Daemon).

Prerequisites

  • A Raspberry Pi running Raspberry Pi OS (formerly Raspbian)
  • The Pi connected to your local network
  • SSH access to your Pi or direct access via keyboard and monitor
  • Basic knowledge of Linux commands

Step 1: Update Your System

Before installing any new software, it's good practice to update your system:

sudo apt update
sudo apt upgrade -y

Step 2: Install vsftpd

vsftpd is a lightweight, stable, and secure FTP server for Linux systems:

sudo apt install vsftpd -y

Step 3: Configure vsftpd

After installation, you'll need to configure the FTP server by editing its configuration file:

sudo nano /etc/vsftpd.conf

Here are the recommended settings to enable or uncomment in this file:

# Allow local users to log in
local_enable=YES

# Enable write permissions
write_enable=YES

# Default umask for local users
local_umask=022

# Chroot users to their home directories for security
chroot_local_user=YES

# Allow the use of the PASV method
pasv_enable=YES
pasv_min_port=10000
pasv_max_port=10100

# Enable logging
xferlog_enable=YES
xferlog_file=/var/log/vsftpd.log
xferlog_std_format=YES

# Set the welcome message (optional)
ftpd_banner=Welcome to my Raspberry Pi FTP service.

Press Ctrl+X to exit, then Y to save, and Enter to confirm.

Step 4: Create a Dedicated FTP User (Optional)

For better security, you can create a dedicated FTP user instead of using the default 'pi' user:

sudo adduser ftpuser
sudo mkdir /home/ftpuser/ftp
sudo chown nobody:nogroup /home/ftpuser/ftp
sudo chmod a-w /home/ftpuser/ftp
sudo mkdir /home/ftpuser/ftp/files
sudo chown ftpuser:ftpuser /home/ftpuser/ftp/files

Step 5: Configure User Access

If you want to restrict the FTP user to only access a specific directory, add these lines to the end of the vsftpd.conf file:

sudo nano /etc/vsftpd.conf

Add:

user_sub_token=$USER
local_root=/home/$USER/ftp

If you're using the dedicated FTP user approach, you may also want to add:

# Limit users to their home directories
allow_writeable_chroot=YES

Step 6: Restart the FTP Service

Apply your configuration changes by restarting the vsftpd service:

sudo systemctl restart vsftpd

Make sure the service is enabled to start at boot:

sudo systemctl enable vsftpd

Step 7: Configure Firewall (If Enabled)

If you have a firewall enabled, you'll need to allow FTP traffic:

sudo ufw allow 20/tcp
sudo ufw allow 21/tcp
sudo ufw allow 10000:10100/tcp

Step 8: Test Your FTP Connection

Now you can test your FTP server using any FTP client. You'll need:

  • Your Raspberry Pi's IP address
  • Your username and password
  • Port 21 (the default FTP port)

To find your Pi's IP address, use:

hostname -I

Using Your FTP Server

From a Linux or macOS Terminal

ftp <your-pi-ip-address>

From Windows

You can use File Explorer by entering the following in the address bar:

ftp://<username>@<your-pi-ip-address>

Or use a dedicated FTP client like FileZilla, WinSCP, or CyberDuck.

Securing Your FTP Server

For better security, consider these additional steps:

  1. Use SFTP Instead: Consider using SFTP (SSH File Transfer Protocol) which is more secure as it uses SSH encryption:

    sudo apt install openssh-server -y
    
  2. Set Up FTP over TLS/SSL: Configure vsftpd to use TLS/SSL encryption:

    ssl_enable=YES
    rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
    rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
    allow_anon_ssl=NO
    force_local_data_ssl=YES
    force_local_logins_ssl=YES
    ssl_tlsv1=YES
    ssl_sslv2=NO
    ssl_sslv3=NO
    
  3. Restrict to Specific IP Addresses: Limit connections to specific IP addresses by using TCP wrappers.

Troubleshooting

Connection Issues

  • Check if the service is running: sudo systemctl status vsftpd
  • Verify network connectivity: ping <your-pi-ip-address>
  • Check firewall settings: sudo ufw status

Permission Problems

If you're having issues with file permissions:

sudo chmod 777 /home/ftpuser/ftp/files

Note: Using 777 permissions should be temporary for testing only.

500 OOPS Error

If you get a "500 OOPS" error, it might be due to the writeable chroot issue. Add:

allow_writeable_chroot=YES

to your vsftpd.conf file.

Conclusion

You now have a functioning FTP server on your Raspberry Pi, allowing you to easily transfer files to and from the device. For personal or home use, this setup is sufficient, but for production environments, consider implementing additional security measures like FTP over TLS/SSL or switching to SFTP entirely.

Happy file transferring!