给user01用户设置密码,然后ssh登录测试一下:
输入密码之后,提示This account is current not available,然后连接就关闭了。
命令使用语法如下:
也可以手动修改文件中的用户shell。

默认情况下,Linux中创建用户帐户时,用户具有shell访问权限。在某些情况下不需要用户帐户登录shell。本文介绍如何设置已存在的用户禁止shell登录、创建用户时禁止shell登录。

创建用户时设置禁止shell登录

默认情况下,创建用户时,将按照文件中定义的为用户分配shell。

/etc/default/useradd

Linux中附带了一个shell,当用户尝试连接时,它会显示一条消息“This account is current not available”。这是禁止用户登录shell的一种方法。下面是使用方式:

/sbin/nologin

useradd -s /sbin/nologin {username}

下面实例,创建一个用户,shell设置为:

/sbin/nologin

[root@localhost ~]# useradd user01 -s /sbin/nologin
[root@localhost ~]# tail -1 /etc/passwd
user01:x:1000:1000::/home/user01:/sbin/nologin

查看可以看到user01的shell为

/etc/passwd

/sbin/nologin

打开网易新闻 查看精彩图片

[root@localhost ~]# echo '123'|passwd --stdin user01
Changing password for user user01.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# ssh user01@localhost
user01@localhost's password:
This account is currently not available.
Connection to localhost closed.

为现有用户时设置禁止shell登录

更改现有用户的shell,可以使用和两个命令来修改:

usermod

chsh

命令使用语法如下:

chsh

chsh -s /sbin/nologin {username}

下面修改user02用户的shell:

# Centos8默认没有安装chsh,使用下面命令安装:
[root@localhost ~]# yum -y install util-linux-user
[root@localhost ~]# chsh -s /sbin/nologin user02
Changing shell for user02.
chsh: Warning: "/sbin/nologin" is not listed in /etc/shells.
Shell changed.

usermod

usermod -s /sbin/nologin {username}

下面修改user03用户的shell:

[root@localhost ~]# usermod -s /sbin/nologin user03

/etc/passwd

总结

在本教程中讲述了如何禁止用户访问默认Shell。

本文原创地址:https://www.linuxprobe.com/forbidden-user-login.html