Showing posts with label sftp. Show all posts
Showing posts with label sftp. Show all posts

Tuesday, June 20, 2023

Configure sftp server

sftp server is an ftp server, but using ssh protocol. To set up one, please follow below steps.


First, create a directory to keep the uploaded data
$ sudo mkdir /data

Then, create a special group for sftp users
$ sudo groupadd sftp_users

Next, create a user called newuser that doesn't have regular login privileges, as a member of the newly created group, home directory set to /upload, and shell set to /sbin/nologin.
$ sudo useradd -g sftp_users -d /upload -s /sbin/nologin newuser

Set a password for the new user
$ sudo passwd newuser

Create an sftp directory for the new user, and set proper permissions for the directory
$ sudo mkdir -p /data/newuser/upload
$ sudo chown -R root:sftp_users /data/newuser
$ sudo chown -R newuser:sftp_users /data/newuser/upload

Add below lines to the end of /etc/ssh/sshd_config. Use any text editor that you are familiar with. Save the file once done.
Match Group sftp_users
ChrootDirectory /data/%u
ForceCommand internal-sftpd 

Test /etc/ssh/sshd_config for any syntax error
$ sudo sshd -t -f /etc/ssh/sshd_config

Restart ssh if no error reported from the above command
$ sudo systemctl restart sshd

Now we can use the user to upload or download data from the server
$ sftp newuser@server.ip.add.ress

Use "get" command to download, and "put" command to upload file.

We can also use applications like winscp and filezilla, to get a user interface.

Wednesday, June 18, 2008

Using sftp to transfer file through network

There are a lot of ways on how to transfer files through network in linux and open source. One of the solution is to use sftp a.k.a. secure file transfer protocol. The reason this sftp is different from the original ftp is, sftp will do all its operation over encrypted ssh transport. This make sure that your file is safely transferred through network. To use sftp, you can just run command
$ sftp user@servername
For example,
$ sftp foo@server.name or
$ sftp foo@192.168.0.1

To use sftp efficiently, a few important commands one need to know, as listed below:

  1. To get help on commands available.
    • sftp> help
    • sftp> ?
  2. The commands are generally divided into 2 groups: the commands that can be used to manipulate localhost and the commands that can be used to manipulate remote host. The commands that start with 'l' are specially for locahost only. Example, to list all directory listing on localhost:
    • sftp> lls
  3. To list directories on remote host, use:
    • sftp> ls
  4. The most important command, how to upload file to the remotehost
    • sftp> put /local/path /remote/path
  5. To download file from remotehost
    • sftp> get /remote/path /local/path
To get more information, use the first step to generate help page where list of commands and how to use it are shown.