useradd
创建用户,更改用户信息
1.工作原理流程
使用此命令式,若不加任何参数选项,直接跟用户名,那么系统会首先读取/etc/login.defs(用户定义文件)和/etc/default/useradd(用户默认配置文件)文件中所定义的参数和规则,然后进行添加用户的操作,同时还会读取/etc/passwd(用户文件)和/etc/group(组文件),添加新用户和用户组信息,再向/etc/shadow(用户密码文件)和/etc/gshadow(组密码文件)添加对应的密码信息。同时根据/etc/default/useradd定义的信息简历家目录,并将/etc/skel中的所有文件复制到新用户的家目录中。
2.useradd加-D参数:改变/etc/default/useradd定义的信息,也可以手动修改
例如,用户账号的停止日期,过期日期,使用的shell名称等,具体可参考配置文件
3.useradd不加-D参数:
-c comment # 用户的说明栏
-d # 新用户家目录
-e # 用户终止日期
-f # 用户过期几日后永久停权,0时,用户立即被停权,-1时,关闭此功能
-g # 指定用户的用户组
-G # 定义用户多个不同组的成员
-m # 用户目录若是不存在,自动简历
-M # 布简历用户家目录,一般创建虚拟用户使用
-s # 指定用户的shell程序
-u # 用户的uid值设定
[[email protected] ~]# useradd ett [[email protected] ~]# ls -ld /home/ett/ drwx------ 2 ett ett 62 Apr 18 15:52 /home/ett/ [[email protected] ~]# grep -w ett /etc/passwd ett:x:1000:1000::/home/ett:/bin/bash [[email protected] ~]# grep -w ett /etc/shadow ett:!!:18004:0:99999:7::: [[email protected] ~]# grep -w ett /etc/group ett:x:1000: [[email protected] ~]# grep -w ett /etc/gshadow ett:!:: [[email protected] ~]# useradd -u 901 -g sa xiaolizi useradd: group ‘sa‘ does not exist [[email protected] ~]# groupadd sa [[email protected] ~]# useradd -u 901 -g sa xiaolizi [[email protected] ~]# id xiaolizi uid=901(xiaolizi) gid=1001(sa) groups=1001(sa) [[email protected] ~]# useradd -M -s /sbin/nologin xiaolizi2 [[email protected] ~]# grep -w xiaolizi2 /etc/passwd xiaolizi2:x:1001:1002::/home/xiaolizi2:/sbin/nologin
usermod
用于修改系统已经存在的用户的账号信息
用法参数与useradd类似
userdel
删除用户 # 工作中及你浪不要随意删除用户,请在/etc/passed里面采用注释的方式
-f # 强制删除用户,即使用户当前已登录
-r # 删除用户的同事,删除与用户相关的所有文件
[[email protected] ~]# tail -3 /etc/passwd ett:x:1000:1000::/home/ett:/bin/bash xiaolizi:x:901:1001::/home/xiaolizi:/bin/bash xiaolizi2:x:1001:1002::/home/xiaolizi2:/sbin/nologin [[email protected] ~]# userdel -r xiaolizi [[email protected] ~]# userdel -r xiaolizi2 userdel: xiaolizi2 home directory (/home/xiaolizi2) not found [[email protected] ~]# tail -3 /etc/passwd postfix:x:89:89::/var/spool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin ett:x:1000:1000::/home/ett:/bin/bash [[email protected] ~]# id ett uid=1000(ett) gid=1000(ett) groups=1000(ett) [[email protected] ~]# vim /etc/passwd # 将用户注释掉 [[email protected] ~]# tail -3 /etc/passwd postfix:x:89:89::/var/spool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin #ett:x:1000:1000::/home/ett:/bin/bash [[email protected] ~]# id ett id: ett: no such user [[email protected] tmp]# cat /tmp/xiaolizi.log |awk ‘{print $1}‘|sed -r ‘s#(.*)#"userdel -r \1";echo -e "`userdel -r \1`">/tmp/userdel.log#g‘|bash
groupadd
用于创建新的用户组,用途一般不多,主要是因为useradd创建用户时会默认创建用户组
-g gid # 指定用户组的gid,从500开始
-f # 新增一个账户,强制覆盖一个已经存在的组账号
groupdel
删除用户组
后面直接跟用户组的名称即可
passwd
修改用户密码及密码过期时间等内容
-k # 为密码已经过期的用户更新有效期
-l # 锁定用户,被多订的用户不能登录,,只有root权限才能设置
--stdin # 从标准输入读取密码字符串,免交互
-u # 解除对用户的锁定,只有root权限才能设置
-d # 删除用户密码,使密码为空,仅root用户有权使用该选项
-e # 使用户密码立即过期,在用户下次登录时强制要求用户修改密码
-n # 设置修改密码最短天数
-x # 设置修改密码最长天数
-w # 设置用户在密码过期前收到警告信息天数
-i # 设置密码过期后多少天后禁用账户
-S # 显示用户密码相关的简单描述
[[email protected] ~]# passwd # 在当前用户下修改密码 Changing password for user root. New password: BAD PASSWORD: The password is shorter than 8 characters # 设置密码过于简单会警告,不影响 Retype new password: passwd: all authentication tokens updated successfully. # 成功设置密码 [[email protected] ~]# useradd xiaolizi01 # 创建个新用户 [[email protected] ~]# passwd xiaolizi01 Changing password for user xiaolizi01. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. [[email protected] ~]# passwd -S xiaolizi01 # 显示账号密码信息 xiaolizi01 PS 2019-04-18 0 99999 7 -1 (Password set, SHA512 crypt.) [[email protected] ~]# echo "123456"|passwd --stdin xiaolizi01 # 免交互式设置密码 Changing password for user xiaolizi01. passwd: all authentication tokens updated successfully. [[email protected] ~]# passwd -n 7 -x 60 -w 10 -i 30 xiaolizi01 # 设置用户7天内不能更改密码,60天以后必须修改密码,过期前10天通知用户,过期后30天禁止用户登录 Adjusting aging data for user xiaolizi01. passwd: Success [[email protected] ~]# chage -l xiaolizi01 # 查看用户设置的列表 Last password change : Apr 18, 2019 Password expires : Jun 17, 2019 Password inactive : Jul 17, 2019 Account expires : never Minimum number of days between password change : 7 -n Maximum number of days between password change : 60 -x Number of days of warning before password expires : 10 -w
# 批量创建用户,随机设置8位密码--------,拼凑命令,交给bash执行 [[email protected] tmp]# echo stu{01..10}|tr " " "\n"|sed -r ‘s#(.*)#useradd \1;pass=$((RANDOM+10000000));echo "$pass"|passwd --stdin \1;echo -e "\1 `echo "$pass"`">>/tmp/xiaolizi.log#g‘ |bash Changing password for user stu01. passwd: all authentication tokens updated successfully. Changing password for user stu02. passwd: all authentication tokens updated successfully. Changing password for user stu03. passwd: all authentication tokens updated successfully. Changing password for user stu04. passwd: all authentication tokens updated successfully. Changing password for user stu05. passwd: all authentication tokens updated successfully. Changing password for user stu06. passwd: all authentication tokens updated successfully. Changing password for user stu07. passwd: all authentication tokens updated successfully. Changing password for user stu08. passwd: all authentication tokens updated successfully. Changing password for user stu09. passwd: all authentication tokens updated successfully. Changing password for user stu10. passwd: all authentication tokens updated successfully. [[email protected] tmp]# cat /tmp/xiaolizi.log stu01 10016083 stu02 10012250 stu03 10018657 stu04 10015755 stu05 10010848 stu06 10016321 stu07 10011326 stu08 10022713 stu09 10028841 stu10 10001627
chage
查看或修改用户密码有效期,有些参数用法与passwd相同
-l # 显示账号有效期的信息
-M -W -X相当于passwd里面对应的小写字母作用
chpasswd
用于从标准输入中读取一定格式的用户名,密码来批量更新用户密码,其格式为“用户名:密码”
[[email protected] tmp]# echo stu{01..10}:$((RANDOM+10000000))|tr " " "\n" >/tmp/chpasswd.xt [[email protected] tmp]# cat /tmp/chpasswd.xt stu01:10027593 stu02:10031643 stu03:10014680 stu04:10030420 stu05:10002831 stu06:10010814 stu07:10026730 stu08:10015381 stu09:10006139 stu10:10008308 [[email protected] tmp]# chpasswd </tmp/chpasswd.xt [[email protected] tmp]# su - stu01 [[email protected] ~]$ su - stu02 Password: [[email protected] ~]$ whoami stu02
su
从当前用户切换到指定用户或者以指定用户的身份执行命令或程序
-c # 向shell传递单个命令
- # 切换用户是,将用户的家目录,系统环境等重新按切换后的用户初始化
[[email protected] ~]$ whoami stu02 [[email protected] ~]$ su - root Password: Last login: Thu Apr 18 16:40:08 CST 2019 from 10.0.0.1 on pts/2 [[email protected] ~]# whoami root # 使系统在每一次开机时都能自动以普通用户启动指定的服务脚本 [[email protected] ~]# echo "su - oldboy -c ‘/bin/sh /service/scripts/deploy.sh‘">> /etc/rc.local
visudo
此命令专门用来编辑/etc/sudoers这个文件的,同时提供语法检查,是sudo命令的配置文件
-c # 手动执行语法检查
一般用于授权用户权限的
## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALL
sudo
可以让普通用户在执行指定的命令或层序上,拥有超级用户的权限,进行分类,并有针对性的命令授予指定的普通用户
-l # 列出当前用户可执行的命令
-u # 以指定用户的身份执行命令
id
显示指定用户真实有效的UID和GID等信息
[[email protected] tmp]# id root uid=0(root) gid=0(root) groups=0(root) [[email protected] tmp]# id xiaolizi01 uid=1001(xiaolizi01) gid=1002(xiaolizi01) groups=1002(xiaolizi01)
w
显示已经登录系统的用户,并显示用户正在执行命令
-h # 不显示前两行标题信息
-u # 忽略执行程序的名称,以及CPU时间的信息
-s # 使用短输出格式
[[email protected] tmp]# w 18:03:13 up 4:02, 3 users, load average: 0.00, 0.01, 0.05 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT root tty1 13:45 4:17m 0.02s 0.02s -bash root pts/1 10.0.0.1 15:45 1:43m 0.29s 0.02s -bash root pts/2 10.0.0.1 16:40 1.00s 0.12s 0.00s w [[email protected] tmp]# w -h root tty1 13:45 4:19m 0.02s 0.02s -bash root pts/1 10.0.0.1 15:45 1:44m 0.29s 0.02s -bash root pts/2 10.0.0.1 16:40 3.00s 0.12s 0.00s w -h [[email protected] tmp]#
who
显示已登录用户的信息,w的精简版
-a # 显示所有信息
-b # 显示系统的启动时间
-d # 显示已死的进程
-H # 显示标题,默认不显示
-l # 显示登录进程
[[email protected] tmp]# who root tty1 2019-04-18 13:45 root pts/1 2019-04-18 15:45 (10.0.0.1) root pts/2 2019-04-18 16:40 (10.0.0.1) [[email protected] tmp]# who -b system boot 2019-04-18 13:44 [[email protected] tmp]# who -d pts/0 2019-04-18 17:57 1679 id=ts/0 term= 0 exit=0 [[email protected] tmp]# who -l [[email protected] tmp]# who -H NAME LINE TIME COMMENT root tty1 2019-04-18 13:45 root pts/1 2019-04-18 15:45 (10.0.0.1) root pts/2 2019-04-18 16:40 (10.0.0.1) [[email protected] tmp]# who -H -a NAME LINE TIME IDLE PID COMMENT EXIT system boot 2019-04-18 13:44 root + tty1 2019-04-18 13:45 04:22 911 run-level 3 2019-04-18 13:45 pts/0 2019-04-18 17:57 1679 id=ts/0 term=0 exit=0 root + pts/1 2019-04-18 15:45 01:47 16283 (10.0.0.1) root + pts/2 2019-04-18 16:40 . 16574 (10.0.0.1) [[email protected] tmp]#
whoami
显示当前登录的用户名
last
从日志文件/var/log/wtmp读取信息并显示用户最近的登录列表
-n num # 指定显示结果的行数
[[email protected] ~]# ll /var/log/wtmp -rw-rw-r--. 1 root utmp 29568 Apr 18 17:57 /var/log/wtmp [[email protected] ~]# less /var/log/wtmp "/var/log/wtmp" may be a binary file. See it anyway? [[email protected] ~]# last root pts/2 10.0.0.1 Thu Apr 18 16:40 still logged in root pts/1 10.0.0.1 Thu Apr 18 15:45 still logged in
lastb
从/var/log/btmp中读取信息,并显示登录失败的记录,用于发现系统异常登录
[[email protected] ~]# lastb root tty1 Sat Oct 27 00:26 - 00:26 (00:00) (unknown tty1 Fri Oct 26 23:35 - 23:35 (00:00) btmp begins Fri Oct 26 23:35:31 2018
lastlog
显示所有用户的最近登录记录,异常记录,从/var/log/lastlog中读取信息
[[email protected] ~]# lastlog Username Port From Latest root pts/2 Thu Apr 18 17:16:05 +0800 2019 bin **Never logged in** daemon **Never logged in** adm **Never logged in** lp **Never logged in** sync **Never logged in** shutdown **Never logged in** halt **Never logged in** mail **Never logged in** operator **Never logged in** games **Never logged in** ftp **Never logged in** nobody **Never logged in** systemd-network **Never logged in** dbus **Never logged in** polkitd **Never logged in** tss **Never logged in** abrt **Never logged in** sshd **Never logged in** postfix **Never logged in** chrony **Never logged in** xiaolizi01 pts/1 Thu Apr 18 16:17:20 +0800 2019
原文地址:https://www.cnblogs.com/flashfish/p/10731365.html