默認情況下,Linux中創建用戶帳戶時,用戶具有shell訪問權限。在某些情況下不需要用戶帳戶登錄shell。本文介紹如何設置已存在的用戶禁止shell登錄、創建用戶時禁止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 ~]# useradd user01 -s /sbin/nologin [root@localhost ~]# tail -1 /etc/passwd user01:x:1000:1000::/home/user01:/sbin/nologin
查看/etc/passwd
可以看到user01的shell為/sbin/nologin
給user01用戶設置密碼,然后ssh登錄測試一下:
[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.
輸入密碼之后,提示This account is current not available,然后連接就關閉了。
為現有用戶時設置禁止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://mp.weixin.qq.com/s/vuFygVagKLuVeYrqGlRo4w