麒麟操作系统基础知识保姆级教程(十四)用户密码管理和sudo提权

如果你想拥有你从未拥有过的东西,那么你必须去做你从未做过的事情

你的密码是什么?

有些人喜欢使用自己的生日作为密码,有些人习惯使用一些纪念日为密码,有些人习惯密码直接1234,2589,158357,或者6666,8888,这些都属于弱密码,有些公司有涉密要求,强制使用强密码,那么什么是强密码,一般来说强密码强度要求要在12位以上,包括小写字母,大写字母,数字,特殊符号结合形成的密码,一个仅由8位小写字母组成的密码,其可能的组合数为26的8次方,约2088亿种组合;而一个12位包含大小写字母、数字和特殊字符的密码,其组合数将是一个极其庞大的数字。

💬欢迎交流:在学习过程中如果你有任何疑问或想法,欢迎在评论区留言,我们可以共同探讨学习的内容。你的支持是我持续创作的动力!

👍点赞、收藏与推荐:如果你觉得这篇文章对你有所帮助,请不要忘记点赞、收藏,并分享给更多的小伙伴!你们的鼓励是我不断进步的源泉!

🚀推广给更多人:如果你认为这篇文章对你有帮助,欢迎分享给更多对Linux感兴趣的朋友,让我们一起进步,共同提升!

目录

1.用户密码管理

2.MD5校验

2.1、MD5值定义和校验方法

2.2、md5值进阶

2.3、对多个文件进行MD5校验

3.sudo提权

3.1、sudo提权的概念和配置方法

3.2、sudo提权实战

每个人设置的密码都有会有固定思维,那么有没有生成随机密码的方法吗?

1.用户密码管理

如何设置较为复杂的密码:
使用下面命令会生成一个基于 Base64 编码的、长度大约为 14 个字符的随机字符串。
[root@yunzhongziedu ~]#  openssl rand -base64 14
bnKZSAvmagqArf8Gt6U=
​
pwgen:pwgen 是一个简单易用的密码生成工具,可以生成指定长度和复杂度的随机密码。例如,使用命令pwgen 12 1,其中 “12” 表示生成的密码长度为 12 位,“1” 表示生成一个密码,它会生成包含大小写字母和数字的随机组合密码,如 “aB3cD4eF5g6”。
​​​​​​​​
grub2-mkpasswd-pbkdf2:如果要为 GRUB 设置密码,可以使用grub2-mkpasswd-pbkdf2工具。运行该命令后,输入要设置的密码,它会生成对应的密码哈希值,将此哈希值配置到 GRUB 的相关文件中,即可完成 GRUB 密码的设置。

2.MD5校验

2.1、MD5值定义和校验方法

MD5值
MD5 的全称是 Message-Digest Algorithm 5,即信息摘要算法5,它是一种计算机广泛使用的散列算法,能将任意长度的数据运算为一个固定长度的 128 位二进制数,通常表示为 32 位十六进制数。
在理想情况下,不同的输入数据会产生不同的 MD5 值。但由于 MD5 已被王小云教授等学者破译,存在碰撞的可能性,即不同的数据可能产生相同的 MD5 值,但这种概率极低;而且无法通过 MD5 值反向推导出原始数据;相同的输入数据,无论在何时何地进行计算,得到的 MD5 值都是相同的。MD5 值主要用于确保信息传输的完整一致,常用于数字签名、文件完整性验证以及口令加密等领域;
​
MD5 校验
MD5 校验是一种用于验证传输数据完整性和正确性的技术,通过对接收的数据执行 MD5 散列运算来产生校验和,然后将其与原始数据附带的校验和进行比较,以确保数据未被篡改。利用了散列函数的特点,即将任意长度的数据转化为固定的短字符串。在数据传输前,先计算出原始数据的 MD5 值,并将其与数据一同传输。接收方收到数据后,再次计算数据的 MD5 值,然后将计算得到的值与接收到的 MD5 值进行对比。如果两者匹配,则表明数据传输过程中未发生错误;如果不匹配,则表示数据可能已被修改或损坏。许多编程语言都提供了计算 MD5 值的函数或库。例如,在 Python 中可以使用 hashlib 模块,而在 Java 中可以使用 java.security.MessageDigest 类。通过调用相应的函数,可以计算出给定数据的 MD5 值,并与预期的 MD5 值进行比较。在 Linux 中可以使用 md5sum 命令,在 Windows 中可以使用 certutil 命令来计算文件的 MD5 值,然后将计算结果与预期值进行对比。有一些在线的 MD5 校验工具,用户只需将要校验的数据输入到网页上,即可获取计算出的 MD5 值,然后与已知的 MD5 值进行比较。
MD5 校验广泛应用于各种需要确保数据完整性和安全性的领域,如机密资料的完整性验证、下载文件的准确性检测、软件的完整性校验等。例如,当用户从网上下载软件时,软件开发者通常会提供软件的 MD5 值,用户下载完成后可以通过计算下载文件的 MD5 值,并与开发者提供的 MD5 值进行对比,以确保下载的软件没有被篡改或损坏。

2.2、md5值进阶

1.生成md5值
[root@yunzhongziedu ~]# echo www.baidu.com > code.txt
[root@yunzhongziedu ~]# cat code.txt
www.baidu.com
[root@yunzhongziedu ~]# md5sum code.txt
ed6322bc931d5aba9cc72b63f0f03a82  code.txt
2.修改文件内容
[root@yunzhongziedu ~]# echo www.新浪.com > code.txt
3.重新校验发现发生变化
[root@yunzhongziedu ~]# md5sum code.txt
a494c461be8078726f7a22e91c5251ba  code.txt
-----------------------------------------------------------------
将生成的值放到一个文件中:
1.对文件生成md5的值
[root@yunzhongziedu ~]# echo www.yunzhongziedu.com > code.txt
[root@yunzhongziedu ~]# cat code.txt
www.yunzhongziedu.com
2.将md5的值保存到文件中
[root@yunzhongziedu ~]# md5sum code.txt >md5.log
[root@yunzhongziedu ~]# cat md5.log
13d2dc1d237f3789bb1466fbaae5eab7  code.txt
3.通过md5检查文件
[root@yunzhongziedu ~]# md5sum -c md5.log
code.txt: OK
4.修改code.txt
[root@yunzhongziedu ~]# echo www.yunzhongzi.com > code.txt
[root@yunzhongziedu ~]# md5sum -c md5.log  #重新验证
code.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
​
​
注意路径问题
因为md5sum -c检查的以文件中的路径为准,文件是相对路径code.txt
将文件移动个位置
[root@yunzhongziedu ~]# mv md5.log /opt/
[root@yunzhongziedu ~]# cat code.txt
www.yunzhongzi.com
[root@yunzhongziedu ~]# cd /opt
[root@yunzhongziedu opt]# cat md5.log
13d2dc1d237f3789bb1466fbaae5eab7  code.txt
[root@yunzhongziedu 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

2.3、对多个文件进行MD5校验

对多个文件做MD5校验值
[root@yunzhongziedu ~]# touch {1..5}.txt
[root@yunzhongziedu ~]# md5sum /root/*.txt
d41d8cd98f00b204e9800998ecf8427e  /root/1.txt
d41d8cd98f00b204e9800998ecf8427e  /root/2.txt
d41d8cd98f00b204e9800998ecf8427e  /root/3.txt
d41d8cd98f00b204e9800998ecf8427e  /root/4.txt
d41d8cd98f00b204e9800998ecf8427e  /root/5.txt
[root@yunzhongziedu ~]# md5sum /root/*.txt  > /opt/md5.log
[root@yunzhongziedu ~]# cat /opt/md5.log
d41d8cd98f00b204e9800998ecf8427e  /root/1.txt
d41d8cd98f00b204e9800998ecf8427e  /root/2.txt
d41d8cd98f00b204e9800998ecf8427e  /root/3.txt
d41d8cd98f00b204e9800998ecf8427e  /root/4.txt
d41d8cd98f00b204e9800998ecf8427e  /root/5.txt
[root@yunzhongziedu ~]# md5sum -c /opt/md5.log
/root/1.txt: OK
/root/2.txt: OK
/root/3.txt: OK
/root/4.txt: OK
/root/5.txt: OK
创造环境
[root@yunzhongziedu ~]# mkdir test
[root@yunzhongziedu ~]# touch test/{1..3}.txt
[root@yunzhongziedu ~]# ll
total 0
-rw-r--r-- 1 root root  0 Mar 21 16:46 1.txt
-rw-r--r-- 1 root root  0 Mar 21 16:46 2.txt
-rw-r--r-- 1 root root  0 Mar 21 16:46 3.txt
-rw-r--r-- 1 root root  0 Mar 21 16:46 4.txt
-rw-r--r-- 1 root root  0 Mar 21 16:46 5.txt
drwxr-xr-x 2 root root 45 Mar 21 16:50 test
[root@yunzhongziedu ~]# ll test
total 0
-rw-r--r-- 1 root root 0 Mar 21 16:50 1.txt
-rw-r--r-- 1 root root 0 Mar 21 16:50 2.txt
-rw-r--r-- 1 root root 0 Mar 21 16:50 3.txt
查找出所有的文件
[root@yunzhongziedu ~]# find /root/yunzhongzi/ -type f
/root/yunzhongzi/1.txt
/root/yunzhongzi/2.txt
/root/yunzhongzi/3.txt
/root/yunzhongzi/4.txt
/root/yunzhongzi/5.txt
/root/yunzhongzi/test/1.txt
/root/yunzhongzi/test/2.txt
/root/yunzhongzi/test/3.txt
创建MD5校验值
[root@yunzhongziedu ~]# find /root/yunzhongzi/ -type f|xargs md5sum >/opt/md5.log
[root@yunzhongziedu ~]# cat /opt/md5.log
d41d8cd98f00b204e9800998ecf8427e  /root/yunzhongzi/1.txt
d41d8cd98f00b204e9800998ecf8427e  /root/yunzhongzi/2.txt
d41d8cd98f00b204e9800998ecf8427e  /root/yunzhongzi/3.txt
d41d8cd98f00b204e9800998ecf8427e  /root/yunzhongzi/4.txt
d41d8cd98f00b204e9800998ecf8427e  /root/yunzhongzi/5.txt
d41d8cd98f00b204e9800998ecf8427e  /root/yunzhongzi/test/1.txt
d41d8cd98f00b204e9800998ecf8427e  /root/yunzhongzi/test/2.txt
d41d8cd98f00b204e9800998ecf8427e  /root/yunzhongzi/test/3.txt
在任意路径下直接检测md5.log
修改文件后检查
[root@yunzhongziedu ~]# echo lookup  > /root/yunzhongzi/1.txt
[root@yunzhongziedu ~]# md5sum -c /opt/md5.log
/root/yunzhongzi/1.txt: FAILED
/root/yunzhongzi/2.txt: OK
/root/yunzhongzi/3.txt: OK
/root/yunzhongzi/4.txt: OK
/root/yunzhongzi/5.txt: OK
/root/yunzhongzi/test/1.txt: OK
/root/yunzhongzi/test/2.txt: OK
/root/yunzhongzi/test/3.txt: OK
md5sum: WARNING: 1 computed checksum did NOT match

3.sudo提权

3.1、sudo提权的概念和配置方法

sudo的概念
sudo 是一种权限管理机制,允许系统管理员授权普通用户去执行 root 权限的操作,而无需知道 root 的密码。普通用户第一次执行 sudo 命令时,需输入自己的账户密码,系统会在 /var/run/sudo/ts 目录下为该用户创建时间戳,在有效时间内(一般为 5 分钟)再次执行 sudo 命令时则无需输入密码,可使用 - v 参数延长有效期,使用 - k 参数清除时间戳;在多人协作管理系统时,避免了因共享 root 密码带来的安全风险,同时也能让普通用户在需要时临时获得足够的权限来完成特定任务,提升了系统管理的灵活性和安全性;一些可利用 sudo 进行提权的常见命令有 find、cat、apt、zip、xxd、time、taskset、git、sed、pip、ed、tmux、scp、perl、bash、less、awk、man、vi、env、ftp、ed、screen 等。
​
总结sudo的作用:可以让普通用户执行命令的时候,相当于root的权限
​
sudo 提权配置
sudo 的主配置文件是 /etc/sudoers,通常使用 visudo 命令来编辑该配置文件,以确保配置的语法正确并防止错误配置导致系统问题.
​
用户提权配置:
语法格式:用户 用户=(主机名) 命令列表。例如,ABC ALL=(ALL) NOPASSWD: ALL 表示 ABC 用户可以在任何用户身份下,于任何主机上无需密码地执行任何命令;又如 abc ALL=(ALL) NOPASSWD: /usr/bin/mkdir, /usr/sbin/useradd, /usr/sbin/userdel 表示 abc 用户可以以任何用户身份在任何主机上无需密码地执行 mkdir、useradd 和 userdel 命令,命令需使用绝对路径。
​
配置示例:若要允许用户 nick 在 192.168.10.0/24 网段上连接主机并且以 root 权限执行 useradd 命令,可在 sudoers 文件中添加 nick 192.168.10.0/24=(root) /usr/sbin/useradd;
​
组提权配置:
语法格式:%组名 ALL=(ALL) 命令列表 或 %组名 ALL=(特定用户) 命令列表。例如,%wheel ALL=(ALL) ALL 表示 wheel 组的用户可以以任何用户身份在任何主机上执行所有命令;%abc tom=(server1) NOPASSWD: rm -rf, remove, del 表示 abc 组的用户可以以 tom 用户的身份在 server1 主机上无密码执行 rm -rf、remove、del 命令,同样命令建议使用绝对路径;
​
配置示例:将用户加入 wheel 组,可使该用户获得 sudo 权限。如 usermod -G wheel username ,然后在 sudoers 文件中有 %wheel ALL=(ALL) ALL 这一配置,则该用户就能使用 sudo 来执行相应命令。

3.2、sudo提权实战

准备两个窗口:
1.root管理员窗口
[root@yunzhongziedu ~]# useradd yunzhongzi
[root@yunzhongziedu ~]# echo 1|passwd --stdin yunzhongzi
Changing password for user yunzhongzi.
passwd: all authentication tokens updated successfully.
2.yunzhongzi窗口
[root@yunzhongziedu ~]# su - yunzhongzi
[yunzhongzi@yunzhongziedu ~]$ 
3.在普通用户的窗口测试,看是否有权限
[yunzhongzi@yunzhongziedu ~]$ cat  /var/log/messages
cat: /var/log/messages: Permission denied   #无权限
4.给yunzhongzi用户的cat命令提权
两种方法:
1).visodu回车     #有语法检测功能
2).vim /etc/sudoers#错了就错了,检测不出来
查看cat的绝对路径
[root@yunzhongziedu ~]# which cat
/usr/bin/cat
给yunzhongzi用户的cat命令提权
visudo回车(大概在100行)
[root@yunzhongziedu ~]# grep yunzhongzi /etc/sudoers
yunzhongzi  ALL=(ALL)       /usr/bin/cat
5.返回普通用户执行cat命令
[yunzhongzi@yunzhongziedu ~]$ sudo -l
​
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
​
    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.
​
[sudo] password for yunzhongzi: 
Matching Defaults entries for yunzhongzi on yunzhongziedu:
    !visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin, env_reset,
    env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2
    QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION
    LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER
    LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY",
    secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
​
User yunzhongzi may run the following commands on yunzhongziedu:
    (ALL) /usr/bin/cat   #查看已经有了cat命令的权限
[yunzhongzi@yunzhongziedu ~]$ sudo cat /var/log/messages(可以正常查看)
​
​
配置提权的格式
 1.授权单个命令
yunzhongzi  ALL=(ALL)       /usr/bin/cat
 2.授权多个命令  授权给开发 测试人员
[root@db01 ~]# grep yunzhongzi /etc/sudoers
 yunzhongzi  ALL=(ALL)       /usr/bin/cat,/usr/bin/cd
 3.授权给运维人员(危险危险危险)
 yunzhongzi  ALL=(ALL)       NOPASSWD: ALL
 4.对all进行取反
[root@db01 ~]# grep yunzhongzi /etc/sudoers
 yunzhongzi  ALL=(ALL)       NOPASSWD: ALL,!/usr/bin/rm

用户密码和sudo提权知识的分享到此结束,明日预告,权限管理~


想成为大佬,就要从小白开始,从0开始,一点一点的积累,慢慢成长,明天你就是大佬!!想学习更多麒麟操作系统的知识,关注小屁,让你成为运维老鸟~~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值