Linux下sudo命令及/etc/sudoers配置说明

本文详细介绍了Linux系统中sudo命令的使用方法及权限配置过程。包括如何通过sudo命令临时获得root权限执行特定任务,如何编辑和理解/etc/sudoers文件来为用户或用户组分配不同级别的权限,以及如何避免输入密码直接使用sudo命令。

Linux下sudo命令及/etc/sudoers配置说明

使用su命令切换用户 如切换到root用户 需要输入root密码

su root

普通用户想要使用root权限 必须切换到root用户 这不废话么

或者配置 /etc/sudoers

su root

vim /etc/sudoers

root    ALL=(ALL)  ALL                 # 系统默认
user1   ALL=(ALL)  ALL                 # user1 可以使用最高权限 需要输入用户密码
user2   ALL=(ALL)  NOPASSWD : ALL      # user2 可以使用最高权限 不需要输入用户密码

# 如果设置用户组前面加 %

%group1  ALL=(ALL)  ALL                # 用户组group1下所有用户可以使用最高权限

由于/etc/sudoers文件是只读文件,所以保存时使用 !

:w!    # 保存
:wq!   # 保存并退出

切换到 su user1

使用最高权限

sudo -s
passwd:    # 需要输入当前user1的用户密码

切换到 su user2

使用最高权限

sudo -s    # 不需要输入当前user2的用户密码

cat /etc/sudoers
shutdown

直接使用sudo

sudo cat /etc/sudoers
sudo shutdown
The `/etc/sudoers` file is a critical security file in Linux systems that controls which users can run which commands with superuser (root) privileges. If it has world-writable permissions, it poses a significant security risk as any user on the system could modify it to gain unauthorized root access. Here are the steps to fix the issue: ### 1. Check the current permissions First, check the current permissions of the `/etc/sudoers` file using the `ls -l` command: ```bash ls -l /etc/sudoers ``` The correct permissions for `/etc/sudoers` should be `0440`, which means read-only for the owner (root) and the group (usually `root`), and no access for others. If the permissions are different, you need to change them. ### 2. Use `visudo` to edit the permissions The `visudo` command is the recommended way to edit the `/etc/sudoers` file as it checks for syntax errors before saving the changes. It also ensures that only one person can edit the file at a time. To change the permissions, first, open the file with `visudo`: ```bash sudo visudo ``` If the file is already being edited, `visudo` will prompt you to wait or take over the editing session. ### 3. Set the correct permissions After opening the file, you can set the correct permissions using the `chmod` command within the shell. Open a new terminal window (while keeping the `visudo` session open in the other terminal) and run the following command: ```bash sudo chmod 0440 /etc/sudoers ``` This command sets the permissions to read-only for the owner and the group, and no access for others. ### 4. Verify the permissions After running the `chmod` command, go back to the terminal where `visudo` is open and verify that the permissions have been changed correctly. You can exit `visudo` without making any changes to the file content by pressing `Ctrl + X`, then `Y`, and then `Enter`. ### 5. Double-check the permissions Finally, double-check the permissions of the `/etc/sudoers` file again using the `ls -l` command: ```bash ls -l /etc/sudoers ``` The output should show the permissions as `0440`. ### Summary By following these steps, you can safely fix the issue of the `/etc/sudoers` file having world-writable permissions. It's important to ensure that the file has the correct permissions to maintain the security of your system.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值