在我们运维工作中,会经常要求一些用户不允许登陆系统,以加固系统安全。今天这里介绍下锁定账号登陆的几种方法:

一、最常用方式,修改用户的shell类型为/sbin/nologin (推荐使用)

这种方式会更加人性化一点,因为不仅可以禁止用户登录,还可以在禁用登陆时给提示告诉它这么做的原因。

修改/etc/nologin.txt,没有的话就手动新建一个,在里面添加给被禁止用户的提示(这种方式的所有用户的锁定信息都在这个文件中,在登陆时给与提示)。

如下,禁用wangshibo账号登陆系统:

[root@host-192-168-1-117 ~]# useradd wangshibo

[root@host-192-168-1-117 ~]# echo "123456"|passwd --stdin wangshibo

Changing password for user wangshibo.

passwd: all authentication tokens updated successfully.

[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo //或者使用shell类型修改命令"usermod -s /sbin/nologin wangshibo"

wangshibo:x:500:500::/home/wangshibo:/bin/bash

[root@host-192-168-1-117 ~]# sed -i 's#/home/wangshibo:/bin/bash#/home/wangshibo:/sbin/nologin#g' /etc/passwd

[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo

wangshibo:x:500:500::/home/wangshibo:/sbin/nologin

[root@host-192-168-1-117 ~]# touch /etc/nologin.txt

[root@host-192-168-1-117 ~]# cat /etc/nologin.txt

In order to protect the system security, this type of user is locked!

现在尝试用wangshibo账号登陆系统,就会被拒绝,并给出提示信息:

[ops@host-192-168-1-117 ~]$ su - wangshibo

Password:

In order to protect the system security, this type of user is locked!

[ops@host-192-168-1-117 ~]$

解禁用户登陆就是把shell改为它原有的就可以了

[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo

wangshibo:x:500:500::/home/wangshibo:/sbin/nologin

[root@host-192-168-1-117 ~]# sed -i 's#/home/wangshibo:/sbin/nologin#/home/wangshibo:/bin/bash#g' /etc/passwd

[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo

wangshibo:x:500:500::/home/wangshibo:/bin/bash

[root@host-192-168-1-117 ~]# su - ops

[ops@host-192-168-1-117 ~]$ su - wangshibo

Password:

[wangshibo@host-192-168-1-117 ~]$

可以使用usermod命令修改用户的shell类型,加-s参数,如

[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo

wangshibo:x:500:500::/home/wangshibo:/bin/bash

[root@host-192-168-1-117 ~]# usermod wangshibo -s /sbin/nologin

[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo

wangshibo:x:500:500::/home/wangshibo:/sbin/nologin

另外注意下一个小细节:

这一种方法,无论是从root用户,还是从其他用户,都不能ssh登陆或su切换到锁定账号下

二、修改用户配置文件/etc/shadow,将第二栏设置为“*”

使用这种方式会导致该用户的密码丢失,要再次使用时,需重设密码。一般不推荐这种方式!

[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo

wangshibo:x:500:500::/home/wangshibo:/bin/bash

[root@host-192-168-1-117 ~]# cat /etc/shadow|grep wangshibo

wangshibo:$1$05NU4y2$OBGISa8yaloVNYVLFCoP3.:17133::::::

[root@host-192-168-1-117 ~]# cat /etc/shadow|grep wangshibo # 将第二栏密码设置为*

wangshibo:*:17133::::::

[root@host-192-168-1-117 ~]# su - ops

[ops@host-192-168-1-117 ~]$ su - wangshibo #不能登陆系统

Password:

su: incorrect password

解禁用户登陆,需要重置密码

[root@host-192-168-1-117 ~]# echo "123456"|passwd --stdin wangshibo

Changing password for user wangshibo.

passwd: all authentication tokens updated successfully.

[root@host-192-168-1-117 ~]# cat /etc/shadow|grep wangshibo

wangshibo:$1$RPfkekf7$QAUGmJ0SCIb64aEvJvNif1:17133::::::

[ops@host-192-168-1-117 ~]$ su - wangshibo

Password:

[wangshibo@host-192-168-1-117 ~]$

三、使用命令passwd

passwd -l 用户 //锁定账号,-l:lock

passwd -u 用户 //解禁用户,-u:unlock

[root@host-192-168-1-117 ~]# passwd -l wangshibo

Locking password for user wangshibo.

passwd: Success

[ops@host-192-168-1-117 ~]$ su - wangshibo

Password:

su: incorrect password

[root@host-192-168-1-117 ~]# passwd -u wangshibo

Unlocking password for user wangshibo.

passwd: Success

[ops@host-192-168-1-117 ~]$ su - wangshibo

Password:

[wangshibo@host-192-168-1-117 ~]$

四、使用命令usermod

usermod -L 用户 //锁定帐号,-L:lock

usermod -U 用户 //解锁帐号,-U:unlock

[root@host-192-168-1-117 ~]# usermod -L wangshibo

[ops@host-192-168-1-117 ~]$ su - wangshibo

Password:

su: incorrect password

[root@host-192-168-1-117 ~]# usermod -U wangshibo

[ops@host-192-168-1-117 ~]$ su - wangshibo

Password:

[wangshibo@host-192-168-1-117 ~]$

这里有个细节需要注意一下:

第三和第四种方式,即passwd或usermod命令锁定的用户:

1)无论从root用户还是其他普通用户,都不能ssh登陆锁定用户下

2)可以从root用户su切换到锁定用户下,但是用其他普通用户不能su切换到锁定用户下

五、禁止所有的用户登录(手动创建/etc/nologin文件)

如果不想让除root用户之外的其他所有用户登录系统(比如在系统维护情况下),如果按照上面的几种方式,就需要一个一个地去禁止用户登录,这就是一种很傻X的工作方式,效率也很低!

下面介绍一种简洁有效的设置方式:

只需要在/etc目录下建立一个nologin文档,那么Linux上的所有用户(除了root以外)都无法登录!!

[root@host-192-168-1-117 ~]# touch /etc/nologin

在/etc/nologin(注意:这可不是第一种方式中的nologin.txt)文件里面可以自定义一些内容,告诉用户为何无法登录。

[root@host-192-168-1-117 ~]# cat /etc/nologin

抱歉,系统维护中,暂时禁止登陆!

这样,就会发现除root之外的其他用户统统无法登陆系统了。

[root@linux-node2 ~]# ssh root@192.168.1.117

抱歉,系统维护中,暂时禁止登陆!

[root@host-192-168-1-117 ~]#

[root@linux-node2 ~]# ssh wangshibo@192.168.1.117

wangshibo@192.168.1.117's password:

抱歉,系统维护中,暂时禁止登陆!

Connection closed by 192.168.1.117

[root@linux-node2 ~]# ssh ops@192.168.1.117

ops@192.168.1.117's password:

抱歉,系统维护中,暂时禁止登陆!

Connection closed by 192.168.1.117

注意一点:

这种方法设置后,只是禁止了从外部ssh登陆本机时有效!但是在本机上,无论是从root用户还是其他普通用户使用su命令切换到锁定用户下都不受影响。

[root@host-192-168-1-117 ~]# su - ops

[ops@host-192-168-1-117 ~]$ su - wangshibo

Password:

[wangshibo@host-192-168-1-117 ~]$

解禁帐号也简单,直接将/etc/nologin删除就行了!

[root@host-192-168-1-117 ~]# rm -f /etc/nologin

[root@host-192-168-1-117 ~]# ll /etc/nologin

ls: cannot access /etc/nologin: No such file or directory

[root@linux-node2 ~]# ssh wangshibo@192.168.1.117

wangshibo@192.168.1.117's password:

[wangshibo@host-192-168-1-117 ~]$

以上几种锁定账号的设置完成后,在远程使用ssh命令都将无法登陆系统!

linux 阻止 复位命令,Linux下锁定账号,禁止登录系统的设置总结相关推荐

  1. linux 阻止 复位命令,linux防误删操作(使用safe-rm;使用mv命令删除文件)

    本文于2021年1月16日由AlvinCR更新 1.基于文本的Linux为什么没有回收站? 个人观点: 1.linux是指令行操作系统,不同于win的图形化界面,linux无法方便的快速恢复文件,例如 ...

  2. Linux下锁定账号,禁止登录系统的设置总结

    在我们运维工作中,会经常要求一些用户不允许登陆系统,以加固系统安全.今天这里介绍下锁定账号登陆的几种方法: 一.最常用方式,修改用户的shell类型为/sbin/nologin  (推荐使用) 这种方 ...

  3. linux新建自定义命令,Linux 创建自定义命令

    Linux 创建自定义命令 Linux 可以创建自定义使用命令 这里我们采取使用"alias"命令.这里我们首先了解两个文件,通过这两个文件我们可以根据环境配置相应的自定义命令. ...

  4. linux shell 未找到命令,未找到linux问题setenv命令(linux issue setenv command not found)

    未找到linux问题setenv命令(linux issue setenv command not found) 我在Linux中开发了一个Tcl / Tk脚本工具. 为了运行该工具,每次需要在she ...

  5. Linux火狐解压完运行不了,在Ubuntu系统下firefox账号无法登录的解决

    在Ubuntu 16.04系统下默认自带有firefox浏览器,但是使用这个firefox浏览器会发现账号无法登录,原来是在windows系统下的数据没有办法同步,书签也同步不了.经过查询资料后得知, ...

  6. linux查代替命令,Linux下查/删/替 命令(转)

    ▪查看某目录下所有文件的个数: [root@localhost1 opt]# ls -l |grep "^-"|wc -l ▪查看某目录下所有文件的个数,包括子目录里面的: [ro ...

  7. 红帽子linux改ip命令,Linux系统下图形界面更改IP地址

    1.打开终端的命令模式: 点击左上角的application 然后点击System Tools下拉菜单,再点击其展开的下拉菜单Terminal,这样就打开一个命令模式了. 2.查找所要使用的命令模块的 ...

  8. linux 下载python命令_Linux下修改Python命令的方法示例(附代码)

    本篇文章给大家带来的内容是关于Linux下修改Python命令的方法示例(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. Linux默认python命令指向的是/usr/bi ...

  9. linux 网络冲浪,命令行下的网络冲浪工具命令行浏览器介绍

    Linux命令行是强大的工具,命令行是我们的日常工作,命令行更是我们日常生活.之前虫虫给大家写过一些命令的的介绍,命令行的工具,命令行下的开发.实际上命令行也是我们不可或缺的生活.本文我们来介绍一下命 ...

最新文章

  1. R—计算系统发育多样性PD (Calculate Faith’s Phylogenetic Diversity)
  2. java 多态 降低耦合_java多态
  3. java.lang.NumberFormatException: For input string: “name”
  4. java电子商务源码解读 b2b2c o2o
  5. 2021年,作为算法工程师的你们会在CV业务上用Transformer吗?
  6. SpringMVC @ModelAttribute注解
  7. 3dmax高版本转低版本插件_Fundebug前端JavaScript插件更新至1.8.0,兼容低版本的Android浏览器...
  8. 鼠标和按键在android 上的识别和区别
  9. CentOS Mysql安装配置
  10. 二十一天学通C语言:C语言中指针排序
  11. oracle中使用java存贮过程
  12. 互联网晚报 | 8月22日 星期日 | 抖音回应腾讯《扫黑风暴》相关投诉;比亚迪半导体被中止上市审核;三星正式推出UPC技术...
  13. 能上QQ,无法打开网页解决办法!!!(亲测能用)
  14. Xposed环境安装
  15. NVIDIA显卡深度学习算力表
  16. mysql数据库算法_MySql联接算法
  17. word无法打开请去应用商店_抖音去水印 | HTTP Catcher方法全解析
  18. CentOS7开启Google TCP-BBR优化算法
  19. python输入名字配对情侣网名_输入名字制作情侣网名-网名搜索
  20. Error: 1307. Verify that you have sufficient privileges to modify the security permissions for this

热门文章

  1. Android Studio 最新汉化包下载及安装方法,持续更新 IDEA
  2. Foucsky演示工具使用教程入门(笔记)
  3. Linux克隆Mac地址一样,详解Linux系统中网卡MAC地址克隆方法
  4. 微分代数几何基础(1)
  5. python输入代码如何清除_如何清空python
  6. Unity游戏开发 怪物巡逻AI
  7. JDK8新特性(五):JDK8时间日期API
  8. SEO优化技巧,并不是词库排名优化操作那么简单!
  9. 微滤-超滤-反渗透膜系统“携手并进”处理造纸废水 效果惊人
  10. java web.xml taglib_java – 在web.xml中声明JSP taglib指令