Comments on How to setup an SFTP server on 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. 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.

30 Comment(s)

Add comment

Please register in our forum first to comment.

Comments

By: Chandrakumar Muthaiah

Looks great but you need to change this line from

chown -R shahrilk:sftpusers /data/shahrilk

to

chown -R root:root /data/shahrilk

as it is needed for the chroot to work or sshd will produce permission denied.

 

By: shahril bin kamaruzzaman

Nice heads up! 

By: Jere

I did it, but it still produce  Permission denied, please try again.

By: greenears

Hey thanks for your tutorial , but I have a problem. When I try to test it using the sftp user@sftpserver it gives me this error ' Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). Couldnt read packet: Connection reset by peer '

Maybe something with the ssh_config file ?

Thanks

By: JJ

WoW... Thanks for explaination. i will keep to visit this website to learn...

By: ws

Nice write up!!!  Is it possible to add additional configurations such as block IP addresses after X numbers of incorrect login?

Thanks in advance.

By: Paul

Thanks for your article. I was able to set it up.

 

For CentOS7 (my environment) -- I will agree with Chandrakumar's comment that the ownership of /data/shahrilk is not correct in  your article. 

The path /data/shahrilk must be root owned and writable only by root.

In your article, I would simply remove/skip the line:

chown -R shahrilk:sftpusers /data/shahrilk

 

Also, it will be useful to include a tip to look at /var/log/secure if readers are having trouble getting going. This will show connection attempts and any errors with authentication or setup.

Further, on CentOS 7, the line in /etc/ssh/sshd_config already exists, with no Match rules.... and show up slighly differently as:

Subsystem sftp internal-sftp    ? on CentOS 7, this appear as: /usr/libexec/openssh/sftp-server

And so you only need to add the rules at the end of the file, namely (only these lines)

Match Group sftpusersChrootDirectory /data/%uForceCommand internal-sftp

By: Gatsu

Merci beaucoup mon ami. Tu es trés bon

By: Robert

Hi,

Thanks for this tutorial. It's great and it worked for me. I installed SFTP server and it already works.But I still have one question...

What is the purpose of /data/sftp directory if all users upload their files to /data/%u/upload and /data/sftp is hidden from them?Why do we need it?

By: Robert

Hi,

Is this a mistake

useradd -g sftpusers -d /upload -s /sbin/nologin mysftpuser

Should it be

useradd -g sftpusers -d /data/myftpuser/upload -s /sbin/nologin mysftpuser

Because when I made the second user I got an error that "home directory already exist".

By: Nishant Kashyap

Were you able to resolve this issue as I am facing a similar issue when I try to create a second user? 

By: Stefan

useradd -M -g sftpusers -d /upload -s /sbin/nologin mysftpuser

Add -M to not try to create "fake" directory

 

By: Pete Hope

Thanks for the tutorial. It seems there's an omission (at least on my system).Following the instructions and attempting to connect a client resulted in:

"Permission denied (publickey,gssapi-keyex,gssapi-with-mic)." 

I assume the server was expecting an SSH Key?

Solution:Edit /etc/ssh/sshd_config :

PasswordAuthentication yes

This allowed the client to log in with a password.

Thanks again

By: Reza

You made my day bro! Thank you

By: Zai

Great explanation. All went as suggested in the steps listed.My username is set to zaiftp and password is accepted.

However at final step of testing the SFTP service, it failed with message :

[root@blackmamba data]# sftp [email protected]@192.168.1.150's password:Write failed: Broken pipeCouldn't read packet: Connection reset by peer

My local IP is : 192.168.1.150

I did :  grep zaiftp /etc/passwd

and got :zaiftp:x:1005:1005::/data/sftp/upload/:/sbin/nologin

So the user dir /data/sftp/upload/ seems to have been allocated fine.

Help appreciated!

Thanks!

 

By: rohi

Best article to setup sftp server on any centos machine. Perfect step by step instructions are given. Thank you so much.

By: thelinuxwiz

Change -g to -G to add the user to a secondary group 

useradd -G sftpusers -d /upload -s /sbin/nologin mysftpuser

 

By: Jake

Hello, 

Do you use this as an internal SFTP Server only? Is a setup like this secure enough to put this on public internet if only port 22 is permitted? 

By: drakkan

You can also try SFTPGo

https://github.com/drakkan/sftpgo

it has chroot support builtin, virtual quota, atomic uploads and many other features.

It can execute configurable custom commands and/or HTTP notifications on upload, download, delete or rename.

It is written in Go, so no runtime dependencies, and it works on Windows too

 

 

By: Marc Nevin

What options exist to manage multiple connections and ensuring no single connection can take over the entire network bandwidth provided by OS/network interface ? We have multiple clients uploading during a perscribed window of time and sometime a client will get very poor (read slow) connection.   I need to balance the bandwidth among the total number of active connections while net over budening the network connection to the hosting server. 

Great article !! 

By: mcajina

When I try to connect to the SFTP server via FileZilla, I get a warning that the "server's host key is unknown," and it wants confirmation to trust the key. It then tries to connect, but fails with "Error: Connection reset by peer."

I also tried it via Terminal from my computer and I also got a warning about the "ECDSA key fingerprint." After I gave permission to continue, it also failed with a "Broken pipe" message. Same thing if I try from the server itself @localhost

My sshd_config file has the following edits, but everything else in the file that was uncommented I left alone:

PermitRootLogin noPasswordAuthentication yesSubsystem    sftp    internal-sftpMatch Group sftpusersChrootDirectory /ftpserver/%uForceCommand internal-sftpPermitTunnel noAllowAgentForwarding noAllowTcpForwarding noX11Forwarding no

When I set up a test sftp server a few months back following these instructions, it worked perfectly. Now that I need to set up a real one, it's failing to allow the user account to connect. Any ideas why I'm getting these errors now?

By: mcajina

Nevermind, I figured it out. I accidentally changed the ownership of the folder with the username to that user. It's supposed to be owned by root.

By: Bas

Thanks for this great article!

For security reasons its wise to change the port from the default 22 to something else. If you want to connect to it using SFTP, use:

sftp -oPort=1234 [email protected]

Where 1234 you write your own port of course.

By: Andrew Bruce

Dude! You actually specified nmap to test - you completely rock. To make up for the other comments - your instructions had me going in (I timed it) 8 minutes.

 

Thanks so much for saving me from my local neural net failures. And for specifying nmap as a verification test...I cannot tell you how pleased that makes me. For all of you out there - *learn nmap* as that and ncat are truly, truly your friends.

By: Andrew Bruce

One problem - this loses in many situations where you must connect over Someone Else's Network. So many time, any ports other than 80 / 443 / 22 simply get filtered out and denied egress. Don't get me wrong, I agree with you in principle (although port knocking is even more effective if you want security as *nmap the fabulous tool* will quickly figure out that 1234 is also being used) - but in practice using non-standard ports as security via obscurity has bitten me when I couldn't connect from an arbitrary endpoint where I happened to be stuck (such as in the hotel...). But rock on with the security - good stuff!

By: Camilo Luna

Works perfect for me. Thank's, it was very helpful.

By: R Helms

I setup the sftp server and it works great.  I do have an issue when I try to use ssh keys with the sftp login.  It is not working for me.  I have tried to put .ssh/authorized_keys in both /data/<account>/.ssh/authorized_keys and /data/<account>/upload/.ssh/authorized_keys.  Neither works for me.  Using sftp -vv <account>@<sftp_server> I see that it checks the id_rsa key I used but it does not seem to find the authorized_keys file on the sftp_server to read my public id_rsa.pub key.

Any suggestions/help?

By: bruno

great work, thanks!! 

By: Nice and clear

It is work for me i downloaded the file.txt that i upload to my sftpserver, but when  I Tried to upload a Static-OpenVPN.key to my sftp server and then when I used my client to get this key, didn't  let me download it.

What king of file i could  download from my sftp server?

 

By: Dny238

The instructions say howtoforge user, but the code says mysftpuser. I think it's a typo. 

 

Then create a user 'howtoforge' and assign it to the SFTPUSERS group. Below are the steps:

useradd -g sftpusers -d /upload -s /sbin/nologin mysftpuserpasswd mysftpuser