sftp is a way to use ftp on a linux server. it use the ssh account to connect the server, we can setup the sftp account and provide differences authentication
create sftp account without shell permission
1 | sudo adduser --shell /bin/false sftpuser |
then set the password for the user
1 | sudo passwd sftpuser |
create sftp directory for the user, and give the permission
/var/sftp/files here
1 | sudo mkdir -p /var/sftp/files |
create public key
we need to create the files in the user’s home directory, and generate the authentication keys
1 | mkdir /home/sftpuser/.ssh |
generate keys by ssh-keygen -t rsa
and point the file location to /home/sftpuser/.ssh/id_rsa
set authorized keys and permission
1 | cd .ssh |
save the generate private key to your compucter and give 600 permission
1 | cat /home/sftp/.ssh/id_rsa > sftp.pem |
update sftp user config to enable password and public key authentication
edit file at /etc/ssh/sshd_config, append below lines
1 | Match User sftpuser |
set PasswordAuthentication to yes to enable password authentication,
set PubkeyAuthentication to yes to enable public key authentication,
and then restart sshd servicessudo service sshd restart
test sftp connection by command line
password authentication
1 | sftp sftpuser@<server_host> |
public key authentication
1 | sftp -i sftp.pem sftpuser@<server_host> |