linux 禁止其他用户登录,在Linux中如何禁止用户登录

给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

d5d0970219ba58fe98665e5704eb732f.png

[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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值