63

Linux中修改SSH端口号

 5 years ago
source link: https://www.linuxprobe.com/linux-card.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
导读 SSH端口默认是22了,我们为了服务器的安全一般会把SSH端口22修改成其它的端口好了,下文小编来各位整理一篇SSH端口修改教程供各位参考.

vps一到手,第一件事就是更改ssh端口,使用一个不容易被拆解的端口号,防止被黑。

更改端口号是通过修改SSH的配置文件实现的,登录ssh后,输入:

vim /etc/ssh/sshd_config

向下找到#Port 22这段

进入vi插入模式,进行编辑

删除掉Port 22前面的#,然后下一行输入新的端口号如:Port 10000(这个你自己定,最大不能超过65535)

编辑好,先按ESC键,再输入 :wq  保存退出.

接着重新启动ssh,代码如下:

/etc/init.d/sshd restart

然后使用10000端口登录,看是否可以连接成功,连接成功后在删除22端口.

不过在这有一个问题,一般的linux服务器都有自带的防火墙iptables规则,把端口10000 在规则中设置备放行。

iptables -I INPUT  -p tcp --dport 10000 -j ACCEPT

参数 -I  是把上面那条规则插入到iptables规则的最前面,然后删除ssh默认的22端口,保存规则,重启防火墙,到此大功告成。

下面说一下,怎么删除iptables规则,有时候有些规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用–line-number查看出该条规则的行号,再通过行号删除,代码如下:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    DROP       all  --  192.168.1.3          0.0.0.0/0

比如删除第二条规则,输入 :

[root@test ~]# iptables -D INPUT 2

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK