TFTP & FTP Server on Centos 7
If you ever needed a TFTP or an anonymous FTP server to transfer files, logs, or crash debugs to and from your network devices it can be a little tricky if you don't have anything setup. There are some free quick programs out there if you are in a pinch for one-time transfers but if you ever wanted to have something in infrastructure that is ready to go for this kind of stuff just follow this tutorial below.
I'm using the latest version of Centos 7 minimal, we need to add some house keeping items first so let's get started! Install NANO ( I like it, don't judge me!):
yum install nano
yum install firewalld
systemctl enable firewalld
systemctl start firewalld
Add the TFTP rule to the Centos Firewall and reload it:
firewall-cmd --permanent --zone=public --add-service=tftp
firewall-cmd --reload
yum install xinetd tftp-server tftp
systemctl enable xinetd tftp
systemctl start xinetd tftp
useradd -s /bin/false -r tftp
mkdir /var/FileServerRoot
mkdir /var/FileServerRoot/TFTP
chown tftp:tftp /var/FileServerRoot/TFTP/
nano /etc/xinetd.d/tftp
# default: off
# description: The tftp server serves files using the trivial file transfer \
# protocol. The tftp protocol is often used to boot diskless \
# workstations, download configuration files to network-aware printers, \
# and to start the installation process for some operating systems.
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -c -s /var/FileServerRoot/TFTP -v -v -v -u tftp -p
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
systemctl restart xinetd tftp
chcon -t tftpdir\_rw\_t /var/FileServerRoot/TFTP
Install FTP Server on Centos 7
Alight let's install the FTP server on Centos and include the FTP client just in case we want to test locally:
yum install vsftpd ftp
firewall-cmd --permanent --zone=public --add-service=ftp
firewall-cmd --reload
systemctl enable vsftpd
mkdir /var/FileServerRoot
mkdir /var/FileServerRoot/FTP
chmod 555 /var/FileServerRoot/FTP\
mkdir /var/FileServerRoot/FTP/upload
mkdir /var/FileServerRoot/FTP/download
chown ftp:ftp /var/FileServerRoot/FTP/upload
chown ftp:ftp /var/FileServerRoot/FTP/download
cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.orig
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=NO
#
# 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
#
# Point users at the Public directory
anon_root=/var/FileServerRoot/FTP/
#
# Stop prompting for a password on the command line.
no_anon_password=YES
#
# Show the user and group as ftp:ftp, regardless of the owner.
hide_ids=YES
#
# Limit the range of ports that can be used for passive FTP
pasv_min_port=40000
pasv_max_port=50000
yum install policycoreutils-python
semanage fcontext -a -t public\_content\_t "/var/FileServerRoot/FTP(/.\*)?"
restorecon -R -v /var/FileServerRoot/FTP
restorecon reset /var/FileServerRoot/FTP context unconfined_u:object_r:var_t:s0->unconfined_u:object_r:public_content_t:s0
restorecon reset /var/FileServerRoot/FTP/upload context unconfined_u:object_r:var_t:s0->unconfined_u:object_r:public_content_t:s0
restorecon reset /var/FileServerRoot/FTP/download context unconfined_u:object_r:var_t:s0->unconfined_u:object_r:public_content_t:s0
semanage fcontext -a -t public\_content\_rw\_t "/var/FileServerRoot/FTP/upload(/.\*)?"
restorecon -R -v /var/FileServerRoot/FTP/upload
restorecon reset /var/FileServerRoot/FTP/upload context unconfined_u:object_r:public_content_t:s0->unconfined_u:object_r:public_content_rw_t:s0
semanage boolean -m --on allow\_ftpd\_anon\_write\
shutdown -r now
- Check SELinux, if you are getting file permissions errors when uploading or downloading and permissions look this is likely SELinuix. It it can be a little picky which is why you'll read a lot of people just disable it. You can disable SELinux Temporarily (Turns back on when you reboot) to check if TFTP or FTP works by running
setenforce 0
- If you are having problems with anonymous FTP, like downloading or uploading check security context by running:
ls -Z
- For FTP to work the context output should like something below:
Keep in mind we just created a system that is open to any TFTP and FTP connection. Reads and writes are anonymous so if there is a need to control what/who can access your server over your network/internet its recommended to in put ACLs in place to block unwanted connections. Otherwise you might be on list like this one, in which the entire IPv4 address space was scanned to find what responds anonymously on TCP port 21. That's all I got for now, as always I hope this information helpful!
[root@localhost ~]# ls -Z /var/FileServerRoot/FTP/ drwxr-xr-x. ftp ftp unconfined_u:object_r:public_content_t:s0 download drwxr-xr-x. ftp ftp unconfined_u:object_r:public_content_rw_t:s0 upload [root@localhost ~]#