[Linux] Linux用户管理(复习笔记)

@Linux用户和用户组

Linux系统中的用户分三类:普通用户、根用户/超级用户、系统用户(例如apache,mysql)

ps aux 的第一列是进程的USER

ls加上-n,第三列和第四列显示UID和GID

/etc/passwd 第三列和第四列是用户的UID和GID

id 查看当前用户的UID和GID

groups 查看当前用户的用户组

who 查看当前在线用户

[root@xxx ~]# touch test1225
[root@xxx ~]# ls -ln test1225
-rw-r--r--. 1 0 0 0 Dec 25 22:09 test1225
[root@xxx ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@xxx ~]# groups
root
[root@xxx ~]#

@冻结用户

冻结账号 usermod -L <user> ,解冻账号 usermod -U <user>

[root@xxx ~]# grep test /etc/shadow
test:$6$2.oruiRV$lyeBXNMG1Oe45xw2MlV7GYsdXYbTxI/zHjHB2zWS8eRZhCWrhtZQH4HV86jdPUPhHxquPWkCofNbibjANMmic0:18621:0:99999:7:::
[root@xxx ~]# usermod -L test
[root@xxx ~]# grep test /etc/shadow
test:!$6$2.oruiRV$lyeBXNMG1Oe45xw2MlV7GYsdXYbTxI/zHjHB2zWS8eRZhCWrhtZQH4HV86jdPUPhHxquPWkCofNbibjANMmic0:18621:0:99999:7:::
[root@xxx ~]# ssh test@localhost
test@localhost's password:
Permission denied, please try again.
test@localhost's password:
Permission denied, please try again.
test@localhost's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@xxx ~]# usermod -U test
[root@xxx ~]# ssh test@localhost
test@localhost's password:
Last failed login: Fri Dec 25 22:19:55 CST 2020 from localhost on ssh:notty
There were 3 failed login attempts since the last successful login.
Last login: Fri Dec 25 22:18:40 2020 from xxx
[test@xxx ~]$

@w命令

第一行会显示当前时间、系统运行时间、已登录的用户数量和系统负载,之后的列如下,
第一列:登录用户的用户名。
第二列:用户登录终端。
第三列:如果用户从网络登录,则显示远程主机的主机名或IP地址。
第四列:用户登录时间。
第五列:用户闲置时间。
第六列:与终端相关的当前所有运行进程消耗的CPU时间总量。
第七列:当前WHAT列所对应的进程所消耗的CPU时间总量。
第八列:用户当前运行的进程。

[root@xxx ~]# w
 22:21:33 up 10 days, 21:51,  2 users,  load average: 0.24, 0.22, 0.13
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       :0               15Dec20 ?xdm?   2days  3.72s /usr/libexec/gnome-session-binar
root     pts/0    192.168.160.248  18:23    5.00s  0.25s  0.03s w
[root@xxx ~]#

@sudo命令

sudo并不是真的切换了用户,而是使用root的身份和权限执行了命令。

普通用户运行sudo命令无需输入root用户的密码,系统首先检查/etc/sudoers,判断该用户是否有执行sudo的权限,在确定有执行权限后,系统要求用户输自己的密码,如果密码输入正确,则会以root用户的身份运行passwd user1命令。

使用visudo(会检查语法)编辑/etc/sudoers
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

执行sudo不输入当前普通用户的密码可以这样设置
test    ALL=(ALL)       NOPASSWD:ALL

将最后一个参数设置为ALL很不安全,在工作中可以根据实际情况定义用户可以sudo执行的命令列表
eg:用户test由于工作需要,经常要重启或者关闭服务器,那么就可以进行如下设置
test    ALL=(ALL)       NOPASSWD:/sbin/shutdown,/usr/bin/reboot

参考书籍:《Linux系统命令及Shell脚本实践指南》王军 著,第2章 Linux用户管理

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值