Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Network Services»SSH»How to Create SFTP Only User in Ubuntu & Debian

    How to Create SFTP Only User in Ubuntu & Debian

    By RahulAugust 23, 20223 Mins Read

    SFTP (SSH File Transfer Protocol) is a secure file protocol used to access, manage, and transfer files over an encrypted SSH transport session. Here SFTP only user means to create an account to access the server via SFTP only. That user doesn’t have SSH shell access. This allows you a secure channel to provide limited access to specific files and directories.

    This blog post describes you create SFTP-only users without shell access on Ubuntu and Debian systems.

    Step 1- Creating a New User

    First of all, create a user account in your system to use as an SFTP user. The following command will create a new account named sftpuser with no shell access. You can change the username of your choice

    sudo adduser --shell /bin/false sftpuser 
    

    The command will prompt for the password to be set for a new account.

    Step 2 – Create Directory for SFTP

    Now, create the directory structure to be accessible by the SFTP user.

    sudo mkdir -p /var/sftp/files 
    

    Here we will allow users to access the “files” directory only.

    Now, change the ownership of the files directory to the sftpuser. So that SFTP users can read and write on this directory only. No files outside of this directory will be accessible.

    sudo chown sftpuser:sftpuser /var/sftp/files 
    

    And set the owner and group owner of the /var/sftp to root. The root user has read/write access on this access. Group members and other accounts have only read and execute permissions.

    sudo chown root:root /var/sftp 
    sudo chmod 755 /var/sftp 
    

    It will restrict SFTP users from writing files under /var/sftp directory.

    Step 3 – Configure sshd for SFTP Only

    /etc/ssh/sshd_config is the main configuration file of the OpenSSH server. Be careful with changing this configuration file, because any mistake can lead to connection loss.

    Edit the SSH configuration file in a text editor:

    sudo vim /etc/ssh/sshd_config 
    

    then add the following settings at end of the file.

    Match User sftpuser
    	ForceCommand internal-sftp
    	PasswordAuthentication yes
    	ChrootDirectory /var/sftp
    	PermitTunnel no
    	AllowAgentForwarding no
    	AllowTcpForwarding no
    	X11Forwarding no
    

    Save the file and close.

    The directives are:

    • Match User Defines the username, on which the SFTP only configurations applied. In our case it is: sftpuser
    • ForceCommand internal-sftp enforce the SFTP only access to user and restrict for the shell access.
    • PasswordAuthentication yes allows password authentication for the user.
    • ChrootDirectory /var/sftp Restrict user to access directories under this directory only. Here /var/sftp is act as the root directory of the user.
    • AllowAgentForwarding no Specifies whether ssh-agent forwarding is permitted. The default is yes.
    • AllowTcpForwarding no Specifies whether TCP forwarding is permitted. The default is yes.
    • X11Forwarding no Specified where the graphical application is permitted for not

    Restart SSH service to apply new settings:

    sudo systemctl restart ssh 
    

    That’s it. You have successfully completed the instructions to create an SFTP-only user on Debian-based systems.

    Step 4 – Security Tips (Options)

    Here are some basic but important security tips for SFTP accounts in a production environment.

    1. Run SSH server on a non-standard port
    2. Disallow the password authentication and configure key-based authentication
    3. Make sure the firewall is restricted to specific IP addresses only
    4. And keep the OpenSSH package up to date

    Conclusion

    This tutorial describes you create SFTP-only users in the Ubuntu system. It will disabled shell access for the same users to restrict to a specified directory only.

    FTP SFTP SSH
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Avoid the SSH Broken Pipe Error

    Setting Up Ubuntu Docker Container with SSH Access

    A Practical Guide to Disabling SSH Timeout

    View 2 Comments

    2 Comments

    1. Akshay on March 9, 2022 8:32 am

      Hi Rahul,

      Could you help me to understand the below configuration. I couldn’t find details related to “-m” option in man pages of sshd_config.

      # sftp subsystem
      Subsystem sftp internal-sftp -m 117

      Is -m a valid option? If so, may I know what does -m 117 does?

      Reply
    2. Johnson on May 31, 2021 12:46 pm

      Thanks, Helped me a lot.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Difference Between Full Virtualization vs Paravirtualization
    • Virtualization vs. Containerization: A Comparative Analysis
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.