7

禁用ROOT的4种方法

 3 years ago
source link: https://www.lujun9972.win/blog/2018/12/21/%E7%A6%81%E7%94%A8root%E7%9A%844%E7%A7%8D%E6%96%B9%E6%B3%95/index.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.

禁用ROOT的4种方法

https://www.tecmint.com/disable-root-login-in-linux/ 中看到的,记录一下。

我们都知道 root 的权限过大,因此一般不推荐直接使用 root 用户操作,而是使用普通用户,在必要时通过 sudo 命令来提权。 在Ubuntu中,更是直接把root禁用了,那么如何在linux中禁止root登陆呢?有下面几种方法。

修改root用户的登陆shell

可以通过下面将root的登陆shell改为 /sbin/nologin

sudo sed -i '/^root:/ s?[^:]*$?/sbin/nologin?' /etc/passwd

此后,使用 root 登陆只会显示一条信息 “This account is currently not available”. 也可以通过修改 /etc/nologin.txt 来自定义出错信息

这种方法只能禁止那些需要shell的用户登陆场景,像sudo,ftp之类的操作依然可以使用root账户.

禁止root在任何终端设备上登陆

PAM模块中有一个叫做 pam_securetty 的模块,它会限定 root 用户只能在安全终端上登陆。 而所谓的安全终端由 /etc/securetty 来决定,该文件由包含数行tty设备名(每行一个设备名,前面不加 /dev/),只有这些设备方允许 root 登录.

所以我们可以通过清空安全终端列表的方式禁止root登陆

sudo cp /etc/securetty /etc/securetty.bak
sudo echo |tee /etc/securetty

这种方法只会影响需要分配终端的程序,比如login,gdm/kdm/xdm之类的显示管理器等, 像su,sudo,scp,sftp,ssh之类的操作则无效

禁止root通过SSH登陆

修改 /etc/ssh/sshd_config,加上 PermitRootLogin no,然后再重启 sshd 服务

sudo systemctl restart sshd

很明显,这种方法只会影响ssh,scp,sftp之类ssh系列工具

通过PAM禁止root访问login和sshd服务

通过 /lib/security/pam_listfile.so 模块,可以限定指定用户禁止访问某些服务。其步骤如下:

  1. 创建一个 /etc/deniedusers,其中加上 root 或任意不想让它访问服务的用户名

    sudo echo root |tee -a /etc/deniedusers
    
  2. sudo chmod 600 /etc/ssh/deniedusers
    
  3. /etc/pam.d/login/etc/pam.d/sshd 中加上下面配置

    auth    required       pam_listfile.so \
           onerr=succeed  item=user  sense=deny  file=/etc/ssh/deniedusers
    

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK