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
4. 修改 #PermitRootLogin yes 为 PermitRootLogin no
- [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 ~]$
user 普通用户不能编辑 / etc/SSH/sshd_config, 需要切换 root 才能编辑, 切换 root 命令 : su
7.root 进行运行, 编写 shell 脚本.// 脚本通用 CentOS 7 (主要: sshd_config 端口号定义输入对 root 登录禁止)
- #!/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
运行测试 (可循环使用) 本次防火墙关闭 / 生产服务器都是关闭防火墙的
- [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
各路大神, 求带飞有什么建议敬请留言. QQ:861996013, 李辉 . 转载请注明, 谢谢.
来源: https://www.cnblogs.com/huixst/p/11450707.html