咋日内容:
1.vim编辑工具
普通模式
编辑模式
末行模式
视图模式(shift +v、ctrl+v)
今日内容:
1.什么是用户?
用户指的是能够正常登录Linux或Windows系统
windows 系统不允许同一时刻多个用户同时登陆但是 linux系统则允许同一时刻登录多个用户同时操作互相之间不影响
2.为什么要创建用户?
系统上的每一个进程(运行的程序),都需要一个特定的用户运行
通常在公司是使用普通用户管理服务器,因为root权限过大,容易造成故障。
3.如何查看当前用户的详情?
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n99" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">查看当前所登录的用户
[root@oldboy ~]# id
uid=0(root) gid=0(root) 组=0(root)
查看其它所登录的用户
[root@oldboy ~]# id bgx
uid=5001(bgx) gid=1000(student) 组=1000(student),1001(sa)
每个进程都会由一个用户身份运行
root 5 0.0 0.0 0 0 ? S< 3月11 0:00
</pre>
4.创建用户会在那个配置中保存信息
Linux系统会将用户的信息存放在/etc/passwd,记录了用户的信息,但没有密码信息。
密码被存放在/etc/shadow中,(这两个文件非常的重要,一般不要轻易删除与修改。)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n103" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@oldboy ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
root #用户名称
x #密码占位符
0 #用户UID
0 #组GID
root #注释信息
/root#用户家目录
/bin/bash #登录shell</pre>
[图片上传失败...(image-a21385-1584002011602)]
/etc/shadow 配置文件解释如下图
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n124" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@oldboy ~]# head -1 /etc/shadow
root:.aEJ6xoAWWucLsVp$9CzM7zxD05CPPlBisQBaSpO53k9BcH/HvQ2USJ5aLpjljvj4Vn6RLh4ULTiUnrTwnjb5WyxHFvTYW0PeNQbqW.::0:99999:7:::</pre>
[图片上传失败...(image-a86e5d-1584002011602)]
5.如何创建用户删除用户修改用户
1.使用useradd命令新增用户
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n138" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#选项
-u 指定要创建用户的UID,不允许冲突
-g 指定要创建用户默认组
-G 指定要创建用户附加组,逗号隔开可添加多个附加组
-d 指定要创建用户家目录 (默认在/home下)
-s 指定要创建用户的bash shell
-c 指定要创建用户注释信息
-M 给创建的用户不创建家目录
-r 创建系统账户,默认无家目录</pre>
1.创建abc用户,UID5003,基本组students,附加组sa 注释信息:2020 new student,登陆shell:/bin/bash
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n144" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#创建组
[root@oldboy ~]# groupadd students
[root@oldboy ~]# groupadd sa
[root@oldboy ~]# useradd abc -u 5003 -g students -G sa -c "2020 new student" -s /bin/bash #创建abc用户并指定属性
[root@oldboy ~]# id abc
uid=5003(abc) #用户UID
gid=5003(students) #组GID
组=5003(students), #基本组
1001(sa) #附加组
过滤配置文件检查用户详情
[root@oldboy ~]# grep 'abc' /etc/passwd
abc:x:5003:5003:2020 new student:/home/abc:/bin/bash</pre>
创建ttt系统用户,-M不建立用户家目录 -s指定nologin使其用户无法登陆系统
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n155" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@oldboy ~]# useradd ttt -r -s /sbin/nologin
[root@oldboy ~]# useradd ttt1 -M -s /sbin/nologin
[root@oldboy ~]# grep 'ttt' /etc/passwd
ttt:x:996:994::/home/ttt:/sbin/nologin
ttt1:x:5004:5004::/home/ttt1:/sbin/nologin
</pre>
如何使用usermod命令修改用户信息
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n162" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#选项
-u 指定要修改用户的UID
-g 指定要修改用户基本组
-G 指定要修改用户附加组,使用逗号隔开多个附加组, 覆盖原有的附加组
-d 指定要修改用户家目录
-s 指定要修改用户的bash shell
-c 指定要修改用户注释信息
-l 指定要修改用户的登陆名</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n168" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#查看之前创建abc用户的信息
[root@oldboy ~]# grep "abc" /etc/passwd
abc:x:5003:5003:2020 new student:/home/abc:/bin/bash
修改abc用户的uid 6001 group:devops groups:network ,sa
[root@oldboy ~]# groupadd devops
[root@oldboy ~]# groupadd network
[root@oldboy ~]# usermod abc -u 6001 -g devops -G network,sa
检查修改后的abc用户
[root@oldboy ~]# grep 'abc' /etc/passwd
abc:x:6001:5005:2020 new student:/home/abc:/bin/bash
[root@oldboy ~]# id abc
uid=6001(abc) gid=5005(devops) 组=5005(devops),1001(sa),5006(network)
修改abc用户的注释信息 用户家目录 /opt/abc 登录shell /bin/sh 登录名 abc_new
修改abc用户的信息
[root@oldboy ~]# usermod abc -d /opt/abc -s /bin/sh -l abc_new -c"2020 new student"
检查修改后的abc用户
[root@oldboy ~]# grep 'abc' /etc/passwd
abc_new:x:6001:5005:2020 new student:/opt/abc:/bin/sh
[root@oldboy ~]# id abc_new
uid=6001(abc_new) gid=5005(devops) 组=5005(devops),1001(sa),5006(network)
锁定用户[扩展]
[root@oldboy ~]# echo "123" |passwd --stdin username
[root@oldboy ~]# usermod -L username #锁定后会无法登陆系统
解锁用户[扩展]
[root@oldboy ~]# usermod -U username
使用userdel命令删除账户
选项 -r 删除用户的家目录,以及用户的邮件
尽量不要使用-r操作,因为对方的家目录下可能有一些比较重要的配置、或者其他...
删除user1用户,但不删除用户家目录和 var spool mail
[root@oldboy ~]# userdel user1
[root@oldboy ~]# id user1
id: user1: no such user
-r参数可以连同用户家目录一起删除(慎用)
[root@oldboy ~]# userdel -r user1
who、whoami、w检查用户登陆情况</pre>
6.如何为用户设定密码,又如何修改密码
创建用户后,如需要使用该用户登陆系统则需要为用户设定密码,设定密码使用passwd命令。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n178" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#1.使用passwd命令修改用户密码
passwd #给当前用户修改密码
passwd root #给root用户修改密码(只能是root才有此权限,其他任何用户都没有该权限)
passwd oldboy #给ttt用户修改密码(root可以,或者ttt用户自己给自己修改密码)
通过passwd --stdin读取输出的结果,将结果赋值给对应的用户
echo "123" | passwd --stdin xuliangwei #非交互式修改密码</pre>
备注:注意事项
1.普通用户只允许变更自己的密码,无法修改其他人密码,并且密码长度必须8位字符 2.管理员用户允许修改任何人的密码,无论密码长度多长或多短。
3.系统内置变量生成随机字符串
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n194" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@oldboy ~]# echo $RANDOM | md5sum | cut -c 2-10
ffaf9ce5a</pre>
4.mkpasswd生成随机字符串
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="HTML" cid="n200" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">选项:
-l 设定密码长度,
-d 数子,
-c 小写字母,
-C 大写字母,
-s 特殊字符
yum install expect -y
注意:各种特殊字符的位数加起来,不能超过-l指定的长度
[root@oldboy ~]# mkpasswd -l 10 -c 4 -C 3 -d 1 -s 2
8(jRNZ\yed</pre>
PS: 推荐密码保存套件工具,支持windows、MacOS、Iphone以及浏览器插件Lastpass官方网站