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

匿名 (未验证) 提交于 2019-12-02 23:57:01

1.新建用户wwweee000

[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"

[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的答案.

[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

[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

[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服务

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

6.使用 wwweee000 用户登录变化

[wwweee000@localhost ~]$ 

7.root进行运行(编写shell脚本)

#!/bin/bash sshd_Prot=`cat /etc/ssh/sshd_config|grep "Port\ "` echo 当前sshd端口:$sshd_Prot read -ep "请输入sshd服务的连接端口Prot (1-65536)请确保其他端口冲突和防火墙端口开放业务:" sshd_Prot_read echo " 你输入的端口号:$sshd_Prot_read" sed -i "s/$sshd_Prot/Port $sshd_Prot_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

运行

[root@localhost]# sh prot.sh  当前sshd端口:Port 22 请输入sshd服务的连接端口Prot (1-65536)请确保其他端口冲突和防火墙端口开放业务:4096  你输入的端口号:4096 已经设置端口为:Port 4096

各路大神,求带飞有什么已经请留言。QQ:861996013,李辉 。转载请注明,谢谢。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!