1. 程式人生 > 其它 >Linux的SSH預設22埠更改

Linux的SSH預設22埠更改

文章目錄
一、修改sshd配置檔案
二、重啟ssh服務
三、總結
預設22的SSH連線埠很容易受攻擊,故通過修改預設埠來達到一定的防護作用。

一、修改sshd配置檔案
檢視目前ssh連線埠

sudo netstat -tunlp | grep "ssh"
1

目前預設指向的就是22埠,接下來我們通過修改/etc/ssh/sshd_config的配置檔案來達到修改連線埠的目的:

sudo vim /etc/ssh/sshd_config
1

這裡需要重點關注#Port 22的位置,由於預設就是22埠所以該行是被註釋的,我們測試接下來用2020埠來連線SSH,因為可以存在多個SSH連線埠,這裡我們先暫時保留22埠,修改如下:

# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 22
Port 2020
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
二、重啟ssh服務
重啟ssh命令需要根據伺服器系統的版本,CentOS 6及以下

sudo service ssh restart
1
CentOS 7及以上

systemctl restart sshd
1
再次檢視當前開放的SSH連線埠

sudo netstat -tunlp | grep "ssh"
1
由於22埠和2020埠同時開放,所以理論結果應該會顯示這兩個埠,測試一下2020埠是否能夠連線成功,若測試成功後即按照上述步驟在配置檔案裡將22埠去除後重啟ssh服務即達到了我們更改22預設連線埠的目的。

三、總結
通過調整預設連線埠達到一定的防護作用,需在瞭解系統版本的基礎上進行調整。
————————————————
版權宣告:本文為CSDN博主「imVainiycos」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/imVainiycos/article/details/109841406