هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowGroups sshusers
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
ClientAliveInterval 300
ClientAliveCountMax 2Keep your current session open and test a new login in a second terminal before you close anything.
Step by step
- Before you start: your key login works (
ssh -i ~/.ssh/id_ed25519 alice@web1) and you have a second way in (VM or cloud console). - Create the group of allowed users:
- Write the settings in a drop-in file (quick answer). Files in
sshd_config.d/are read first, and in sshd the first value wins — so your file beats the defaults in the big main file.
| Setting | Why |
|---|---|
PermitRootLogin no | Log in as a person, then sudo — the log shows who did it. |
PasswordAuthentication no + KbdInteractiveAuthentication no | No password guessing possible. |
AllowGroups sshusers | Only members may log in, even if other accounts exist. |
MaxAuthTries 3, LoginGraceTime 30 | Fewer tries, less time per connection. |
ClientAliveInterval 300 + CountMax 2 | Dead sessions close after about 10 minutes. |
- Test and reload:
sshd -t
systemctl reload sshd
sshd -T | grep -Ei '^(permitrootlogin|passwordauthentication|allowgroups|maxauthtries)'On Ubuntu the service is ssh (systemctl reload ssh).
- Crypto: use the system-wide crypto policy on RHEL instead of hand-written cipher lists:
update-crypto-policies --show
update-crypto-policies --set DEFAULTCheck what the server offers from another machine:
nmap --script ssh2-enum-algos -p 22 web1.example.com- Network layer: allow port 22 only from your office, VPN or bastion; add Fail2ban for what is left open.
Check that it worked
From another machine:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password alice@web1.example.com
alice@web1.example.com: Permission denied (publickey).
ssh root@web1.example.com
root@web1.example.com: Permission denied (publickey).
ssh alice@web1.example.comPassword login and root login are refused, your key login works.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Locked out after reload | No key login, or the user is not in sshusers. | Use the old session or the console; usermod -aG sshusers USER. |
| Your setting has no effect | Another file sets it first (cloud images: 50-cloud-init.conf with PasswordAuthentication yes). | sshd -T shows the real value; rename your file so it sorts first (10-…) or fix the other file. |
sshd -t errors | Typo or unknown option. | Fix the line it names; never reload with errors. |
| Ansible/CI can not log in any more | Its user is not in sshusers. | Add the deploy user to the group. |
| Port changed, but connections fail | SELinux port label / firewall. | semanage port -a -t ssh_port_t -p tcp 2222 and open the firewall. |
REMOTE HOST IDENTIFICATION HAS CHANGED on clients | Server was rebuilt (new host keys). | Check with the admin, then ssh-keygen -R HOST. |
Go further
- Command pages: sshd, ssh, nmap.
- Next how-tos: Log in with SSH keys, Block brute-force logins with Fail2ban, See who logged in and when, Connect to EC2 without SSH keys, Use a jump host.
- Full guide: Install and harden the OpenSSH server.
- Learn it: RHCSA — sshd_config, RHCSA — Root login, RHCSA — Limit users.
- Practise: Lab: harden the SSH server.
1Why put your settings in /etc/ssh/sshd_config.d/10-hardening.conf?
It is read first, and in sshd the first value of a setting wins.