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

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

总结

以上所述是小编给大家介绍的centos 7 修改sshd | 禁止 root登录及sshd端口脚本定义,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

(0)

相关推荐

  • CentOS7如何修改SSH登录端口

    centos7修改ssh默认登录端口和centos6差不多,就是防火墙不一样,然后关闭selinux最好. [修改ssh默认22端口] vi /etc/ssh/sshd_config 在Port 22下面加一行,以端口50000为例,Port 50000 然后保存,重启ssh服务systemctl restart sshd.service [防火墙中放行新加入端口] firewall-cmd --permanent --add-port=50000/tcp 用该命令查询 firewall-cmd

  • Redhat 7/CentOS 7 SSH 免密登录的方法

    先决条件 3 台 CentOS 7 HOSTNAME IP ROLE server1 10.8.26.197 Master server2 10.8.26.196 Slave1 server3 10.8.26.195 Slave2 步骤 1. 用 root 用户登录.每台服务器都生成公钥,再合并到 authorized_keys. 2. CentOS 默认没有启动 ssh 无密登录,去掉 /etc/ssh/sshd_config 其中 2 行的注释,每台服务器都要设置. RSAAuthentic

  • CentOS 7 sshd 链接被拒绝问题解决办法

    1,服务器链接不上 ssh: connect to host XXXXX port 22: Connection refused 原因,centos7 修改了链接的端口变成 2200了. #Port 22 Port 2200 修改回去,或使用2200链接. 修改:vi /etc/ssh/sshd_config 2,centos7默认安装了防火墙不是iptables了 因为是本地测试,所以直接关闭防火墙,禁用开机启动. systemctl stop firewalld systemctl disa

  • centos7 设置grub密码及单用户登录实例代码

    centos7与centos6在设置grub密码的操作步骤上有很大的差别,特此记录供以后查用 grub加密的目的: 防止不法分子利用单用户模式修改root密码 给grub加密可以采用明文或者加密的密文两种,建议使用加密的密文方式,两者操作步骤上相差不多,本文以加密的密文为例 一.设置grub加密 1.使用grub2-mkpasswd-pbkdf2命令创建密文(一定的保存记住自己设置的密码) 2.在/etc/grub.d/00_header 文件末尾,添加以下内容  (root 为单用户登录使用的

  • Centos7下用户登录失败N次后锁定用户禁止登陆的方法

    前言 针对linux上的用户,如果用户连续3次登录失败,就锁定该用户,几分钟后该用户再自动解锁.Linux有一个pam_tally2.so的PAM模块,来限定用户的登录失败次数,如果次数达到设置的阈值,则锁定用户. PAM的配置文件介绍 PAM配置文件有两种写法: 一种是写在/etc/pam.conf文件中,但centos6之后的系统中,这个文件就没有了. 另一种写法是,将PAM配置文件放到/etc/pam.d/目录下,其规则内容都是不包含 service 部分的,即不包含服务名称,而/etc/

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

    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 s

  • Linux系统禁止root账号远程登录的命令

    ps:下面给大家介绍下Linux系统禁止root账号远程登录的方法,具体内容如下所述: 修改配置文件/etc/ssh/sshd_config,去掉PermitRootLogin前的注释,修改值为no,然后重启sshd服务即可 #LoginGraceTime 2m PermitRootLogin no #StrictModes yes MaxAuthTries 3 #MaxSessions 10 service sshd restart 执行该操作后root账号便无法远程登录,因此在此之前要先新建

  • linux下禁止root远程登录和添加新用户的方法

    一.添加和root权限一样的用户 1. adduser admin passwd  admin (修改密码) 然后输入密码   (密码简单了通不过) 系统提示输入确认密码后再输入一次.OK添加成功. 2.修改 /etc/sudoers 文件,找到下面一行,在root下面添加一行,如下所示: vim /etc/sudoers ## Allow root to run any commands anywhere root ALL=(ALL) ALL admin ALL=(ALL) ALL 这个文件只

  • CentOS新建用户并使能密钥登录的方法

    CentOS 默认只有一个 root 用户,但是 root 用户的权限过大,而且不利于多人协作,基于权限管理和安全的原因,我们为系统新建一个用户,并且使能其 SSH 登录,同时禁止 root 用户的登录: 基于CentOS Linux release 7.6.1810 (Core)实践: 新建用户 在 CentOS 中,adduser和useradd没有区别: [root@centos_7_6_1810 ~]# ll /usr/sbin/ | grep user lrwxrwxrwx 1 roo

  • Centos下 修改mysql密码的方法

    1.修改MySQL的登录设置: # vim /etc/my.cnf 加上一句:skip-grant-tables 如: [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock 2.重启mysql # service mysqld restart Stopping MySQL: [ OK ] Starting MySQL: [ OK ] 3.修改密码 mysql> USE mysql ; Database changed m

  • mysql 5.1版本修改密码及远程登录mysql数据库的方法

    mysql创建用户并授权: 格式:grant 权限 on 数据库名.表名 to 用户@登录主机 identified by "用户密码"; grant[英][grɑ:nt]   承认; 同意; 准许; 授予; 例1:允许mk用户从localhost登录 mysql> grant all on book.* to mk1@localhost identified by "123456"; #允许访问book数据库下的所有表, 只能访问book表,而且是同一个服务

  • linux下 root 登录 MySQL 报错的问题

    最近在centOS 7上,通过yum安装了mysql,安装成功后,使用root登录,出现了如下报错: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) mysql版本为:Ver 14.14 Distrib 5.7.10, for Linux(X86_64) using EditLine wrapper 然而实际上并没有进行过root密码设置(不知道是不是安装过程有被忽略的地方?).

  • 在Linux下修改和重置root密码的方法(超简单)

    刚开始接触Linux的人,忘记了root密码可能会不知所措.想找回自己的root密码,但是又不知道方法.其实,只需要简单的几步就可以重置自己的root密码了(找回密码我也不会) 1.开机之后在内核上敲击e,然后编辑选项 2.在linux16这一行,将红框内的内容改成rw rd.break 根据提示,使用组合键CTRL+X进入密码修改 3.进入到系统的紧急求援模式 4.依次输入以下的命令 # chroot /sysroot //进入系统的根目录 # passwd //重置密码 # touch /.

  • Laravel框架基于中间件实现禁止未登录用户访问页面功能示例

    本文实例讲述了Laravel框架基于中间件实现禁止未登录用户访问页面功能.分享给大家供大家参考,具体如下: 1.生成中间件 [root@localhost MRedis]# php artisan make:middleware CheckLogin Middleware created successfully. 2.实现中间件,在app\http\middleware\CheckLogin.php public function handle($request, Closure $next)

  • Asp.net mvc 权限过滤和单点登录(禁止重复登录)

    1.权限控制使用controller和 action来实现,权限方式有很多种,最近开发项目使用控制控制器方式实现代码如下 /// <summary> /// 用户权限控制 /// </summary> public class UserAuthorize : AuthorizeAttribute { /// <summary> /// 授权失败时呈现的视图 /// </summary> public string AuthorizationFailView

随机推荐