[CENTOS] Ändern Sie die SSH-Portnummer

Die Standardeinstellung des SSH-Servers verwendet Port 22. Diese Portnummer ist allgemein bekannt und erhöht das Risiko eines Eindringens beim Öffnen des Ports. Ändern Sie daher die SSH-Portnummer von 22, um Risiken zu vermeiden.

Ändern Sie die SSH-Konfigurationsdatei

Erstellen Sie zuvor eine Sicherungskopie der SSH-Konfigurationsdatei zur Sicherung

[root@localhost ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.old

Bearbeiten Sie den Port-Eintrag in der SSH-Konfigurationsdatei (/ etc / ssh / sshd_config), um die Portnummer zu ändern.

/etc/ssh/sshd_config Port 59695 (59695-Nummern sind Beispiele)

Verwenden Sie den Befehl diff, um die Einstellungsänderung zu bestätigen

[root@localhost ~]# diff /etc/ssh/sshd_config.old /etc/ssh/sshd_config
17c17
< #Port 59695
---
> Port 59695

Überprüfen Sie die Syntax der Konfigurationsdatei und stellen Sie sicher, dass keine Fehler vorliegen

[root@localhost ~]# sshd -t

Wenn kein Fehler angezeigt wird, wurde die Syntax normal geändert.

SSH-Einstellungen widerspiegeln

[root@localhost ~]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service

Überprüfen Sie, ob Sie sich mit der geänderten Portnummer SSH anmelden können

SSH-Verbindung von der Client-Seite

ssh -p 59695 usr@(IP)
ssh: connect to host (IP) port 59695: Connection timed out

Es wird keine Verbindung hergestellt, anscheinend muss die Firewall festgelegt werden

[root@localhost ~]# cat /usr/lib/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="22"/>
</service>

Kopieren Sie die Konfigurationsdatei, bevor Sie die Firewall konfigurieren

[root@localhost ~]#  cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/

Erstellen Sie dann eine Sicherungskopie der Konfigurationsdatei

[root@localhost ~]# cp /etc/firewalld/services/ssh.xml  /etc/firewalld/services/ssh.xml.old

Ändern Sie den Inhalt der Einstellungsdatei Ändern Sie den Port in /usr/lib/firewalld/services/ssh.xml in 59695

[root@localhost ~]#  cat /etc/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="59695"/>
</service>

Verwenden Sie den Befehl diff, um die Einstellungsänderung zu bestätigen

[root@localhost ~]# diff /etc/firewalld/services/ssh.xml.old /etc/firewalld/services/ssh.xml
5c5
<   <port protocol="tcp" port="22"/>
---
>   <port protocol="tcp" port="59695"/>

Reflektieren Sie die geänderten Einstellungen

[root@localhost ~]# firewall-cmd --reload
success

Wenn Erfolg angezeigt wird, OK

Starten Sie sshd neu

[root@localhost ~]# systemctl restart sshd

Überprüfen Sie, ob Sie sich erneut mit der geänderten Portnummer SSH anmelden können

ssh -p 59695 usr@(IP)
Last login: Time from (IP)
[usr@localhost ~]$

Die Verbindung wird hergestellt, indem der SSH-Port sicher geändert wird

Referenz-URL https://webkaru.net/linux/change-ssh-port/ https://webkaru.net/linux/centos7-firewalld-ssh-port/

Recommended Posts

Ändern Sie die SSH-Portnummer