怎么获取root权限教程(如何让普通用户获取root用户的权限)
时间:2024-09-02 08:46:04 编辑:IT生涯
sudo 的特点
限制用户执行指定的命令 记录用户执行的每一条命令 配置文件(/etc/sudoers)提供集中的用户管理、权限与主机等参数 验证密码的后5分钟内(默认值)无须再让用户再次验证密码
实战演练
环境:Red Hat Enterprise Linux Server release 7.3
1. 测试普通用户能否删除 root 用户创建的文件
[root@localhost ~]# mkdir /test [root@localhost ~]# cd /test [root@localhost test]# touch test.txt [root@localhost test]# mkdir test.dir [root@localhost test]# ll total 0 drwxr-xr-x. 2 root root 6 Jul 18 02:19 test.dir -rw-r--r--. 1 root root 0 Jul 18 02:19 test.txt [root@localhost test]# id test uid=1004(test) gid=1005(test) groups=1005(test) [root@localhost test]# su - test Last login: Thu Jul 18 02:17:11 EDT 2019 on pts/0 [test@localhost ~]$ cd /test [test@localhost test]$ ll total 0 drwxr-xr-x. 2 root root 6 Jul 18 02:19 test.dir -rw-r--r--. 1 root root 0 Jul 18 02:19 test.txt [test@localhost test]$ rm -rf test.dir/ rm: cannot remove ‘test.dir/’: Permission denied [test@localhost test]$ rm -rf test.txt rm: cannot remove ‘test.txt’: Permission denied [test@localhost test]$ ll total 0 drwxr-xr-x. 2 root root 6 Jul 18 02:19 test.dir -rw-r--r--. 1 root root 0 Jul 18 02:19 test.txt
2. 用 visudo 命令配置 sudo
[root@localhost ~]# visudo # 在 /etc/sudoers 配置文件中 root ALL=(ALL) ALL 这一行下面加入 test ALL=(ALL) ALL [root@localhost ~]# cat /etc/sudoers | grep ALL Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY" root ALL=(ALL) ALL test ALL=(ALL) ALL # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS %wheel ALL=(ALL) ALL # %wheel ALL=(ALL) NOPASSWD: ALL %wheel ALL=(ALL) NOPASSWD: ALL # %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
3. 普通用户结合 sudo 删除 root 用户的文件
[root@localhost ~]# echo "Jaking" | passwd --stdin test Changing password for user test. passwd: all authentication tokens updated successfully. [root@localhost ~]# su - test Last login: Thu Jul 18 02:34:50 EDT 2019 on pts/0 [test@localhost ~]$ cd /test/ [test@localhost test]$ ll total 0 drwxr-xr-x. 2 root root 6 Jul 18 02:19 test.dir -rw-r--r--. 1 root root 0 Jul 18 02:19 test.txt [test@localhost test]$ rm -rf test.dir/ rm: cannot remove ‘test.dir/’: Permission denied [test@localhost test]$ rm -rf test.txt rm: cannot remove ‘test.txt’: Permission denied [test@localhost test]$ sudo rm -rf test.dir/ [sudo] password for test: [test@localhost test]$ ll total 0 -rw-r--r--. 1 root root 0 Jul 18 02:19 test.txt [test@localhost test]$ sudo rm -rf test.txt [test@localhost test]$ ll total 0
4. sudo 免密配置
[test@localhost test]$ sudo cat /etc/shadow [sudo] password for test: root:$6$YZrm6scxO5zzICbR$fOzORb.0Ib9POZzJmrnzOGDqfFySp8X.9p5QpcpnJXWHIJvZcFpXQONyNigwrZbhXtyfnFn5F1mJsdkXS3jEF/::0:99999:7::: bin:*:16925:0:99999:7::: daemon:*:16925:0:99999:7::: adm:*:16925:0:99999:7::: ***省略部分输出信息*** [test@localhost test]$ id test2 uid=1006(test2) gid=1007(test2) groups=1007(test2) [root@localhost ~]# visudo # 在 /etc/sudoers 配置文件中 %wheel ALL=(ALL) NOPASSWD: ALL 这一行的下面 加入test ALL=(ALL) NOPASSWD: ALL [root@localhost ~]# cat /etc/sudoers | grep NOPASSWD # %wheel ALL=(ALL) NOPASSWD: ALL %wheel ALL=(ALL) NOPASSWD: ALL test ALL=(ALL) NOPASSWD: ALL [test@localhost ~]$ sudo cat /etc/shadow # 用普通用户查看 /etc/shadow 文件已经不需要再输入当前登录用户的密码 root:$6$YZrm6scxO5zzICbR$fOzORb.0Ib9POZzJmrnzOGDqfFySp8X.9p5QpcpnJXWHIJvZcFpXQONyNigwrZbhXtyfnFn5F1mJsdkXS3jEF/::0:99999:7::: bin:*:16925:0:99999:7::: daemon:*:16925:0:99999:7::: adm:*:16925:0:99999:7::: ***省略部分输出信息***
5. 配置 sudo 的部分权限
[root@localhost ~]# cd /tmp [root@localhost tmp]# rm -rf * [root@localhost tmp]# ll total 0 [root@localhost tmp]# touch file [root@localhost tmp]# mkdir dir [root@localhost tmp]# ll total 0 drwxr-xr-x. 2 root root 6 Jul 18 03:01 dir -rw-r--r--. 1 root root 0 Jul 18 03:01 file [root@localhost tmp]# whereis cat cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz [root@localhost tmp]# visudo # 把 /etc/sudoers 配置文件中的 test ALL=(ALL) ALL 改为 test ALL=(ALL) /usr/bin/cat [root@localhost ~]# cat /etc/sudoers | grep cat ## Updating the locate database # Defaults specification # Preserving HOME has security implications since many programs test ALL=(ALL) /usr/bin/cat [root@localhost ~]# su - test Last login: Thu Jul 18 03:06:55 EDT 2019 on pts/0 [test@localhost ~]$ sudo cat /etc/shadow # 给 test 用户配置了查看权限 root:$6$YZrm6scxO5zzICbR$fOzORb.0Ib9POZzJmrnzOGDqfFySp8X.9p5QpcpnJXWHIJvZcFpXQONyNigwrZbhXtyfnFn5F1mJsdkXS3jEF/::0:99999:7::: bin:*:16925:0:99999:7::: daemon:*:16925:0:99999:7::: adm:*:16925:0:99999:7::: ***省略部分输出信息*** [test@localhost ~]$ cd /tmp [test@localhost tmp]$ ll total 0 drwxr-xr-x. 2 root root 6 Jul 18 03:06 dir -rw-r--r--. 1 root root 0 Jul 18 03:01 file [test@localhost tmp]$ rm -rf dir # test 用户已经没有了删除权限 rm: cannot remove ‘dir’: Permission denied [test@localhost tmp]$ rm -rf file # test 用户已经没有了删除权限 rm: cannot remove ‘file’: Permission denied [test@localhost tmp]$ ll total 0 drwxr-xr-x. 2 root root 6 Jul 18 03:06 dir -rw-r--r--. 1 root root 0 Jul 18 03:01 file
相关文章
-
近日,有我爱卡论坛的网友发帖称,自己信用卡的额度原来是20000,在冷冻3个月后,接到短信提醒说额度调整为2000元。对此,网友大呼,这种冷冻提额实在“太坑爹”了。据了解,原来这位网友想尝试一下“传说中”的交行冷冻提额,便打客服电话咨询,客服自称这个系统每个月会审核用户的用卡情况,进行调整,说是对长[详细]
-
问题一:万一得了大病怎么办?在人的一生中,有两个我们无法预知的突发事件:疾病和意外。如果一个人真的遭遇大病或残疾,有三笔费用是必须要面对的:医疗费、康复费、收入损失费。社保可以为你解决基本医疗费的问题,但如果得了重病,需要进口药或者复杂的手术,那就只能自费了。这是一笔巨大的开支,特别是病后的恢复。医[详细]
-
近日,吴起县人民法院成功调解了一起借贷担保纠纷。 2011年5月24日,原告张某某为好友陈某某在被告某银行处借款提供了个人担保。该笔借款期限从2011年5月24日至2014年5月22日。张某某提供担保的保证期限为借款到期之日起两年内,即从2014年5月22日至2016年5月22日。借款到期后陈[详细]
-
装修大计,水电先行,电路作为隐蔽工程,一旦发生事故,轻则全屋短路,重则引发事故。可见做好电线布局十分重要,电线不能直接埋进墙体或者地面,应该进行穿线,防止电线绝缘层受损。因此,一款好的电工套管显得十分重要。然而,市面上的电工套管琳琅满目,如何选择一款性价比高、质量过硬的电工套管成了摆在业主面前的一道[详细]
-
爱养花的朋友来自各个领域,商人也是其中之一。大部分花店都为很多商务场所和家庭提供了各种花卉盆栽。他们对花卉的挑选和摆放都有很高的要求。今天这几种很受商业人士喜爱的花,不仅好看还有好寓意!白掌白掌,也被称为“一帆风顺”,代表着万事顺利。它四季常青,不会掉叶,适合在酸性泥炭土中生长,喜欢半阴半阳的环境,[详细]