阅读 261

centos 7 修改sshd | 禁止 root登录及sshd端口脚本定义

这篇文章主要介绍了centos 7 修改sshd | 禁止 root登录及sshd端口脚本定义,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

1.新建用户wwweee000

1
2
3
4
5
6
7
[root@localhost ~]# useradd wwweee000
[root@localhost ~]# passwd wwweee000
Changing password for user wwweee000.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

2.#Port 22字段删掉,将22改为其他不被使用的端口,服务器端口最大可以开到65536.//注意Port是大写的"P"

1
2
3
4
5
6
7
8
9
[root@localhost ~]# cat -n /etc/ssh/sshd_config|grep "Port *"
  17 #Port 22
  100 #GatewayPorts no
[root@localhost ~]# cat /etc/ssh/sshd_config|grep -n "Port *"
17:#Port 22
100:#GatewayPorts no
[root@localhost ~]# awk "/Port */" /etc/ssh/sshd_config
#Port 22
#GatewayPorts no

上面还是不能满足输出结果:我们只要Port 22的答案.

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# cat /etc/ssh/sshd_config|grep -n "Port\ "     
17:#Port 22
[root@localhost ~]# cat -n /etc/ssh/sshd_config|grep "Port\ "     
  17 #Port 22
[root@localhost ~]# cat /etc/ssh/sshd_config|grep -n "Port\ "     
17:#Port 22
[root@localhost ~]# awk "/Port\ /" /etc/ssh/sshd_config        
#Port 22
[root@localhost ~]# sed -i "17s/#Port 22/Port 22/g" /etc/ssh/sshd_config
[root@localhost ~]# awk "/Port\ /" /etc/ssh/sshd_config        
Port 22

-n显示行号;\反斜杠实质定义

3.不使用vi/vim进行修改Port 22 为 4096

1
2
3
[root@localhost ~]# sed -i "17s/Port 22/Port 4096/g" /etc/ssh/sshd_config
[root@localhost ~]# cat /etc/ssh/sshd_config|grep "Port\ "
Port 4096

4.修改 #PermitRootLogin yes 为 PermitRootLogin no

1
2
3
4
5
6
[root@localhost ~]# cat -n /etc/ssh/sshd_config|grep "PermitRootLogin"
  49 #PermitRootLogin yes
  104 # the setting of "PermitRootLogin without-password".
[root@localhost ~]# sed -i "49s/#PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config
[root@localhost ~]# cat -n /etc/ssh/sshd_config|grep "PermitRootLogin no"
  49 PermitRootLogin no

5.重启sshd服务

1
[root@localhost ~]# systemctl restart sshd.service

6.使用 wwweee000 用户登录变化

[wwweee000@localhost ~]$

user普通用户不能编辑/etc/ssh/sshd_config,需要切换root才能编辑,切换root命令 :  su

7.root进行运行,编写shell脚本。//脚本通用centos 7 (主要:sshd_config端口号定义输入对root登录禁止)

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
sshd_Port=`cat /etc/ssh/sshd_config|grep "Port\ "`
echo "  当前sshd端口:$sshd_Port"
read -ep "  请输入sshd服务的连接端口Port (1-65536);
  请确保其他端口冲突和防火墙端口开放业务:" sshd_Port_read
echo "  你输入的端口号:$sshd_Port_read"
sed -i "s/$sshd_Port/Port $sshd_Port_read/g" /etc/ssh/sshd_config
echo "  已经设置端口为:`cat /etc/ssh/sshd_config|grep "Port\ "`"
sed -i "s/#PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config
systemctl restart sshd.service
exit 0

运行测试(可循环使用)本次防火墙关闭/生产服务器都是关闭防火墙的

1
2
3
4
5
6
7
8
[root@localhost wwweee000]# firewall-cmd --state
not running
[root@localhost ~]# sh sshd_config_Port.sh
  当前sshd端口:Port 22
  请输入sshd服务的连接端口Port (1-65536)
  请确保其他端口冲突和防火墙端口开放业务:4096
  你输入的端口号:4096
  已经设置端口为:Port 4096

总结

以上所述是小编给大家介绍的centos 7 修改sshd | 禁止 root登录及sshd端口脚本定义,希望对大家有所帮助



文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐