玩转Redhat Linux 8.0系列 | 获取超级用户访问权限

素材来源:Redhat Linux 8.0培训教材《RH124》、《RH134》和《RH294》

玩了5-6年的Linux,现在再来温习一遍RHCE培训教材,按照指导完成实验并与大家分享。

附上汇总贴:玩转Redhat Linux 8.0系列 | 合集_热爱编程的通信人的博客-CSDN博客


1 从workstation,以student用户身份打开连接servera的SSH会话。

[student@workstation ~]$ ssh student@servera
student@servera's password: 
Activate the web console with: systemctl enable --now cockpit.socket

This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register

Last login: Sat May 20 20:07:49 2023 from 172.16.190.227
[student@servera 20:14:19 ~]$

2 探索student的shell环境。查看当前的用户和组信息,并显示当前的工作目录。此外,还要查看指定用户主目录的环境变量以及用户可执行文件的位置。

2.1 运行id来查看当前的用户和组信息。

[student@servera ~]$ id
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[student@servera ~]$

2.2 运行pwd以显示当前的工作目录。

[student@servera ~]$ pwd
/home/student
[student@servera ~]$

2.3 显示HOME和PATH变量的值,以分别确定主目录和用户可执行文件的路径。

[student@servera ~]$ echo $HOME
/home/student
[student@servera ~]$ echo $PATH
/home/student/.local/bin:/home/student/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
[student@servera ~]$

3 在非登录shell中切换到root,并探索新的shell环境。

3.1 在shell提示符下运行sudo su,变为root用户。

[student@servera ~]$ sudo su
[sudo] password for student: 
[root@servera student]# 

3.2 运行id来查看当前的用户和组信息。

[root@servera student]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@servera student]# 

3.3 运行pwd以显示当前的工作目录。

[root@servera student]# pwd
/home/student
[root@servera student]# 

3.4 显示HOME和PATH变量的值,以分别确定主目录和用户可执行文件的路径。

[root@servera student]# echo $HOME
/root
[root@servera student]# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin
[root@servera student]# 

如果您已有一些Linux和su命令相关的经验,您可能会预计到,不带短划线(-)选项使用su来变为root会导致您保留student的当前PATH。这并未发生。正如您在下一步中所见,这也不是root的正常PATH。

发生了什么情况?差别在于您没有直接运行su。相反,您使用sudo以root身份运行了su,因为您没有掌握超级用户的密码。出于安全方面的原因,sudo命令最初覆盖来自初始环境的PATH。在初次覆盖之后运行的任何命令仍然可以更新PATH变量,您会在后续步骤中看到这一点。

3.5 从root用户的shell退出,以返回到student用户的shell。

[root@servera student]# exit
exit
[student@servera ~]$

4 在登录shell中切换到root,并探索新的shell环境。

4.1 在shell提示符下运行sudo su-,变为root用户。

[student@servera ~]$ sudo su -
[root@servera ~]# 

注意shell提示符与上一步中sudo su的相比的区别。

sudo可能会提示您输入student密码,也可能不提示,这取决于sudo的超时期限。默认的超时期限是五分钟。如果您在最近五分钟内通过sudo的身份验证,sudo不会提示您输入密码。如果自您通过sudo的身份验证起已超过五分钟,您需要输入student 作为密码以通过sudo的身份验证。

4.2 运行id来查看当前的用户和组信息。

[root@servera ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@servera ~]# 

4.3 运行pwd以显示当前的工作目录。

[root@servera ~]# pwd
/root
[root@servera ~]# 

4.4 显示HOME和PATH变量的值,以分别确定主目录和用户可执行文件的路径。

[root@servera ~]# echo $HOME
/root
[root@servera ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@servera ~]#

如前一步中所见,在sudo重置了student用户shell环境设置的PATH变量后,su -命令运行了root的shell登录脚本,并将PATH变量设置为另一个值。如果不带短划线(-)选项,su命令不会那么做。

4.5 从root用户的shell退出,以返回到student用户的shell。

[root@servera ~]# exit
logout
[student@servera ~]$

5 验证operator1用户是否配置为可利用sudo以任何用户身份运行任何命令。

[student@servera ~]$ sudo cat /etc/sudoers.d/operator1
operator1 ALL=(ALL) ALL
[student@servera ~]$

6 变为operator1,再查看/var/log/messages的内容。将/etc/motd复制到/etc/motdOLD,并将它删除(/etc/motd OLD)。这些操作需要管理权限,因此请使用sudo以超级用户身份运行这些命令。不要使用sudo su或sudo su -切换到root。使用redhat作为operator1的密码。

6.1 切换到operator 1。

[student@servera ~]$ su - operator1
Password: 
[operator1@servera ~]$

6.2 尝试查看/var/log/messages的最后五行,但不使用sudo。这应该会失败。

[operator1@servera ~]$ tail -5 /var/log/messages 
tail: cannot open '/var/log/messages' for reading: Permission denied
[operator1@servera ~]$

6.3 尝试使用sudo来查看/var/log/messages的最后五行。这应该会成功。

[operator1@servera ~]$ sudo tail -5 /var/log/messages 

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 operator1: 
May 20 08:35:26 servera org.gnome.Shell.desktop[8209]: Window manager warning: last_user_time (13046086) is greater than comparison timestamp (13046082).  This most likely represents a buggy client sending inaccurate timestamps in messages such as _NET_ACTIVE_WINDOW.  Trying to work around...
May 20 08:35:26 servera org.gnome.Shell.desktop[8209]: Window manager warning: W3 appears to be one of the offending windows with a timestamp of 13046086.  Working around...
May 20 08:35:31 servera systemd[1]: fprintd.service: Succeeded.
May 20 08:35:43 servera su[9142]: (to operator1) student on pts/3
May 20 08:36:07 servera systemd[1]: realmd.service: Succeeded.
[operator1@servera ~]$

6.4 尝试为/etc/motd制作副本/etc/motdOLD,但不使用sudo。这应该会失败。

[operator1@servera ~]$ cp /etc/motd /etc/motdOLD
cp: cannot create regular file '/etc/motdOLD': Permission denied
[operator1@servera ~]$

6.5 尝试使用sudo来为/etc/motd制作副本/etc/motd OLD。这应该会成功。

[operator1@servera ~]$ sudo cp /etc/motd /etc/motdOLD
[operator1@servera ~]$

6.6 尝试删除/etc/motdOLD,但不使用sudo。这应该会失败。

[operator1@servera ~]$ rm /etc/motdOLD
rm: remove write-protected regular empty file '/etc/motdOLD'? y
rm: cannot remove '/etc/motdOLD': Permission denied
[operator1@servera ~]$

6.7 尝试使用sudo来删除/etc/motd OLD。这应该会成功。

[operator1@servera ~]$ sudo rm /etc/motdOLD
[operator1@servera ~]$

6.8 从operator1用户的shell退出,以返回到student用户的shell。

[operator1@servera ~]$ exit
logout
[student@servera ~]$

6.9 从servera注销。

[student@servera ~]$ exit
logout
Connection to servera closed.
[student@workstation ~]$

猜你喜欢

转载自blog.csdn.net/guolianggsta/article/details/131305483