- 切换到root用户
[[email protected] ~]$ su root
密码:
[[email protected] ~]#
- 查看/etc/sudoers文件权限,如果只读权限,修改为可写权限
[[email protected] ~]# ll /etc/sudoers
-r--r-----. 1 root root 4030 12月 10 09:55 /etc/sudoers
[[email protected] ~]# chmod 777 /etc/sudoers
[[email protected] ~]# ls -l /etc/sudoers
-rwxrwxrwx. 1 root root 4030 12月 10 09:57 /etc/sudoers
- 修改/etc/sudoers文件,执行命令如下:
/*username是你的用户名*/
[[email protected] ~]# echo ‘username ALL=(ALL) ALL‘ >> /etc/sudoers
或者root权限下输入Visudo 或者 vim /etc/sudoers,找到 root ALL=(ALL) ALL的字段,在下一行追加:
username ALL=(ALL) ALL
5分钟之后密码过期,下次需要重新输入,如果不想如此麻烦,可以用以下方法
username ALL=(ALL) NOPASSWD: ALL
说明:格式为{用户名 网络中的主机=(执行命令的目标用户) 执行的命令范围}
- 保存退出,并恢复/etc/sudoers的访问权限为440
[[email protected] ~]# chmod 440 /etc/sudoers
[[email protected] ~]# ll /etc/sudoers
-r--r-----. 1 root root 4030 12月 10 09:59 /etc/sudoers
- 切换到普通用户,测试用户权限提升功能
时间: 2024-12-30 10:09:33