Setup FTP Server on Linux (Cent OS) and automate the file upload using a shell script

FTP Stands for File Transfer Protocol which is standard client-server protocol method for transferring files between a server and clients over a network.

The following are the several open source Linux FTP servers

Here are the steps to install vsftpd (Very Secure Ftp Daemon) on a Linux (Cent OS) server.

INstalling FTP Server

Login to the Linux server

Install vsftpd using below yum command

yum install -y vsftpd

Enable the service to start at boot

systemctl enable vsftpd

Start the service for the first time

systemctl start vsftpd

Configuring FTP Service (VSFTPD)

Take the backup of original file

cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.default

Use vi or nano or any editor of your choice update the below parameters in the config file

anonymous_enable=NO     # disable anonymous login
local_enable=YES        # permit local logins
write_enable=YES        # enable FTP commands which change the file system
userlist_enable=YES     # enable vsftpd to load a list of usernames
userlist_file=/etc/vsftpd/user_list   # stores usernames.
userlist_deny=NO        # option lets you specify users to be blocked

Restart the vsftpd to effect all the changes

systemctl restart vsftpd

Add FTP User

sudo adduser ftpuser
sudo passwd ftpuser
echo "ftpuser" | sudo tee -a /etc/vsftpd/user_list

Connect to FTP from other server

Make sure ftp package is installed from the client system from where you want to connect to the ftp server. If ftp package is not there, you can install using yum command as below.

yum install -y ftp

Connect to ftp server and list files

Connect to FTP server and list files

Download files from FTP

Upload files to FTP

Shell script to upload the files to FTP automatically

Below is shell script to automatically upload files to ftp whenever this script is executed.

  • ftp host name or ip address, username and password as variables
  • Connect to ftp
  • Go to /opt/ folder in client machine and upload all the files txt files to the server.
    • mput command is to upload multiples where as put command is to upload single file.
!/bin/bash
HOST=demo.server.com
USER=ftpuser
PASSWORD=admin@123
ftp -inv $HOST <<EOF
user $USER $PASSWORD
lcd /opt/
mput *.txt
bye
EOF

Save the file as ftpupload.sh file and use below commands to execute the shell script to upload the txt files to ftp server.

chmod +x ftpupload.sh
sh ftpupload.sh
0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like