FTP stands for File Transfer Protocol. It was written by Abhay Bhushan and published in 1971. FTP is supported by all the operating systems and browsers.

It is a client-server based protocol.

How FTP works

Step a: Client connects to server on port 21.

Step b: Server responds and ask for authentication.

Step c: Client decides weather to connect passively or actively and authenticate with credentials(user name password).

Step d: If it is an active connection, server opens port 20 for data transfer and gives ftp prompt after successful authentication.

Step e: Client call for file and server initiates file transfer.

Following picture shows a simple way of data transfer through ftp.Data and command portBefore setting up FTP server we have to clear our self about active and passive ftp

Why FTP uses two ports

As we already discussed FTP uses 21 port for control and 20 for data transfer, this is because of many reasons such as

  1. Separate data transfer path so that you can still use control port do some communication with server.
  2. Can initiate multiple data connections without control interruptions.
  3. Server decides when to send data which will minimize any increase of load on server.

Difference between an Active FTP and Passive FTP server

FTP is a tricky protocol which uses two ports one as command port(21) and other as data port(For active FTP it is 20 and for passive port it is grater then 10000). So it boils down to which port is used for data transfer.

We will set-up and configure ftp server in CentOS 6.7. This procedure is same for all Redhat based distributions like Centos, Fedora, Scientific Linux, Oracle Linux etc.

Installing FTP server in Centos

Step 1: We will use below host name and IP address for our test machine to setup FTP server

Server IP: 192.168.0.9

Host Name: ftp.linuxnix.com

Just edit file /etc/hosts

#vi /etc/hosts

and add the line on bottom and save

192.168.0.9  ftp.linuxnix.com

Step 2: Install vsftpd (very secure FTP daemon) package.

#yum install vsftpd ftp

Configuring FTP server in Linux Centos

Step 3: Configure vsftpd package. We will edit /etc/vsftpd/vsftpd.conf you can do this with gedit (If installed) or vi command.

#vi /etc/vsftpd/vsftpd.conf

Change the line which contain anonymous_enable=YES to anonymous_enable=NO. This will permit any one to access FTP server with authentication.

anonymous_enable=YES   

Uncomment the following line

local_enable=YES                                    allow users in /etc/passwd to login

write_enable=YES                                   allow users to write files. “NO” will permit only to read.

Change the line chroot_local_user=NO to chroot_local_user=YES. This will permit local user as FTP account. If you add an user, it will be treated as a FTP account as well.

The local user directory will be the FTP directory.

chroot_local_user=YES 

Save the file.

Step 4: Permit Home user to FTP account

Permit FTP account directory as user home directory.

#setsebool ftp_home_dir on

Step 5: Open firewall or IP Table update so that our FTP server is accessed through 21 port.

We can do this with one of the two ways.

a) First Way: Edit the file /etc/sysconfig/iptables and add the line (Like the picture)

-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT

ftp iptables

than

#service iptables restart

b) or Second way: Through setup command.

#setup

Than the screen will come as shown below.
Firewall permission for FTP
Select FTP
Firewall permission of FTP server
Save

Firewall permission of FTP server

Actually this will add the above line to iptables file.

Step 6: Start FTP service once you do all the above edit’s.

#service vsftpd start

To enable this service at boot time, you have to execute below chkconfig command.

#chkconfig vsftpd on

Step 7: Now the FTP server is live. We can check with ftp command. Just create a test account to do the testing.

#useradd linuxnix
#passwd linuxnix
Changing password for user rejaul.
New password:                                    <Enter Password for user>

Now we will login to ftp

ftp ftp.linuxnix.com
# ftp localhost
 Trying ::1...
 ftp: connect to address ::1Connection refused
 Trying 127.0.0.1...
 Connected to localhost (127.0.0.1).
 220 (vsFTPd 2.2.2)
 Name (localhost:root): lftp ftp.linuxnix.com
 331 Please specify the password.
 Password:                                            <Enter Password for user>
 230 Login successful.
 Remote system type is UNIX.
 Using binary mode to transfer files.

Now FTP server is ready and you do multiple file transfers from FTP as well. You can also browse your ftp server from web browser after typing below line in address bar.

ftp://ftp.linuxnix.com

User Name and password will be asked. Than you will find the file and directory tree.

You can learn interview question about FTP and also TFTP implementation in our the previous post.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.