متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 77Security · SSH

How to harden the SSH server

Every server on the internet gets SSH login attempts within minutes. A hardened SSH server allows keys only, no root login, only the people who need it, and modern crypto — and you change it without locking yourself out. This how-to writes one drop-in file with the settings, tests it, and checks the result from the outside.

  • 5 الدروس
  • 10 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

77.1

The quick answer

/etc/ssh/sshd_config.d/10-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowGroups sshusers
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
ClientAliveInterval 300
ClientAliveCountMax 2
sshd -t
systemctl reload sshd

Keep your current session open and test a new login in a second terminal before you close anything.

77.2

Step by step

  1. 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).
  2. Create the group of allowed users:
groupadd sshusers
usermod -aG sshusers alice
  1. 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.
SettingWhy
PermitRootLogin noLog in as a person, then sudo — the log shows who did it.
PasswordAuthentication no + KbdInteractiveAuthentication noNo password guessing possible.
AllowGroups sshusersOnly members may log in, even if other accounts exist.
MaxAuthTries 3, LoginGraceTime 30Fewer tries, less time per connection.
ClientAliveInterval 300 + CountMax 2Dead sessions close after about 10 minutes.
  1. 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).

  1. Crypto: use the system-wide crypto policy on RHEL instead of hand-written cipher lists:
update-crypto-policies --show
update-crypto-policies --set DEFAULT

Check what the server offers from another machine:

nmap --script ssh2-enum-algos -p 22 web1.example.com
  1. Network layer: allow port 22 only from your office, VPN or bastion; add Fail2ban for what is left open.

77.3

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.com

Password login and root login are refused, your key login works.

الأوامر في هذا الدرس

77.4

Common problems

ProblemCauseFix
Locked out after reloadNo key login, or the user is not in sshusers.Use the old session or the console; usermod -aG sshusers USER.
Your setting has no effectAnother 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 errorsTypo or unknown option.Fix the line it names; never reload with errors.
Ansible/CI can not log in any moreIts user is not in sshusers.Add the deploy user to the group.
Port changed, but connections failSELinux port label / firewall.semanage port -a -t ssh_port_t -p tcp 2222 and open the firewall.
REMOTE HOST IDENTIFICATION HAS CHANGED on clientsServer was rebuilt (new host keys).Check with the admin, then ssh-keygen -R HOST.

77.5

Go further

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.

2Which command shows the settings sshd really uses?

كل الأوامر في هذا الفصل