22/tcp = SFTP only
2222/tcp = normal SSH shell
Design:
sshd.service -> /etc/ssh/sshd_config -> port 2222
sshd-sftp.service -> /etc/ssh/sshd_config_sftp -> port 22
dnf install -y openssh-server firewalld policycoreutils-python-utils
systemctl enable --now firewalld sshd
semanage port -a -t ssh_port_t -p tcp 2222 2>/dev/null || \
semanage port -m -t ssh_port_t -p tcp 2222
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload
groupadd --system sftpusers 2>/dev/null || true
File: /etc/ssh/sshd_config
Port 2222
Protocol 2
UsePAM yes
Include /etc/ssh/sshd_config.d/*.conf
PermitRootLogin no
PasswordAuthentication yes
KbdInteractiveAuthentication no
PubkeyAuthentication yes
X11Forwarding no
PermitTunnel no
PermitUserEnvironment no
MaxStartups 10:30:60
MaxSessions 10
MaxAuthTries 4
LoginGraceTime 60
ClientAliveCountMax 0
Subsystem sftp /usr/libexec/openssh/sftp-server
Banner /etc/banner
Create drop-in:
File: /etc/ssh/sshd_config.d/05-sftp-hardening.conf
# Block SFTP-only users from shell SSH on port 2222.
DenyGroups sftpusers
File: /etc/ssh/sshd_config_sftp
Port 22
Protocol 2
UsePAM yes
ListenAddress 0.0.0.0
ListenAddress ::
Subsystem sftp internal-sftp
PermitRootLogin no
PasswordAuthentication yes
KbdInteractiveAuthentication no
PubkeyAuthentication yes
X11Forwarding no
AllowTcpForwarding no
PermitTunnel no
AllowAgentForwarding no
PermitTTY no
PermitUserEnvironment no
AllowGroups sftpusers
Match Group sftpusers
ChrootDirectory /sftp/%u
ForceCommand internal-sftp -d /upload
X11Forwarding no
AllowTcpForwarding no
PermitTunnel no
AllowAgentForwarding no
PermitTTY no
File: /etc/systemd/system/sshd-sftp.service
[Unit]
Description=OpenSSH SFTP-only service on port 22
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target
[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D -f /etc/ssh/sshd_config_sftp $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
Enable:
sshd -t -f /etc/ssh/sshd_config
sshd -t -f /etc/ssh/sshd_config_sftp
systemctl daemon-reload
systemctl restart sshd
systemctl enable --now sshd-sftp.service
Verify:
ss -tlnp | grep sshd
Expected:
:22
:2222
File: add-new-sftp-user
wget -P /usr/local/sbin/add-new-sftp-user https://raw.githubusercontent.com/archham/linux.scripts/refs/heads/main/sftp/add-new-sftp-user.sh && chmod 700 /usr/local/sbin/add-new-sftp-user
Password-based:
/usr/local/sbin/add_new_sftp_user.sh customer1
passwd customer1
SSH-key based:
/usr/local/sbin/add_new_sftp_user.sh customer1 'ssh-ed25519 AAAA...'
Check shell SSH configuration:
sshd -T -f /etc/ssh/sshd_config
Check SFTP configuration:
sshd -T -f /etc/ssh/sshd_config_sftp
Verify listeners:
ss -tlnp | grep sshd
sftp -P 22 customer1@server.example.com
Should work.
ssh -p 2222 customer1@server.example.com
Should fail.
ssh -p 2222 adminuser@server.example.com
Should work.
rm -rf /sftp/DESIRED_USER/
userdel DESIRED_USER
| User Type | Port 22 (SFTP) | Port 2222 (SSH) |
|---|---|---|
Member of sftpusers |
Allowed | Denied |
| Normal admin user | Denied | Allowed |
| Root | Denied | Denied (PermitRootLogin no) |