Keywords: centos 7 vsftpd chroot
vsFTPD stands for Very Secure File Transport Protocol Daemon and is a fast and secure (if you configure it well) FTP server for Unix/Linux systems. This guide should also work well on RHEL CentOS, Scientific Linux 7 version too.
Create a Direcotry and Group for Ftp user’s.
Before installing anything we need to create a ftp user first, assign him a directory and a Shell (or not).
lets create a Directory for our FTP user or if you want to assign user a already created directory then you can skip this step.
mkdir -p /dir/of/ftp
Create a GROUP for FTP user
Create a new FTP user.
Create a new user for FTP and assign him the DIRECTORY, SHELL and then create a password for him.
useradd -d /dir/of/ftp -s /dev/null user1 passwd user1<br>
( Its very important to not give any real shell to your FTP users. this way even if somehow a user successfully get out of ch-rooted jail, it would not have the possibility of executing any user tasks. )
Add user to the FTP user’s group. ( You can also assign group to user’s while creating them but this is better way. )
usermod -a -G user_of_ftp user1
Change the GROUP ownership of the DIRECTORY.
chgrp user_of_ftp /dir/of/ftp
SET GID to the DIRECTORY. so that all new files and directories created under /dir/of/ftp are owned by the “user_of_ftp” group
chmod g+srwx -R /dir/of/ftp
Now edit the /etc/shells file and add a fake shell /dev/null to it that will limit the system for FTP users.
vi /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin /dev/null
Next we will edit our /etc/passwd file, find the line for user user1 and replace the /dir/of/ftp with /dir/of/./ftp to divide the /dir/of directory from /ftp directory where the user user1 should be automatically transferred to.
This is a must step for every FTP user you want to add to your /etc/passwd file.
vi /etc/passwd ... ... user1:x:1002:1002::/dir/of/ftp:/dev/null
After this user’s will automatically be placed in /dir/of/ftp directory as their root directory. They wont be able to move up the file system.
Install vsFTPD package.
Now that we have a USER, GROUP and a DIRECTORY, let’s install the vsftpd package.
yum update yum install vsftpd -y
Now we can start editing our vsFTPD config file but its always a good practice to take backup of any config file before editing it.
cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf-$(date +%F).bak
Make necessary config changes.
After the backup now we can start editing the config file with text editor of your choice.
I am using VIM to edit files here, you’re free to use any editor you like.
we will make couple of changes within this configuration file.
First we will find this line anonymous_enable=YES and replace it with anonymous_enable=NO . This is the first step in ensuring security.
Also make sure these variables are set to defaults. ( usually are by default )
local_enable=YES write_enable=YES local_umask=022 dirmessage_enable=YES xferlog_enable=YES xferlog_std_format=YES connect_from_port_20=NO
( /var/log/xferlog log records downloads and uploads, which concur with the above directory listing )
Now lets configure vsftpd to be able to chroot (also known as jailing) users to their home directories or other directories. Its necessary for security (and privacy).
When user’s are in chroot, they can’t move up a level in the directory structure.
You can chroot a user in vsftpd by editing following in config file.
allow_writeable_chroot=YES chroot_local_user=YES chroot_list_enable=YES chroot_list_file=/etc/vsftpd/vsftpd_chroot.list
By default every user uses chroot but if you want to specify some users to not use chroot then you must define those users in a file (vsftpd_chroot.list)
Now lets create that file and you can leave this file blank if you don’t want to specify any user now.
Next we need to take care of listen directives.
(Always remember you can’t set both “listen” and “listen_ipv6” to “yes” at the same time.)
Configure TLS with vsFTPD.
Normal ftp connections are not encrypted which is not secure at all and should be avoided whenever possible. Encrypted connections are the need of hour. we will be using TLS instead of SSL ( TLS is more SECURE ).
First things first, we need certificates to use with our encrypted connections and a directory to store them.
so lets create a directory to store our certificates.
mkdir -p /etc/ftp/ssl/private
Now we can create certificates. It’s better to create the certificate and the key in a single file, use this command:
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout /etc/ftp/ssl/private/vsftpd.pem -out /etc/ftp/ssl/private/vsftpd.pem
Now we will make necessary changes within vsftpd.conf to configure the secure connection.
Add TLS info at the end of the file.
Next, we need enable the use of these files and disable anonymous users. We should also force the use of SSL for both data transfer and login routines. This will make the security mandatory.
For extra security we will make it mandatory to use TLS for Logins and data transfer. Add this to end of file.
ssl_enable=YES allow_anon_ssl=NO force_local_data_ssl=YES force_local_logins_ssl=YES ssl_tlsv1=YES ssl_sslv2=NO ssl_sslv3=NO require_ssl_reuse=NO ssl_ciphers=HIGH
Add the following to the end of the file if not already present in file.
pam_service_name=vsftpd userlist_enable=YES tcp_wrappers=YES allow_writeable_chroot=YES # Max Rates max_clients=10 max_per_ip=2 # Passive Mode pasv_address=139.xx.xx.xx < Your Server's IP here > pasv_enable=YES pasv_min_port=12321 pasv_max_port=12421 pasv_promiscuous=YES
passive mode connects to any random port > 1023, what we are doing here is defining a fixed port range for VSFTP to use for PASV connections and then will allow these in our firewall rules.
To set your FTP welcome Banner use this:
ftpd_banner=Welcome to xyz FTP service (Optional)
Start service and firewall configuration.
Enale the vsftpd service to make it start automatically after reboot
systemctl enable vsftpd.service
Start the vsftpd service.
systemctl start vsftpd.service
NOW make changes in firewall configuration. Allow ftp service through FIREWALL.
firewall-cmd --zone=public --add-service=ftp --permanent firewall-cmd --zone=public --add-port=21/tcp --permanent firewall-cmd --zone=public --add-port=12321-12421/tcp --permanent firewall-cmd --reload
LAST BUT NOT LEAST CONFIGURE SELINUX.
configure SELinux to allow ftp access to the users home directories.
setsebool -P ftp_home_dir on
( PAY EXTRA ATTENTION TO SYNTAX IN VSFTPD.CONF )
Here’s sample config file just to make things simple.
# Example config file /etc/vsftpd/vsftpd.conf # # The default compiled in settings are fairly paranoid. This sample file # loosens things up a bit, to make the ftp daemon more usable. # Please see vsftpd.conf.5 for all compiled in defaults. # # READ THIS: This example file is NOT an exhaustive list of vsftpd options. # Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's # capabilities. # # Allow anonymous FTP? (Beware - allowed by default if you comment this out). anonymous_enable=NO # # Uncomment this to allow local users to log in. # When SELinux is enforcing check for SE bool ftp_home_dir local_enable=YES # # Uncomment this to enable any form of FTP write command. write_enable=YES # # Default umask for local users is 077. You may wish to change this to 022, # if your users expect that (022 is used by most other ftpd's) local_umask=022 # # Uncomment this to allow the anonymous FTP user to upload files. This only # has an effect if the above global write enable is activated. Also, you will # obviously need to create a directory writable by the FTP user. # When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access #anon_upload_enable=YES # # Uncomment this if you want the anonymous FTP user to be able to create # new directories. #anon_mkdir_write_enable=YES # # Activate directory messages - messages given to remote users when they # go into a certain directory. dirmessage_enable=YES # # Activate logging of uploads/downloads. xferlog_enable=YES # # Make sure PORT transfer connections originate from port 20 (ftp-data). connect_from_port_20=NO # # If you want, you can arrange for uploaded anonymous files to be owned by # a different user. Note! Using "root" for uploaded files is not # recommended! #chown_uploads=YES #chown_username=whoever # # You may override where the log file goes if you like. The default is shown # below. xferlog_file=/var/log/xferlog # # If you want, you can have your log file in standard ftpd xferlog format. # Note that the default log file location is /var/log/xferlog in this case. xferlog_std_format=YES # # You may change the default value for timing out an idle session. #idle_session_timeout=600 # # You may change the default value for timing out a data connection. #data_connection_timeout=120 # # It is recommended that you define on your system a unique user which the # ftp server can use as a totally isolated and unprivileged user. #nopriv_user=ftpsecure # # Enable this and the server will recognise asynchronous ABOR requests. Not # recommended for security (the code is non-trivial). Not enabling it, # however, may confuse older FTP clients. #async_abor_enable=YES # # By default the server will pretend to allow ASCII mode but in fact ignore # the request. Turn on the below options to have the server actually do ASCII # mangling on files when in ASCII mode. # Beware that on some FTP servers, ASCII support allows a denial of service # attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd # predicted this attack and has always been safe, reporting the size of the # raw file. # ASCII mangling is a horrible feature of the protocol. #ascii_upload_enable=YES #ascii_download_enable=YES # # You may fully customise the login banner string: #ftpd_banner=Welcome to blah FTP service. # # You may specify a file of disallowed anonymous e-mail addresses. Apparently # useful for combatting certain DoS attacks. #deny_email_enable=YES # (default follows) #banned_email_file=/etc/vsftpd/banned_emails # # You may specify an explicit list of local users to chroot() to their home # directory. If chroot_local_user is YES, then this list becomes a list of # users to NOT chroot(). # (Warning! chroot'ing can be very dangerous. If using chroot, make sure that # the user does not have write access to the top level directory within the # chroot) chroot_local_user=YES chroot_list_enable=YES # (default follows) chroot_list_file=/etc/vsftpd/chroot.list # # You may activate the "-R" option to the builtin ls. This is disabled by # default to avoid remote users being able to cause excessive I/O on large # sites. However, some broken FTP clients such as "ncftp" and "mirror" assume # the presence of the "-R" option, so there is a strong case for enabling it. #ls_recurse_enable=YES # # When "listen" directive is enabled, vsftpd runs in standalone mode and # listens on IPv4 sockets. This directive cannot be used in conjunction # with the listen_ipv6 directive. listen=YES listen_port=21 # # This directive enables listening on IPv6 sockets. By default, listening # on the IPv6 "any" address (::) will accept connections from both IPv6 # and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6 # sockets. If you want that (perhaps because you want to listen on specific # addresses) then you must run two copies of vsftpd with two configuration # files. # Make sure, that one of the listen options is commented !! listen_ipv6=NO pam_service_name=vsftpd userlist_enable=YES tcp_wrappers=YES rsa_cert_file=/etc/ftp/ssl/private/vsftpd.pem rsa_private_key_file=/etc/ftp/ssl/private/vsftpd.pem ssl_enable=YES allow_anon_ssl=NO force_local_data_ssl=YES force_local_logins_ssl=YES ssl_tlsv1=YES ssl_sslv2=NO ssl_sslv3=NO require_ssl_reuse=NO ssl_ciphers=HIGH allow_writeable_chroot=YES # Max Rates max_clients=10 max_per_ip=2 # Passive Mode pasv_address=201.xx.xx.xx pasv_enable=YES pasv_min_port=33000 pasv_max_port=33100 pasv_promiscuous=YES
Now your vsftpd server is ready and running.
I have tried to cover all the basic to advance concepts with their examples. Still if I have missed anything please update us through comment box. I will keep updating the same based on feedback’s received.????