Linux中怎么禁止用户登录
导读:本文共1530.5字符,通常情况下阅读需要5分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 创建用户时设置禁止shell登录默认情况下,创建用户时,将按照/etc/default/useradd文件中定义的为用户分配shell。Linux中附带了一个/sbin/nologinshell,当用户尝试连接时,它会显示一条消息“This account is current not available”。这是禁止用户登录shell的一种方法。下面是使用方式:... ...
目录
(为您整理了一些要点),点击可以直达。创建用户时设置禁止shell登录
默认情况下,创建用户时,将按照/etc/default/useradd
文件中定义的为用户分配shell。
Linux中附带了一个/sbin/nologin
shell,当用户尝试连接时,它会显示一条消息“This account is current not available”。这是禁止用户登录shell的一种方法。下面是使用方式:
useradd-s/sbin/nologin{username}
下面实例,创建一个用户,shell设置为/sbin/nologin
:
[root@localhost~]#useradduser01-s/sbin/nologin[root@localhost~]#tail-1/etc/passwduser01:x:1000:1000::/home/user01:/sbin/nologin
查看/etc/passwd
可以看到user01的shell为/sbin/nologin
给user01用户设置密码,然后ssh登录测试一下:
[root@localhost~]#echo'123'|passwd--stdinuser01Changingpasswordforuseruser01.passwd:allauthenticationtokensupdatedsuccessfully.[root@localhost~]#sshuser01@localhostuser01@localhost'spassword:Thisaccountiscurrentlynotavailable.Connectiontolocalhostclosed.
输入密码之后,提示This account is current not available,然后连接就关闭了。
为现有用户时设置禁止shell登录
更改现有用户的shell,可以使用usermod
和chsh
两个命令来修改:
chsh
命令使用语法如下:
chsh-s/sbin/nologin{username}
下面修改user02用户的shell:
#Centos8默认没有安装chsh,使用下面命令安装:[root@localhost~]#yum-yinstallutil-linux-user[root@localhost~]#chsh-s/sbin/nologinuser02Changingshellforuser02.chsh:Warning:"/sbin/nologin"isnotlistedin/etc/shells.Shellchanged.
usermod
命令使用语法如下:
usermod-s/sbin/nologin{username}
下面修改user03用户的shell:
[root@localhost~]#usermod-s/sbin/nologinuser03
也可以手动修改/etc/passwd
文件中的用户shell。
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
Linux中怎么禁止用户登录的详细内容,希望对您有所帮助,信息来源于网络。