Get the App
SLTechnology News&Howtos  ›  Servers  › 

Build sftp by Linux

Shulou Source: shulou.com Published: 2022-06-02 07:50:52 11月19日 Update

1: create user groups, users, and set passwords

Groupadd sftp

Useradd-g sftp-s / sbin/nologin-M sftp1

Passwd sftp1

2: create the sftp root directory (created under root users)

Mkdir / home/sftp/uploads

one

3: edit the sftp configuration file

Vim / etc/ssh/sshd_config

one

Add at the end (be sure to put the addition at the end, or restart sshd with an error):

Subsystem sftp internal-sftp # # specifies the use of sftp services to use the internal-sftp that comes with the system

Match Group sftp # # users who match sftp groups. If you want to match multiple groups, separate them with commas.

ChrootDirectory / home/sftp/uploads/ # # sftp home directory assigned to / home/sftp/uploads/

ForceCommand internal-sftp # # specify sftp command

AllowTcpForwarding no # # users cannot use port forwarding

X11Forwarding no # # users cannot use port forwarding

You can test whether the configuration file is correct through sshd-t, and correct if there is no input.

4: set home directory permissions

Chown root:sftp / home/sftp/uploads/ folder owner must be root, user group may not be root

The permission of chmod 744 / home/sftp / / cannot exceed 755, otherwise it will cause login error, which can be 755.

5: restart sshd

Systemctl restart sshd

one

6: log in to sftp (linux/Windows)

Sftp sftp1@ip address

one

At this point, the logged-in user only has permission to view the home directory and its subdirectories, and cannot delete, upload, download, or rename files.

7: create a folder for sftp1

Mkdir / home/sftp/uploads/test1

Chown sftp1:sftp / home/sftp/uploads/test1

Chmod 744 / home/sftp/uploads/test1

8: restart sshd

9: log in to sshd and execute the ls command to view the files in the current directory. You can see a test1 folder. Only when cd enters the test1 folder can you upload local files to the test1 folder or download files from the test1 folder to the local directory. (ignore the test2 folder, which I set up for sftp2 users)

Delete the file an in the ~ directory to the test1 file:

Tags: Files users folders directories logins permissions commands multiple end user groups ports configurations only can pass address subdirectory subdirectory password owner Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Huawei Linux NVidia