DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Install And Configure SFTP

Some of the developers from Nautilus project team have asked for SFTP access to at least one of the app server in Stratos DC. After going through the requirements, the system admins team has decided to configure the SFTP server on App Server 1 server in Stratos Datacenter. Please configure it as per the following instructions:

a. Create a SFTP user rose and set its password to ksH85UJjhb. There is already a group called ftp, you can utilise the same.

b. Password authentication should be enabled for this user.

c. SFTP user should only be allowed to make SFTP connections.


Step 1: Log in to App Server 1

From the jump host, SSH into stapp01 as tony:

# Login: user=tony | password=Ir0nM@n | host=stapp01 (10.244.234.203)
ssh tony@stapp01
Enter fullscreen mode Exit fullscreen mode

Output:

thor@jump-host ~$ ssh tony@stapp01
The authenticity of host 'stapp01 (10.244.234.203)' can't be established.
ED25519 key fingerprint is SHA256:ottNiKFuV3XUMmGi1d08ZjUQOqrZac3botOUEDcbLP4.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'stapp01' (ED25519) to the list of known hosts.
tony@stapp01's password: 
[tony@stapp01 ~]$ 
Enter fullscreen mode Exit fullscreen mode

Step 2: Switch to root

All configuration changes require root privileges:

# Login: user=tony | password=Ir0nM@n | sudo to root (same password)
sudo -i
Enter fullscreen mode Exit fullscreen mode

Output:

[tony@stapp01 ~]$ sudo -i

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for tony: 
[root@stapp01 ~]# 
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the SFTP user rose

Create the user with the ftp group and set the shell to /sbin/nologin so shell login is impossible:

# Login: root@stapp01 | Create user rose, group=ftp, no shell
useradd -G ftp -s /sbin/nologin rose
Enter fullscreen mode Exit fullscreen mode

💡 Why /sbin/nologin? It prevents the user from getting an interactive shell. Combined with ForceCommand internal-sftp (Step 5), the user can only use SFTP.

Set the password to exactly ksH85UJjhb:

# Login: root@stapp01 | Set password to ksH85UJjhb (twice)
passwd rose
Enter fullscreen mode Exit fullscreen mode

When prompted, enter ksH85UJjhb twice:

Changing password for user rose.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
Enter fullscreen mode Exit fullscreen mode

âš ī¸ Note: Nothing is displayed on screen while typing the password — that's normal Linux behavior. Type it carefully and press Enter, then repeat.


Step 4: Verify the user and group membership

# Login: root@stapp01 | Verify user exists, group=ftp
id rose
Enter fullscreen mode Exit fullscreen mode

Output:

uid=1001(rose) gid=1001(rose) groups=1001(rose),50(ftp)
Enter fullscreen mode Exit fullscreen mode

✅ groups=...,50(ftp) confirms rose is in the ftp group.

Confirm the shell is nologin:

# Login: root@stapp01 | Confirm shell is nologin
getent passwd rose
Enter fullscreen mode Exit fullscreen mode

Output:

rose:x:1001:1001::/home/rose:/sbin/nologin
Enter fullscreen mode Exit fullscreen mode

✅ /sbin/nologin confirms no shell access.


Step 5: Configure SSH to force user rose to only use SFTP

Check whether a Match block already exists for rose:

# Login: root@stapp01 | Check for existing Match block
grep -A2 "Match User rose" /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

(No output in this run — the block doesn't exist yet, so we add it.)

Edit the SSH daemon configuration:

# Login: root@stapp01 | Add SFTP-only Match block at end of file
vi /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

Add the following block at the end of the file:

Match User rose
    ForceCommand internal-sftp
    PasswordAuthentication yes
Enter fullscreen mode Exit fullscreen mode

💡 What each line does:

  • Match User rose — applies the following rules only to user rose.
  • ForceCommand internal-sftp — forces every connection to use SFTP, blocking shell/SSH commands.
  • PasswordAuthentication yes — explicitly enables password-based authentication for this user (requirement b).

Step 6: Validate and restart SSH

Test the configuration before restarting:

# Login: root@stapp01 | Validate SSH config
sshd -t
Enter fullscreen mode Exit fullscreen mode

(No output = syntax is valid.)

Restart the SSH service to apply changes:

# Login: root@stapp01 | Restart SSH to apply the Match block
systemctl restart sshd
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify SFTP access works

Test from the server itself (or the jump host):

# Login: user=rose | password=ksH85UJjhb | test SFTP connection
sftp rose@stapp01
Enter fullscreen mode Exit fullscreen mode

Enter password ksH85UJjhb when prompted:

The authenticity of host 'stapp01 (10.244.234.203)' can't be established.
ED25519 key fingerprint is SHA256:ottNiKFuV3XUMmGi1d08ZjUQOqrZac3botOUEDcbLP4.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'stapp01' (ED25519) to the list of known hosts.
rose@stapp01's password: 
Connected to stapp01.
sftp> 
Enter fullscreen mode Exit fullscreen mode

✅ SFTP login successful — the sftp> prompt confirms rose can connect via SFTP.

â„šī¸ In the transcript, a typed ssh rose@stapp01 inside the sftp> prompt returned Invalid command. — that's expected. The sftp> prompt is an SFTP shell, not a regular shell; ssh is not an SFTP command. This actually confirms the user is locked into SFTP mode.


Step 8: Verify SSH shell login is blocked

From a separate terminal (or after exiting SFTP with exit), attempt a normal SSH login:

# Login: user=rose | password=ksH85UJjhb | should be REJECTED
ssh rose@stapp01
Enter fullscreen mode Exit fullscreen mode

Expected result — connection accepted for auth but shell denied:

rose@stapp01's password: 
This service allows sftp connections only.
Connection to stapp01 closed.
Enter fullscreen mode Exit fullscreen mode

✅ The ForceCommand internal-sftp directive blocks the shell, fulfilling requirement c (SFTP-only access).


✅ Final Summary

Requirement Result Proof
User rose created, in ftp group, password set ✅ Done id rose → groups=...,50(ftp)
Password authentication enabled for rose ✅ Done PasswordAuthentication yes in Match block
SFTP-only access (no shell) ✅ Done SFTP connects; SSH shell rejected with "This service allows sftp connections only."

🔑 Key Takeaways

  1. useradd -G ftp -s /sbin/nologin rose — adds the user to the ftp group and blocks shell login in one step.
  2. passwd rose — sets the password; nothing is echoed on screen while typing (normal behavior).
  3. Match User <name> block in /etc/ssh/sshd_config lets you apply per-user SSH rules without affecting other users.
  4. ForceCommand internal-sftp is the critical directive that restricts a user to SFTP only — no shell, no SCP, no SSH commands.
  5. PasswordAuthentication yes inside the Match block explicitly satisfies the "password auth enabled" requirement.
  6. sshd -t validates the config before restart — always run it to catch syntax errors before systemctl restart sshd.
  7. /sbin/nologin + ForceCommand internal-sftp together provide defense in depth: even if the Match block were removed, the nologin shell would still block interactive login.

Top comments (0)