【SRE笔记 2022.8.25 linux用户组及用户命令】

密码修改

passwd

  • 当前用户下输入passwd命令可直接修改当前用户密码
  • 超管修改用户密码 passwd + 用户名
  • 从标准输入获取信息 --stdin
[root@aaa ~]# echo 12 | passwd --stdin aaa
Changing password for user aaa.
passwd: all authentication tokens updated successfully.
You have new mail in /var/spool/mail/root
  • 批量设置密码 chpasswd
  • (查看文件内容时,按alt选中固定右下角,向左上延伸)**

批量修改 chpasswd

[root@aaa ~]# man chpasswd
NAME
       chpasswd - update passwords in batch mode    # 批量设置密码

SYNOPSIS
       chpasswd [options]

DESCRIPTION
       The chpasswd command reads a list of user name and password pairs from standard input
       and uses this information to update a group of existing users. Each line is of the
       format:

       user_name:password   #设置密码格式
  • 批量创建用户
[root@aaa ~]# for i in {01..10};do useradd user$i ;done
[root@aaa ~]# tail -n 10 /etc/passwd
user01:x:8003:8003::/home/user01:/bin/bash
user02:x:8004:8004::/home/user02:/bin/bash
user03:x:8005:8005::/home/user03:/bin/bash
user04:x:8006:8006::/home/user04:/bin/bash
user05:x:8007:8007::/home/user05:/bin/bash
user06:x:8008:8008::/home/user06:/bin/bash
user07:x:8009:8009::/home/user07:/bin/bash
user08:x:8010:8010::/home/user08:/bin/bash
user09:x:8011:8011::/home/user09:/bin/bash
user10:x:8012:8012::/home/user10:/bin/bash
  • 建立相关密码配置文件
user01:01
user02:02
user03:03
user04:04
user05:05
user06:06
user07:07
user08:08
user09:09
user10:10
  • 批量修改密码
[root@aaa ~]# cat userpass.txt | chpasswd   # 好用   chpasswd < userpass.txt
[root@aaa ~]# tail -n 5 /etc/shadow  
user06:$6$CLM.2/rSEj$ELi89Z0NmK.KHuOC/LsGB1YpEg5jNx2lGN9umfob1EYUf3tF8nXM/Xd2n2PECiiSp8FhQN/CJxiX03rKPRmZ4.:19229:0:99999:7:::
user07:$6$EZEe3Qnvr$gpPXC0fNYWaoABZjl0SbaV1tSzZoUM9t8Yp15fsWYnaGVzMpnhY/yyr.oqFWNTRgiAzR6T7SwNIqvtMHmDusx0:19229:0:99999:7:::
user08:$6$4hMTS/XpdtGRituq$XKvohm/iiMmhbfs9fZetcX9rnji.fKv0VA8.sNAb591Np/JpthOude0C0eG2PT9CFxGkVA1AH.2.1ftDqkYQK/:19229:0:99999:7:::
user09:$6$a4XZKXon$MxpCag5mCJ.25DUvnx2/bGDvEfJTNK7rDE2i3rfwjFbpFK4pBudRcmznsDiv3l6IlEs1R9hGbOI4epdkUiTh31:19229:0:99999:7:::
user10:$6$PjPIg/yCk$iavAbSHa5Fgj2of7ooYNdKYktB0STilyuhqhv6UtprKCXjruZob50ZZ4Ds/FBb3tYLaRwWZnVI0QUUW0w0i1c/:19229:0:99999:7:::
  • 修改成功

拓展

  • 批量生成密码文件,注意是单引号,不是反引。notepad++也可实现,alt+c生成序列。
[root@aaa ~]# seq -w 10 | sed -rn 's#(.*)#yonghu\1:\1#gp' >yonghu.txt
[root@aaa ~]# cat yonghu.txt 
yonghu01:01
yonghu02:02
yonghu03:03
yonghu04:04
yonghu05:05
yonghu06:06
yonghu07:07
yonghu08:08
yonghu09:09
yonghu10:10
  • 批量删除用户
root@aaa ~]# seq -w 10 | sed -rn `s#(.*)#userdel yonghu\1:\1#gp` | bash  用bash解释,因为输出内容时文本

env 显示环境变量

[root@aaa ~]# env | grep root
USER=root
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
HOME=/root
LOGNAME=root

查看和更改密码属性 chage

参数-l

[root@aaa ~]# chage -l aaa
Last password change					: Aug 25, 2022
Password expires					: never
Password inactive					: never
Account expires						: never
Minimum number of days between password change		: 0
Maximum number of days between password change		: 99999
Number of days of warning before password expires	: 7

参数-E 设定账户过期时间,类似useradd -e

[root@aaa ~]# chage -E "2022/08/29" aaa 
[root@aaa ~]# chage -l aaa
Last password change					: Aug 25, 2022
Password expires					: never
Password inactive					: never
Account expires						: Aug 29, 2022
Minimum number of days between password change		: 0
Maximum number of days between password change		: 99999
Number of days of warning before password expires	: 7
  • chage和passwd都可设置用户多少天内不能修改密码,过期多少天前提示,多少天后必须修改密码,过期多少天后禁止用户登录。
  • 关联文件 /etc/shadow。

用户组groupadd

  • groupadd + 组名 添加(关联文件 /etc/ggshaow、 /etcgoup)
  • groupdel + 组名 删除

切换用户及提权管理命令

su 切换用户

  • su - aaa # - 代表环境变量
  • 没加- 的问题如下
[root@aaa ~]# su  bbb
[bbb@aaa root]$ env |  grep bbb
USER=bbb
HOME=/home/bbb
LOGNAME=bbb
[bbb@aaa root]$ pwd       # 当前路径错误
/root

带 -

[root@aaa ~]# su - bbb
Last login: Thu Aug 25 18:15:26 CST 2022 on pts/0
[bbb@aaa ~]$ pwd
/home/bbb
  • 参数 c 不切换用户查看
[root@aaa ~]# su - bbb -c pwd
/home/bbb

sudo 提权

  • 无需root密码,有指定命令的权限,完成管理员的操作)(5分钟验证一次有限期,**时间戳文件,位置在/var/run/sudo/ts/下。
  • 配置文件 /etc/sudoers,通过visudo,管理该文件 不建议使用vim /etc/sudoers,该命令不检查语法。
[root@aaa ~]# ll /etc/sudoers
-r--r-----. 1 root root 4328 Sep 30  2020 /etc/sudoers
[root@aaa ~]# visudo # 查看第100行
100 root    ALL=(ALL)       ALL  # 用户  主机=(主机)切换的角色  执行命令(也可添加具体命令如/usr/sbin/useradd)。执行命令越小越具体越好。
101                                # 可在这里添加相关用户
  • 企业中常用跳板机,sudo精细化管理。

查看用户信息命令

who

[root@aaa ~]# who
root     pts/0        2022-08-25 16:57 (10.0.0.1)
[root@aaa ~]# whoami
root

w

[root@aaa ~]#  w
 19:16:24 up  2:18,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    10.0.0.1         16:57    0.00s  0.37s  0.03s 

id

[root@aaa ~]# id root
uid=0(root) gid=0(root) groups=0(root)

查看用户日志

last

[root@aaa ~]# last | head -5
root     pts/0        10.0.0.1         Thu Aug 25 16:57   still logged in   
reboot   system boot  3.10.0-1160.71.1 Thu Aug 25 16:57 - 19:18  (02:21)    
root     tty1                          Sat Aug 27 02:30 - 02:30  (00:00)    
root     pts/0        10.0.0.1         Sat Aug 27 01:22 - 01:45  (00:22)    
root     pts/0        10.0.0.1         Wed Aug 24 19:39 - 01:22 (2+05:42)   
[root@aaa ~]# date
Thu Aug 25 19:18:38 CST 2022

lastlog

root@aaa ~]# lastlog | head -5
Username         Port     From             Latest
root             pts/0                     Thu Aug 25 18:15:14 +0800 2022
bin                                        **Never logged in**
daemon                                     **Never logged in**
adm                                        **Never logged in**
  • 记录用户远程登录信息 /var/log/secure
[root@aaa ~]# tail -2 /var/log/secure 
Aug 25 18:19:13 aaa su: pam_unix(su-l:session): session opened for user bbb by root(uid=0)
Aug 25 18:19:13 aaa su: pam_unix(su-l:session): session closed for user bbb

更改文件属主、组 chown

  • 更改用户所属主、组 chown
    chown 用户.用户组 文件 参数-R 递归更改,更改文件里的所有
    chown 用户
    chown .用户组

chagrp更改属组

更改用户组 chgrp
chgrp 用户组

特殊属性

chattr

  • i 属性 不能修改,不能删除。
  • a 只能追加内容,不能删除。
[root@aaa ~]# ll  file1
[root@aaa ~]# chattr +i  file1
-rw-r--r--. 1 root root 0 Aug 27  2022 file1
[root@aaa ~]# chattr -i  file1

lsattr 查看chattr设置的属性。

[root@aaa ~]# touch file
[root@aaa ~]# lsattr file
---------------- file
[root@aaa ~]# chattr + i file
chattr: No such file or directory while trying to stat i
[root@aaa ~]# chattr +i file
[root@aaa ~]# lsattr file
----i----------- file
[root@aaa ~]# rm -rf file
rm: cannot remove ‘file’: Operation not permitted
  • 出于安全考虑加固用户及组相关文件/etc/passwd,/etc/shadow,/etc/gshadow,/etc/group,/etc/sudoers,或者直接修改命令,如下
[root@aaa bin]# cd /usr/bin/
[root@aaa bin]# move chattr aaa
-bash: move: command not found
[root@aaa bin]# mv chattr aaa
[root@aaa bin]# mv  aaa chattr

系统基础权限介绍

[root@aaa ~]# ll file
-rw-r--r--. 1 root root 0 Aug 25 22:12 file  # 9位权限组    时间是最近的修改时间(关于内容)。
  • 读 写 执行(x),其他用户(o)

文件和目录权限区别

1)针对文件

  • r 读取文件内容**(block里)**的权限。
  • w 可以增加、修改、删除文件内容**(block里)**的权限。
  • x 可执行文件 :本身可执行;可能需要r权限;root不受 r 权限管控。

2)针对目录

  • r 浏览目录下的内容,包含文件和子目录。
  • w 增删改目录下的文件和目录,如果没有x权限的配置,也无法删除和创建文件。
  • x 表示具有进入目录的权限,执行 cd 等命令。

权限设置chmod命令

  • 字符(数字)权限 r(4)w(2)x(1)-(0)
  • 更改权限 chmod -R 递归设置
[root@aaa ~]# chmod -R 755 hehe.sh 
[root@aaa ~]# chmod 755 hehe.sh 
[root@aaa ~]# chmod u/g/o +/- r/w/x  文件  # 语法
[root@aaa ~]# chmod u+x,g-w,o=r  文件

删除原理

  • 删除文件的权限和文件本身无关,权限只针对该文件的block,要想删除block,要先找inode,删除的该文件名放在上级block,删文件是在删除上级目录block,就要拥有上级目录权限,所以和文件本身权限没关系,需要上级目录的写(W)权限。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习使我清醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值