xx is not in the sudoers file. This incident will be reported.

本文介绍如何在CentOS或Ubuntu系统中将新创建的用户添加到sudoers文件中,以便该用户能够以管理员权限执行命令。具体步骤包括:使用管理员权限编辑sudoers文件,在文件中添加新用户,并测试新用户的sudo权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 问题描述

centos或ubuntu下, 新增了用户, 用户名为xx,
则以sudo(以管理员身份)执行命令时, 出现以下提示

xx is not in the sudoers file. This incident will be reported.

xx 不在 sudoers 文件中。此事将被报告。

下面将添加用户xx到sudoers文件中

2. 切换到管理员
# su -
密码:
# vim /etc/sudoers
## Allows members of the 'sys' group to run networking, software, 
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
# %wheel ALL=(ALL) ALL
xx ALL=(ALL) ALL   # 新增条目, xx为用户名
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL

## Allows members of the users group to mount and unmount the 
## cdrom as root
# %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
:wq!

最后强行保存退出

3. 测试sudo

$ sudo mkdir test

没消息即好消息!最好检验下是否成功

# ls -l
### 解决 'bn is not in the sudoers file. This incident will be reported.' 错误并添加用户到sudoers文件 当普通用户尝试使用 `sudo` 命令时,如果收到错误提示 `'bn is not in the sudoers file. This incident will be reported.'`,说明该用户未被授权使用 `sudo` 命令。以下是解决此问题的方法[^1]。 #### 添加用户到sudoers文件 为了使用户能够使用 `sudo` 命令,需要将其添加到具有管理员权限的组中,或者直接编辑 `sudoers` 文件以赋予其权限。 1. **切换到root用户** 使用以下命令切换到 `root` 用户: ```bash su - ``` 输入 `root` 用户的密码完成切换。 2. **将用户添加到sudo组** 在大多数现代 Linux 发行版(如 Ubuntu)中,可以通过将用户添加到 `sudo` 组来赋予其管理员权限: ```bash usermod -aG sudo bn ``` 对于 CentOS 或 RHEL 系统,通常使用 `wheel` 组代替 `sudo` 组: ```bash usermod -aG wheel bn ``` 3. **手动编辑sudoers文件** 如果需要更细粒度地控制用户的权限,可以直接编辑 `/etc/sudoers` 文件。推荐使用 `visudo` 命令进行编辑,因为它会在保存时检查语法错误: ```bash visudo ``` 在文件中添加以下行以赋予用户 `bn` 全局管理员权限: ```plaintext bn ALL=(ALL) ALL ``` 如果希望用户在执行 `sudo` 时无需输入密码,可以使用以下配置: ```plaintext bn ALL=(ALL) NOPASSWD: ALL ``` 4. **验证用户权限** 切换回用户 `bn` 并测试是否可以使用 `sudo` 命令: ```bash su - bn sudo whoami ``` 如果输出为 `root`,则表示用户已成功获得管理员权限。 #### 注意事项 - 编辑 `/etc/sudoers` 文件时务必小心,错误的配置可能导致系统无法正常工作。 - 授予用户 `sudo` 权限时应根据实际需求决定是否允许其执行所有命令或仅特定命令。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值