CentOS
This tutorial explains how to setup and use an SFTP server on CentOS.
Before I start, let me explain what actually SFTP represents and what it is
used for. Currently, most people know that we can use normal FTP for
transferring, downloading or uploading data from a server to client or client
to server. But this protocol is getting hacked easily (if TLS is not used) by
anonymous intruders as it the ports are wide open to anyone. Therefore,
SFTP has been introduced to as another alternative to meet the main
purpose to strengthen the security level.
SFTP stands for SSH File Transfer Protocol or Secure File Transfer
Protocol. It uses a separate protocol packaged with SSH to provide
a secure connection.
1. Preliminary Note
For this tutorial, I am using CentOS 7 in the 64bit version. The same steps
will work on CentOS 6 as well. The tutorial result will show how a client can
be provided with access to the SFTP server but unable to login to the server
itself by SSH.
2. SFTP Installation
Run:
3. SFTP Configuration
mkdir -p /data/sftp
chmod 701 /data
Basically what I'm trying to do with the above step is to get a separate folder
as main directory for the SFTP access. All user directories for the SFTP
users will be subdirectories of this data folder.
Let's create a group for the SFTP user, below are the steps:
groupadd sftpusers
1. I create a user and include the user into sftpusers group using -g
command.
2. I assign the main directory for the user to be in the /upload directory by
setting the -d /upload command. This means that later the /upload
folder will be under /data/mysftpuser/upload.
3. I limit the access to the /sbin/nologin shell to ensure the user is only
able to use the SFTP protocol, not SSH.
4. I name the user "mysftpuser".
5. Set password for user "mysftpuser".
Now let's create the /upload folder under /data/mysftpuser, then assign
appropriate ownership to the folder.
mkdir -p /data/mysftpuser/upload
chown -R root:sftpusers /data/mysftpuser
chown -R mysftpuser:sftpusers /data/mysftpuser/upload
Once done, verify that the new folder under the directory /data exists and
that we made the configuration correct.
Now configure the SSH protocol to create an SFTP process. This can be
done by editing the configuration file under /etc/ssh/sshd_config. Below are
the steps:
nano /etc/ssh/sshd_config
Once done restart the SSH services, below are the steps:
4. Testing SFTP
Now everything has been configured, so let's make a test to ensure the
setup meets our purpose.
I'll access SFTP by using another server called TEST01. First, I'll verify the
Port of the SFTP server . To do that I'll use the nmap function. If your client
server didn't have it you may download and install it with yum as shown
below:
Dependencies Resolved
===========================================================================
Package Arch Version Repository Size
===========================================================================
Installing:
nmap x86_64 2:6.40-7.el7 base 4.0 M
Installing for dependencies:
nmap-ncat x86_64 2:6.40-7.el7 base 201 k
Transaction Summary
===========================================================================
Install 1 Package (+1 Dependent package)
Dependency Installed:
nmap-ncat.x86_64 2:6.40-7.el7
Complete!
You'll notice that currently on our SFTP server, the only open port is SSH
22. Now, let's try to access the SFTP server (IP: 192.168.1.100 in my case)
from TEST01 client. Below are the steps:
Great! Now our SFTP server is accessible from outside. Notice that the
default directory is /upload . This means that SFTP will only show the
default path as /upload even though our previous configuration made in the
SFTP server directory is /data/mysftpuser/upload.
Now let's try to get a file from the SFTP server directory into our testing
client. First, let's create a test file under /data/mysftpuser/upload. Below are
the steps:
cd /data/mysftpuser/upload
touch testing_file.txt
Then go back to our testing site TEST01 and see if we able to get and
download the created file.
Excellent! Our SFTP test has been successful, let's try to access SSH using
the user mysftpuser. As previously, we've set configuration as /sbin/nologin,
therefore the user won't be able to use SSH services: