01.用户密码管理
1.1 手动设置
gn+1x4lXxya5UWJv95k=
1.2 自动生成
[root@db01 ~]# openssl rand -base64 14
pgIz/uABOTtBJViAVns=
mkpasswd 安装
[root@db01 ~]# yum -y install expect
[root@db01 ~]# mkpasswd
50.IsvemX
[root@db01 ~]# mkpasswd
5pLj\Zg7n
02.MD5校验
第一步: 生成md5值
[root@db01 ~]# cat code.txt
www.oldboyedu.com
[root@db01 ~]# md5sum code.txt
13d2dc1d237f3789bb1466fbaae5eab7 code.txt
第二步: 修改文件内容
[root@db01 ~]# echo www.xiaoze.com > code.txt
[root@db01 ~]# cat code.txt
www.xiaoze.com
第三步: 重新校验发现值发生变化
[root@db01 ~]# md5sum code.txt
ea9bc1aa9fb604c8e0ff5fb6c2a9a162 code.txt
---------------------------------------------------------------------------------------------------------------------------------
将生成的MD5值放入到一个文件中:
第一步: 对文件生成md5值
[root@db01 ~]# echo www.oldboyedu.com > code.txt
[root@db01 ~]# cat code.txt
www.oldboyedu.com
第二步: 将MD5值保存到文件中
[root@db01 ~]# md5sum code.txt > md5.log
[root@db01 ~]# cat md5.log
13d2dc1d237f3789bb1466fbaae5eab7 code.txt
第三步: 通过MD5检查文件
[root@db01 ~]# md5sum -c md5.log
code.txt: OK
第四步: 修改code.txt
[root@db01 ~]# echo www.oldboyedu.com > code.txt
[root@db01 ~]# md5sum -c md5.log
code.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
注意路径的问题:
因为<md5sum -c > 检查的以文件中的路径为准,文件中是相对路径code.txt
[root@db01 ~]# mv md5.log /opt/
[root@db01 ~]# ll
total 4
-rw-r--r-- 1 root root 18 Mar 21 10:45 code.txt
[root@db01 ~]# cat code.txt
www.oldboyedu.com
[root@db01 ~]# cd /opt/
[root@db01 opt]# cat md5.log
13d2dc1d237f3789bb1466fbaae5eab7 code.txt
[root@db01 opt]# md5sum -c md5.log
md5sum: code.txt: No such file or directory
code.txt: FAILED open or read
md5sum: WARNING: 1 listed file could not be read
使用绝对路径做MD5校验值
[root@db01 ~]# md5sum /root/code.txt
13d2dc1d237f3789bb1466fbaae5eab7 /root/code.txt
[root@db01 ~]# md5sum /root/code.txt > md5.log
[root@db01 ~]# cat md5.log
13d2dc1d237f3789bb1466fbaae5eab7 /root/code.txt
[root@db01 ~]# mv md5.log /opt/
[root@db01 ~]# ll
total 4
-rw-r--r-- 1 root root 18 Mar 21 10:45 code.txt
[root@db01 ~]# cd /opt/
[root@db01 opt]# ll
total 4
-rw-r--r-- 1 root root 49 Mar 21 11:11 md5.log
[root@db01 opt]# md5sum -c md5.log
/root/code.txt: OK
对多个文件做MD5校验值
[root@db01 ~]# md5sum /root/*.txt
60b725f10c9c85c70d97880dfe8191b3 /root/1.txt
3b5d5c3712955042212316173ccf37be /root/2.txt
2cd6ee2c70b0bde53fbe6cac3c8b8bb1 /root/3.txt
13d2dc1d237f3789bb1466fbaae5eab7 /root/code.txt
[root@db01 ~]# md5sum /root/*.txt > /opt/md5.log
[root@db01 ~]# cat /opt/md5.log
60b725f10c9c85c70d97880dfe8191b3 /root/1.txt
3b5d5c3712955042212316173ccf37be /root/2.txt
2cd6ee2c70b0bde53fbe6cac3c8b8bb1 /root/3.txt
13d2dc1d237f3789bb1466fbaae5eab7 /root/code.txt
[root@db01 ~]# md5sum -c /opt/md5.log
/root/1.txt: OK
/root/2.txt: OK
/root/3.txt: OK
/root/code.txt: OK
创建环境
mkdir oldboy
[root@db01 oldboy]# ll
total 16
-rw-r--r-- 1 root root 2 Mar 21 11:13 1.txt
-rw-r--r-- 1 root root 2 Mar 21 11:13 2.txt
-rw-r--r-- 1 root root 2 Mar 21 11:13 3.txt
-rw-r--r-- 1 root root 18 Mar 21 10:45 code.txt
drwxr-xr-x 2 root root 45 Mar 21 11:15 test
[root@db01 oldboy]# ll test/
total 0
-rw-r--r-- 1 root root 0 Mar 21 11:15 1.txt
-rw-r--r-- 1 root root 0 Mar 21 11:15 2.txt
-rw-r--r-- 1 root root 0 Mar 21 11:15 3.txt
查找出所有文件:
[root@db01 oldboy]# find /root/oldboy/ -type f
/root/oldboy/1.txt
/root/oldboy/2.txt
/root/oldboy/3.txt
/root/oldboy/code.txt
/root/oldboy/test/1.txt
/root/oldboy/test/2.txt
/root/oldboy/test/3.txt
[root@db01 oldboy]# find /root/oldboy/ -type f|xargs md5sum
60b725f10c9c85c70d97880dfe8191b3 /root/oldboy/1.txt
3b5d5c3712955042212316173ccf37be /root/oldboy/2.txt
2cd6ee2c70b0bde53fbe6cac3c8b8bb1 /root/oldboy/3.txt
13d2dc1d237f3789bb1466fbaae5eab7 /root/oldboy/code.txt
d41d8cd98f00b204e9800998ecf8427e /root/oldboy/test/1.txt
d41d8cd98f00b204e9800998ecf8427e /root/oldboy/test/2.txt
d41d8cd98f00b204e9800998ecf8427e /root/oldboy/test/3.txt
[root@db01 oldboy]# find /root/oldboy/ -type f|xargs md5sum > /opt/md5.log
[root@db01 oldboy]# cat /opt/md5.log
60b725f10c9c85c70d97880dfe8191b3 /root/oldboy/1.txt
3b5d5c3712955042212316173ccf37be /root/oldboy/2.txt
2cd6ee2c70b0bde53fbe6cac3c8b8bb1 /root/oldboy/3.txt
13d2dc1d237f3789bb1466fbaae5eab7 /root/oldboy/code.txt
d41d8cd98f00b204e9800998ecf8427e /root/oldboy/test/1.txt
d41d8cd98f00b204e9800998ecf8427e /root/oldboy/test/2.txt
d41d8cd98f00b204e9800998ecf8427e /root/oldboy/test/3.txt
在任意路径下直接检测md5.log
修改文件后检查:
[root@db01 ~]# echo aaaa > /root/oldboy/1.txt
[root@db01 ~]# md5sum -c /opt/md5.log
/root/oldboy/1.txt: FAILED
/root/oldboy/2.txt: OK
/root/oldboy/3.txt: OK
/root/oldboy/code.txt: OK
/root/oldboy/test/1.txt: OK
/root/oldboy/test/2.txt: OK
/root/oldboy/test/3.txt: OK
md5sum: WARNING: 1 computed checksum did NOT match
03.sudo 提权
作用: 可以让普通用户执行命令时候,相当于root的权限
类似皇帝颁发赐予尚方大宝剑 黄马褂
准备两个窗口:
第一个: root管理员
[root@db01 ~]# useradd oldboy
[root@db01 ~]# echo 1|passwd --stdin oldboy
Changing password for user oldboy.
passwd: all authentication tokens updated successfully.
第二个: 普通用户oldboy
[root@db01 ~]# su - oldboy
[oldboy@db01 ~]$
第三步: 在普通用户窗口测试: 查看日志没有权限
[oldboy@db01 ~]$ cat /var/log/messages
cat: /var/log/messages: Permission denied
第四步: 对oldboy用户执行的cat命令进行提权
oldboy用户在执行cat时候就相当于root管理员
visudo ======= vim /etc/sudoers
编辑配置文件两种方式:
1 . 直接visudo回车 # 有语法检测的功能
2 . 使用 vim 编辑 /etc/sudoers
查看命令的绝对路径:
[root@db01 ~]# which cat
/usr/bin/cat
配置oldboy提权cat单条命
visudo 直接回车编辑文件:
[root@db01 ~]# grep oldboy /etc/sudoers
oldboy ALL=(ALL) /usr/bin/cat
第五步: 返回到普通用户执行cat命令
[oldboy@db01 ~]$ sudo -l # 查看自己拥有的提权后的命令
[sudo] password for oldboy: # 输入oldboy自己的密码
[oldboy@db01 ~]$ sudo cat /var/log/messages # 可以正常查看系统日志
配置提权的格式:
1.授权单个命令
oldboy ALL=(ALL) /usr/bin/cat
2.授权多个命令 授权给开发 测试人员
[root@db01 ~]# grep oldboy /etc/sudoers
oldboy ALL=(ALL) /usr/bin/cat,/usr/bin/cd
3.授权给运维人员(危险)
oldboy ALL=(ALL) NOPASSWD: ALL
4.对all进行取反
[root@db01 ~]# grep oldboy /etc/sudoers
oldboy ALL=(ALL) NOPASSWD: ALL,!/usr/bin/rm
last命令
last # 显示登录详细信息
登录用户 终端 来源IP地址(公司|家) 登录时间 退出时间 总共待了多久
root pts/0 123.112.17.72 Thu Mar 21 15:14 - 15:15 (00:00)lastlog # 显示用户最后的一次登录时间