I've been itching to set up an FTP server on my CentOS VPS, so that whenever one of my friend asks for some files, I have a quick way of meeting their needs. I ended up setting up a vsftpd server. As inspiration I used this great tutorial from digitalocean. However the problem with it is that it was set up for a separate directory for each user. Not what I wanted. So I dived deep.
First things first - install the FTP server. It's a lot easier to set this whole thing up if you become root.
dnf install vsftpd
Next the firewall. Don't ever forget the firewall. 😂 Now here, you have some choices to make. Do you want to expose port 21, so that every bot on the planet will try and violate your port or you choose a custom one. I went with the latter. You will need a port for FTP commands (default 21) and a port range for actual data transfer in passive mode (server decides the port). If confused, click.
The default firewall (called firewalld) on RedHat and the like ... really whips the llama's ass. Not only that the control commands make a lot of sense, but you can create your own services to easily add to the rules. Which we are going to do now, since we are here. To set up a service definition for firewalld all you have to do is edit an
*.xml in a certain location.
Add the following lines and save.
<description>VSFTPD custom port settings</description>
<port port="33021" protocol="tcp"/>
<port port="33030-33050" protocol="tcp"/>
All you have to do afterwards is to reload the firewall so it recognizes the freshly defined service, add the service as permanent and reload firewalld again.
firewall-cmd --add-service=vsftpd-custom --permanent
Running the last command should reveal all your active firewall entries and you should see VSFTPD-custom among services. I think it's nice that definitions have a name so you don't have to remember the reason for each of the 4937 ports you opened.
Okay, now with the firewall out of the way, let's get on with the FTP server. First we back up the default configuration (just in case) and create our own.
mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bkp
Next thing, paste the below configuration. The default has some pretty clear comments and I also added my own, so I won't repeat myself here.
# Config guide: https://web.mit.edu/rhel-doc/5/RHEL-5-manual/Deployment_Guide-en-US/s1-ftp-vsftpd-conf.html
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
# Uncomment this to allow local users to log in.
# Uncomment this to enable any form of FTP write command.
# 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)
#022=755; 002=775 (to calculate 777-what you need)
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
# Activate logging of uploads/downloads.
# Make sure PORT transfer connections originate from port 20 (ftp-data).
# You may override where the log file goes if you like. The default is shown
# 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.
# You may change the default value for timing out an idle session.
# You may change the default value for timing out a data connection.
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
# 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.
# with ipv6 it listens also to ipv4
# Port for FTP server to listen on; comment to use default 21
# Only allow users on list
# However, userlist_deny=NO alters the setting, meaning that only users explicitly listed in userlist_file=/etc/vsftpd/vsftpd.userlist will be permitted to login.
# (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
# Jail ftp users to ftp root
# writeable local root
# define local root
# Ports used for PASsiVe mode
#for users to have ftp go to their home dir
# Securing with TLS/SSL (uncomment all bellow)
As you can see I also left the settings for running the server secured with TLS in and also the settings for running the server with the FTP root defaulting to the user's home folder. Lines after
pasv_max_port are safe to delete if you don't care. To securing with TLS I'll get back later.
Back to our setup. Paranoia sets in and I create a user that I specially use only for FTP. The digitalocean article had a nice tip on doing this by defining a custom shell for FTP users. Here is how to set it up:
echo "This account is limited to FTP access only."
Make it xecutable:
chmod a+x /bin/ftponly
/etc/shells and add
/bin/ftponly at the end.
Now let's create the user, add him to the FTP group (your choice) and give him no shell:
useradd -g ftp -s /bin/ftponly ftpuser
After creating the user, we have to add it to the file referenced in
vsftpd.conf. Only users included in this file will be able to access the server.
echo "ftpuser" | tee -a /etc/vsftpd/vsftpd.userlist
In order to allow people and the recently created user to upload files to our FTP server, we need to define a writable folder, because the way we secured our local root ftp directory, nobody is able to write it. And that is the way it should be.
chmod a-w /var/ftp/pub
chown ftpuser:ftp /var/ftp/pub/uploads
chmod g+w /var/ftp/pub/uploads
The only thing that is left to do is to start the vsftpd service and then enable it so it starts automatically when the server reboots.
systemctl start vsftpd
systemctl enable vsftpd
After this is done you should be able to access your own FTP server and put files in the uploads folder. Great success 😎.
Giving access to your FTP to a friend is easy as:
useradd -g ftp -s /bin/ftponly newftpuser
echo "newftpuser" | tee -a /etc/vsftpd/vsftpd.userlist
A note on SELinux: this is a topic that I am no expert in, the only way I got my server working correctly was by modifying it like this - maybe you know a more secure way:
setsebool ftpd_full_access on
Optional: If you want to secure your server with TLS, you have to generate certificates (these will be valid for 1 year if you don't change the
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/pki/tls/private/vsftpd.pem -out /etc/pki/tls/private/vsftpd.pem
With the certificates generated, it's time to comment out the related lines in
vsftpd.conf and restart the FTP server. You should be good to go!
And one more thing ...
Keep away from people who try to belittle your ambitions. Small people always do that, but the really great make you feel that you, too, can become great. - Twain