SFTP Server on Rocky Linux 8 on Azure User Guide
Videos
Overview
This guide covers the deployment and operation of SFTP Server on Rocky Linux 8 on Microsoft Azure, using cloudimg's virtual machine image from the Azure Marketplace (offer e4cf7df5-75b8-4ecd-9814-4d38d7fbe590).
SFTP is the file transfer protocol that runs over SSH: files travel encrypted, on the same port 22 that SSH uses, and every common client (sftp on Linux and macOS, WinSCP, FileZilla, Cyberduck) speaks it. This image sets up Rocky Linux 8's own OpenSSH server as an SFTP server: each SFTP user is locked into a directory of their own and can transfer files, and nothing else.
What's included:
- Rocky Linux 8.10 with OpenSSH 8.0, its SFTP server built in (
internal-sftp) - An
sftpusersgroup: every member is chrooted to/sftp/<user>, gets SFTP only (no shell), and may not forward ports, X11 or tunnels - A helper,
/usr/local/sbin/sftp-add-user, that creates an SFTP user and their directories in one step /sftpon its own 10 GB Azure data disk, formatted and mounted at the first boot- SSH host keys made at each virtual machine's first boot, so two machines from this image never share one
- SELinux in enforcing mode
- cloudimg's support team on hand 24 hours a day by email (support@cloudimg.co.uk), with a guaranteed response within 24 hours
Platform: Microsoft Azure (Generation 2 Hyper V)
Default user: azureuser
Prerequisites
Before deploying this image, ensure you have:
- An active Microsoft Azure subscription, and permission to create virtual machines in a resource group
- The Azure CLI, installed locally or in Azure Cloud Shell
- An SSH key pair for Linux VM access
- A network security group that allows SSH (port 22) from your own network and from the networks your SFTP users connect from: SFTP uses the same port
Step 1: Deploy the Virtual Machine
Deploy the image by its Marketplace URN. The image carries a purchase plan, so accept its terms once per subscription first:
az vm image terms accept --urn cloudimg1647283583153:e4cf7df5-75b8-4ecd-9814-4d38d7fbe590:e4cf7df5-75b8-4ecd-9814-4d38d7fbe590-gen2:latest
az vm create \
--resource-group myResourceGroup \
--name my-sftp-vm \
--image cloudimg1647283583153:e4cf7df5-75b8-4ecd-9814-4d38d7fbe590:e4cf7df5-75b8-4ecd-9814-4d38d7fbe590-gen2:latest \
--admin-username azureuser \
--ssh-key-values ~/.ssh/id_rsa.pub \
--public-ip-sku Standard
Add --size with the size you need. The image brings one data disk with it, at LUN 0; the first boot formats it and mounts it at /sftp, where every SFTP user's files live.
Step 2: Connect via SSH
Run these on your own workstation, not on the VM. Find the public IP, then connect as the administrator:
az vm show --resource-group myResourceGroup --name my-sftp-vm --show-details --query publicIps -o tsv
ssh azureuser@<vm-ip>
Step 3: Check the server
Wait for the first boot to finish, then check that the SSH server and the data disk service are active:
sudo cloud-init status --wait
systemctl is-active sshd cloudimg-datadisk
status: done
active
active
/sftp is the data disk, mounted by its UUID:
findmnt -no TARGET,FSTYPE,SIZE /sftp
/sftp ext4 9.8G
The rules for SFTP users are a Match Group sftpusers block at the end of /etc/ssh/sshd_config:
sudo sed -n '/^Match Group sftpusers/,$p' /etc/ssh/sshd_config
Match Group sftpusers
ChrootDirectory /sftp/%u
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no
PermitTunnel no
Every member of the group is locked into /sftp/<user>, gets the SFTP server only, and may not forward ports, X11 or tunnels.
Step 4: Host key fingerprints
The SSH host keys are made at the first boot of each virtual machine. Give your users the fingerprints, so they can check them the first time their client connects:
for t in ed25519 ecdsa rsa; do ssh-keygen -l -E sha256 -f /etc/ssh/ssh_host_${t}_key.pub; done
256 SHA256:HykyJD1nGxvX5u/IvwpGCb1mrK2bYOuo8MLWVlu8vhE root@my-sftp-vm (ED25519)
256 SHA256:vX1bA7OGlM/BXqxsCh9TYYvWD2H6zlS4BmCLy2UjdjA root@my-sftp-vm (ECDSA)
3072 SHA256:MebuqhxP3hROy0y7IcnWW4TAn27w3jNqLdLDz7pl7zc root@my-sftp-vm (RSA)
Your fingerprints are your own machine's, and differ from these.
Step 5: Add an SFTP user with an SSH key
Ask the user for their public key (the .pub file; the private key never leaves their computer). Copy it to the virtual machine from your workstation:
scp alice.pub azureuser@<vm-ip>:
Then, on the virtual machine, create the user and give them the key. The helper lives in /usr/local/sbin, which sudo does not search on Rocky Linux 8, so call it by its full path:
sudo /usr/local/sbin/sftp-add-user alice
sudo install -d -m 700 -o alice -g sftpusers /home/alice/.ssh
sudo install -m 600 -o alice -g sftpusers alice.pub /home/alice/.ssh/authorized_keys
Created alice. Set a password (passwd alice) or add a key to /home/alice/.ssh/authorized_keys.
The helper creates the account in the sftpusers group with no login shell, the user's chroot /sftp/alice (owned by root, as OpenSSH requires) and a writable upload directory inside it:
id alice
sudo ls -la /sftp/alice
uid=1001(alice) gid=1001(sftpusers) groups=1001(sftpusers)
total 12
drwxr-xr-x. 3 root root 4096 Dec 2 12:36 .
drwxr-xr-x. 4 root root 4096 Dec 2 12:36 ..
drwxr-x---. 2 alice sftpusers 4096 Dec 2 12:36 upload
To see the rules the SSH server applies to alice now that the account exists:
sudo sshd -T -C user=alice,host=client,addr=203.0.113.9 | grep -E '^(chrootdirectory|forcecommand|allowtcpforwarding|x11forwarding|permittunnel|passwordauthentication) '
passwordauthentication no
x11forwarding no
allowtcpforwarding no
forcecommand internal-sftp
chrootdirectory /sftp/%u
permittunnel no
A password on its own does not let the user in: password logins are off on a virtual machine deployed with an SSH key. To allow them, see Step 8.
Step 6: Connect as the SFTP user
From the user's computer, with their private key:
sftp -i alice_key alice@<vm-ip>
Inside the session, / is the user's own directory, and upload is where they can write:
Connected to <vm-ip>.
sftp> pwd
Remote working directory: /
sftp> ls
upload
sftp> cd upload
sftp> put hello.txt
Uploading hello.txt to /upload/hello.txt
hello.txt 100% 20 0.2KB/s 00:00
sftp> ls -l
-rw-r--r-- 1 1001 1001 20 Dec 2 12:37 hello.txt
sftp> get hello.txt back.txt
Fetching /upload/hello.txt to back.txt
hello.txt 100% 20 0.1KB/s 00:00
Graphical clients work the same way: choose the SFTP protocol, port 22, user alice, and the private key.
Step 7: What an SFTP user cannot do
Outside upload, the chroot is read only, and nothing above it is visible:
sftp> mkdir /evil
remote mkdir "/evil": Permission denied
sftp> put hello.txt /hello.txt
dest open "/hello.txt": Permission denied
sftp> ls /etc
Can't ls: "/etc" not found
An SSH login is refused, so the user has no shell:
ssh -i alice_key alice@<vm-ip>
This service allows sftp connections only.
Port forwarding, X11 forwarding and tunnels are off for the group (Steps 3 and 5), and the administrator account azureuser is not a member of it, so it keeps a normal shell.
Step 8: Password logins for SFTP users (optional)
Keys are safer than passwords. If some users can only use a password, switch password logins on for the sftpusers group only by adding a line to its Match block, check the configuration, and restart the SSH server:
sudo sed -i '/^Match Group sftpusers$/a\ PasswordAuthentication yes' /etc/ssh/sshd_config
sudo sshd -t && sudo systemctl restart sshd
sudo sshd -T -C user=alice,host=client,addr=203.0.113.9 | grep ^passwordauthentication
sudo sshd -T -C user=azureuser,host=client,addr=203.0.113.9 | grep ^passwordauthentication
passwordauthentication yes
passwordauthentication no
SFTP users may now sign in with a password; azureuser still signs in with its key only. Set each user's password with sudo passwd alice, which asks for it twice without showing it. To switch password logins off again, delete the PasswordAuthentication yes line from the block and restart sshd.
Step 9: Manage users
List the SFTP users (the accounts whose group is sftpusers):
awk -F: -v g="$(getent group sftpusers | cut -d: -f3)" '$4 == g {print $1}' /etc/passwd
alice
Add another key for a user by appending its line to /home/<user>/.ssh/authorized_keys. To remove a user and all their files:
sudo userdel -r alice
sudo rm -rf /sftp/alice
Leave out the second line to keep their files. userdel refuses a user who is still connected (user alice is currently used by process ...): end their sessions first with sudo pkill -u alice.
Step 10: Keep the system updated
The image does not install updates by itself. Apply Rocky Linux's updates, including OpenSSH, and restart when the command below asks for it:
sudo dnf -y update
sudo dnf needs-restarting -r || sudo systemctl reboot
The data disk, the users and the SFTP rules all come back after the restart. To install security updates automatically from now on, use Rocky Linux's dnf-automatic:
sudo dnf -y install dnf-automatic
sudo systemctl enable --now dnf-automatic-install.timer
Server components
| Component | Version | Notes |
|---|---|---|
| Rocky Linux | 8.10 | Generation 2 Hyper V |
| OpenSSH server | 8.0p1 | sshd, port 22; SFTP by internal-sftp |
| SELinux | enforcing |
Filesystem layout
| Mount | Size | Holds |
|---|---|---|
/ |
63 GB | the system, and the users' home directories (their SSH keys) |
/boot |
1 GB | the boot loader and kernels |
/boot/efi |
100 MB | the EFI system partition |
/sftp |
10 GB data disk (LUN 0) | every SFTP user's chroot and files |
/mnt |
Azure temporary disk | scratch space only: its contents are lost when the machine is deallocated |
Scripts and configuration
| Path | What it is |
|---|---|
/usr/local/sbin/sftp-add-user |
creates an SFTP user: the account, /sftp/<user> and /sftp/<user>/upload |
/usr/local/sbin/cloudimg-datadisk |
formats and mounts the data disk at the first boot (/etc/cloudimg/datadisk.conf, cloudimg-datadisk.service) |
/etc/ssh/sshd_config |
the SSH server's configuration; the Match Group sftpusers block at the end holds the SFTP rules |
/etc/ssh/sshd_config.cloudimg-orig |
Rocky Linux's original configuration, for comparison |
sudo journalctl -u sshd |
the SSH server's log: logins, and the sessions it refused |
On startup
At the first boot, cloudimg-datadisk formats the data disk and mounts it at /sftp, and the SSH host keys are made. Neither runs again on later boots. There is no automatic update unless you switch it on (Step 10).
Troubleshooting
sudo: sftp-add-user: command not found: call it by its full path,sudo /usr/local/sbin/sftp-add-user.- The user gets
Permission denied (publickey,...): check that/home/<user>/.sshis mode 700 andauthorized_keysmode 600, both owned by the user, and that the key in it is the user's public key.sudo journalctl -u sshdnames the reason. - The user connects, then the session closes at once: OpenSSH refuses a chroot that is not owned by root or that others can write.
/sftp/<user>must be owned byroot:rootwith mode 755; the helper sets it that way. - The user cannot write: only
upload(and what they create in it) is writable.
Security recommendations
- Keep port 22 open only to your own network and to the networks your SFTP users connect from.
- Prefer SSH keys; switch on passwords (Step 8) only for the users who need them.
- Rocky Linux's
rpcbindservice listens on port 111 on every interface. The network security group from the Prerequisites does not open it; if you do not use NFS, switch it off:
sudo systemctl disable --now rpcbind.service rpcbind.socket
- Update the system regularly, or switch on automatic updates (Step 10).
Support
cloudimg's support team is on hand 24 hours a day by email (support@cloudimg.co.uk), with a guaranteed response within 24 hours.
For OpenSSH itself, see https://www.openssh.com/manual.html.