30

Linux下4种禁用Root登陆的方法,你掌握了哪几种呢?

 4 years ago
source link: https://www.tuicool.com/articles/fARjYbE
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.

我们都知道 Linux 下 Root 用户的权限是最大的,因此一般不推荐直接使用 Root 用户操作。通常都是使用普通用户,在必要时通过 Sudo 命令来提权。

在 Ubuntu 中,更是直接把 Root 用户直接禁用了。那么如何在 Linux 中禁止Root 登陆呢?今天,我们就来介绍几种常用的方法。

1. 修改 Root 用户的登陆 Shell

我们可以通过编辑 /etc/passwd 文件将 Root 用户的登陆 Shell 改为 /sbin/nologin。

$ sudo vim /etc/passwd  
root:x:0:0:root:/root:/bin/bash  
to  
root:x:0:0:root:/root:/sbin/nologin 

AnaqAzY.png!web

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

这种方法只能禁止那些需要 Shell 用户的登陆场景,但像 Sudo、FTP 之类的操作依然可以使用 Root 账户。

2. 禁止 Root 用户在任何终端设备上登陆

Linux 下 PAM 模块中有一个叫做 pam_securetty 的模块,它可以限定 Root 用户只能在安全终端上登陆。

而所谓的安全终端由 /etc/securetty 来决定,该文件由包含数行 tty 设备名(每行一个设备名,前面不加 /dev/ ),只有这些设备名才可允许 Root 用户登录。

因此,我们可以通过清空安全终端列表的方式来禁止 Root 用户登陆。

$ sudo mv /etc/securetty /etc/securetty.orig  
$ sudo touch /etc/securetty  
$ sudo chmod 600 /etc/securetty 

这种方法只会影响需要分配终端的程序,比如 login,gdm/kdm/xdm 之类的显示管理器等, 像 Su、Sudo、SCP、SFTP、SSH 之类的操作则无效。

3. 禁止 Root 用户通过 SSH 登陆

修改 SSH 的配置文件 /etc/ssh/sshd_config,在其中加上 PermitRootLogin no 配置项。

ARbiaiz.png!web

然后,再重启 SSHD 服务让配置生效。

$ sudo systemctl restart sshd  
OR  
$ sudo service sshd restart 

很明显,这种方法只会影响 SSH、SCP、SFTP 之类的 SSH 系列工具。

4. 通过 PAM 模块禁止 Root 用户访问 Login 和 SSHD 服务

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

  •     创建一个需要禁止访问用户的文件,如:/etc/deniedusers。其中加上 Root 或任意不想让它访问服务的用户名。 
$ sudo echo root |tee -a /etc/deniedusers   
  •     设置这个文件的权限。 
$ sudo chmod 600 /etc/ssh/deniedusers 
  •     在 /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