How To Set Up VSFTPD For A User's Directory On Ubuntu 16.04 - DigitalOcean
How To Set Up VSFTPD For A User's Directory On Ubuntu 16.04 - DigitalOcean
FTP, short for File Transfer Protocol, is a network protocol that was once
widely used for moving files between a client and server. It has since been
replaced by faster, more secure, and more convenient ways of delivering
files. Many casual Internet users expect to download directly from their web
browser with https, and command-line users are more likely to use secure
protocols such as the scp or sFTP.
FTP is still used to support legacy applications and workflows with very
specific needs. If you have a choice of what protocol to use, consider
exploring the more modern options. When you do need FTP, however,
vsftpd is an excellent choice. Optimized for security, performance, and
stability, vsftpd offers strong protection against many security problems
found in other FTP servers and is the default for many Linux distributions.
In this tutorial, we'll show you how to configure vsftpd to allow a user to
upload files to his or her home directory using FTP with login credentials
secured by SSL/TLS.
Prerequisites
To follow along with this tutorial you will need:
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 1 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
When the installation is complete, we'll copy the configuration file so we can
start with a blank configuration, saving the original as a backup.
Output
Status: active
To Action From
-- ------ ----
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 2 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
You may have other rules in place or no firewall rules at all. Since only ssh
traffic is permitted in this case, weʼll need to add rules for FTP traffic.
We'll need to open ports 20 and 21 for FTP, port 990 for later when we
enable TLS, and ports 40000-50000 for the range of passive ports we plan
to set in the configuration file:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
990/tcp ALLOW Anywhere
20/tcp ALLOW Anywhere
21/tcp ALLOW Anywhere
40000:50000/tcp ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
20/tcp (v6) ALLOW Anywhere (v6)
21/tcp (v6) ALLOW Anywhere (v6)
990/tcp (v6) ALLOW Anywhere (v6)
40000:50000/tcp (v6) ALLOW Anywhere (v6)
With vsftpd installed and the necessary ports open, we're ready to proceed
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 3 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
Assign a password when prompted and feel free to press "ENTER" through
the other prompts.
In this example, rather than removing write privileges from the home
directory, we're will create an ftp directory to serve as the chroot and a
writable files directory to hold the actual files.
Create the ftp folder, set its ownership, and be sure to remove write
permissions with the following commands:
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 4 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
Output
total 8
4 dr-xr-xr-x 2 nobody nogroup 4096 Aug 24 21:29 .
4 drwxr-xr-x 3 sammy sammy 4096 Aug 24 21:29 ..
Next, we'll create the directory where files can be uploaded and assign
ownership to the user:
Output
total 12
dr-xr-xr-x 3 nobody nogroup 4096 Aug 26 14:01 .
drwxr-xr-x 3 sammy sammy 4096 Aug 26 13:59 ..
drwxr-xr-x 2 sammy sammy 4096 Aug 26 14:01 files
Finally, we'll add a test.txt file to use when we test later on:
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 5 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
Now that we've secured the ftp directory and allowed the user access to
the files directory, we'll turn our attention to configuration.
/etc/vsftpd.conf
. . .
# Allow anonymous FTP? (Disabled by default).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
local_enable=YES
. . .
Next we'll need to change some values in the file. In order to allow the user
to upload files, weʼll uncomment the write_enable setting so that we have:
/etc/vsftpd.conf
. . .
write_enable=YES
. . .
Weʼll also uncomment the chroot to prevent the FTP-connected user from
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 6 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
/etc/vsftpd.conf
. . .
chroot_local_user=YES
. . .
/etc/vsftpd.conf
user_sub_token=$USER
local_root=/home/$USER/ftp
We'll limit the range of ports that can be used for passive FTP to make sure
enough connections are available:
/etc/vsftpd.conf
pasv_min_port=40000
pasv_max_port=50000
Note: We pre-opened the ports that we set here for the passive port range.
If you change the values, be sure to update your firewall settings.
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 7 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
/etc/vsftpd.conf
userlist_enable=YES
userlist_file=/etc/vsftpd.userlist
userlist_deny=NO
userlist_deny toggles the logic. When it is set to "YES", users on the list
are denied FTP access. When it is set to "NO", only users on the list are
allowed access. When you're done making the change, save and exit the
file.
Finally, weʼll create and add our user to the file. We'll use the -a flag to
append to file:
cat /etc/vsftpd.userlist
Output
sammy
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 8 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
We've configured the server to allow only the user sammy to connect via FTP.
Let's make sure that's the case.
ftp -p 203.0.113.0
Output
Connected to 203.0.113.0.
220 (vsFTPd 3.0.3)
Name (203.0.113.0:default): anonymous
530 Permission denied.
ftp: Login failed.
ftp>
bye
Users other than sammy should fail to connect: Next, we'll try connecting
as our sudo user. They, too, should be denied access, and it should happen
before they're allowed to enter their password.
ftp -p 203.0.113.0
Output
Connected to 203.0.113.0.
220 (vsFTPd 3.0.3)
Name (203.0.113.0:default): sudo_user
530 Permission denied.
ftp: Login failed.
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 9 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
ftp>
bye
sammy should be able to connect, as well as read and write files: Here,
we'll make sure that our designated user canconnect:
ftp -p 203.0.113.0
Output
Connected to 203.0.113.0.
220 (vsFTPd 3.0.3)
Name (203.0.113.0:default): sammy
331 Please specify the password.
Password: your_user's_password
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
We'll change into the files directory, then use the get command to transfer
the test file we created earlier to our local machine:
cd files
get test.txt
Output
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 10 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
We'll turn right back around and try to upload the file with a new name to
test write permissions:
Output
bye
Now that we've tested our configuration, we'll take steps to further secure
our server.
We'll use openssl to create a new certificate and use the -days flag to make
it valid for one year. In the same command, we'll add a private 2048-bit RSA
key. Then by setting both the -keyout and -out flags to the same value, the
private key and the certificate will be located in the same file.
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 11 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/privat
Output
For more detailed information about the certificate flags, see OpenSSL
Essentials: Working with SSL Certificates, Private Keys and CSRs
Once you've created the certificates, open the vsftpd configuration file
again:
Toward the bottom of the file, you should two lines that begin with rsa_.
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 12 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
/etc/vsftpd.conf
# rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
# rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
Below them, add the following lines which point to the certificate and
private key we just created:
/etc/vsftpd.conf
rsa_cert_file=/etc/ssl/private/vsftpd.pem
rsa_private_key_file=/etc/ssl/private/vsftpd.pem
After that, we will force the use of SSL, which will prevent clients that can't
deal with TLS from connecting. This is necessary in order to ensure all
traffic is encrypted but may force your FTP user to change clients. Change
ssl_enable to YES:
/etc/vsftpd.conf
ssl_enable=YES
After that, add the following lines to explicitly deny anonymous connections
over SSL and to require SSL for both data transfer and logins:
/etc/vsftpd.conf
allow_anon_ssl=NO
force_local_data_ssl=YES
force_local_logins_ssl=YES
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 13 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
After this we'll configure the server to use TLS, the preferred successor to
SSL by adding the following lines:
/etc/vsftpd.conf
ssl_tlsv1=YES
ssl_sslv2=NO
ssl_sslv3=NO
Finally, we will add two more options. First, we will not require SSL reuse
because it can break many FTP clients. We will require "high" encryption
cipher suites, which currently means key lengths equal to or greater than
128 bits:
/etc/vsftpd.conf
require_ssl_reuse=NO
ssl_ciphers=HIGH
Now, we need to restart the server for the changes to take effect:
ftp -p 203.0.113.0
Connected to 203.0.113.0.
220 (vsFTPd 3.0.3)
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 14 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
Next, we'll verify that we can connect using a client that supports TLS.
When you first open FileZilla, find the Site Manager icon just below the word
File, the left-most icon on the top row. Click it:
A new window will open. Click the "New Site" button in the bottom right
corner:
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 15 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
Under "My Sites" a new icon with the words "New site" will appear. You can
name it now or return later and use the Rename button.
You must fill out the "Host" field with the name or IP address. Under the
"Encryption" drop down menu, select "Require explicit FTP over TLS".
For "Logon Type", select "Ask for password". Fill in the FTP user you created
in the "User" field:
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 16 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
Click "Connect" at the bottom of the interface. You will be asked for the
user's password:
Click "OK" to connect. You should now be connected with your server with
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 17 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
TLS/SSL encryption.
When youʼve accepted the certificate, double-click the files folder and
drag upload.txt to the left to confirm that youʼre able to download files.
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 18 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
When youʼve done that, right-click on the local copy, rename it to upload-
tls.txt` and drag it back to the server to confirm that you can upload files.
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 19 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
Youʼve now confirmed that you can securely and successfully transfer files
with SSL/TLS enabled.
We'll add a message telling the user why they are unable to log in. Paste in
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 20 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
the following:
#!/bin/sh
echo "This account is limited to FTP access only."
/etc/shells
. . .
/bin/ftponly
Output
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 21 of 22
How To Set Up vsftpd for a User's Directory on Ubuntu 16.04 | DigitalOcean 7/12/19, 12(36
This confirms that the user can no longer ssh to the server and is limited to
FTP access only.
Conclusion
In this tutorial we covered setting up FTP for users with a local account. If
you need to use an external authentication source, you might want to look
into vsftpd's support of virtual users. This offers a rich set of options
through the use of PAM, the Pluggable Authentication Modules, and is a
good choice if you manage users in another system such as LDAP or
Kerberos.
https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-16-04 Page 22 of 22