一、报错 xxx is not in the sudoers file. This incident will be reported.的解决方式
[songj@instance-jgzzg4xl ~]$ tail -n 4 /etc/shadow
tail: cannot open ‘/etc/shadow’ for reading: Permission denied
由于文件/etc/shadow在非超级账号的环境下没有权限打开,因此报错permission denied。我们使用sudo命令来执行
[songj@instance-jgzzg4xl ~]$ sudo !!
## !! 表示执行上一条语句
sudo tail -n 4 /etc/shadow
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 songj:
songj is not in the sudoers file. This incident will be reported.
此时报了我们标题中的错误,如果是第一次遇到可能很懵逼,这里是说在sudoers文件中没有账户songj的配置,我们需要切换到超级账户,对/etc/shadow文件进行添加songj用户的配置即可,具体如下:
- 进入超级用户模式。也就是输入
su -,切换到超级用户登录页面,提示你输入超级用户密码,输入密码后就进入了超级用户模式。(当然,你也可以直接用root账户直接进行操作) - 添加文件的写权限。也就是输入命令
chmod u+w /etc/sudoers。 - 编辑
/etc/sudoers文件。也就是输入命令vim /etc/sudoers,输入i进入编辑模式,找到这一 行:"root ALL=(ALL) ALL",在它的下面添加"[user] ALL=(ALL) ALL"(这里的[user]是你的用户名),然后保存(就是先按一 下Esc键,然后输入:wq)退出。 - 撤销文件的写权限。也就是输入命令
chmod u-w /etc/sudoers。
二、 su和sudo的差异
自己在初次接触su和sudo的时候有疑问,为何它俩的功能如此相近呢?有什么差异呢?
第一、
su意思为swith user即切换用户的意思,从字面意思其可以”向上“切换成超级用户权限,也可以”向下“切换成普通用户权限
sudo意思为super user do,即允许非root用户运行通常需要超级用户权限的其他Linux命令。
第二、
以普通用户权限切换到超级用户为例
su - 或者su,切换的过程中需要root账户密码
而sudo su -在切换到超级用户的权限中时,只需要输入当前用户密码即可。
这个差异就保证了root账号密码不会被大多数人所知,保证了系统不会被恶意破坏;而root用户能够控制哪些用户可以具有sudo的功能。
第三、
su - 或者 su ,只要用户知道root账户密码,就可以执行su命令,而此时需要额外产生一个新的shell
而sudo su - 在切换到超级用户的权限时,临时获取root权限来执行需要root权限的命令,此时不会产出一个新的shell。
第四、
sudo su -在使用超级用户权限时,需要在/etc/sudoers配置中进行配置本地用户对应有相应的权限,而su -命令不需要该配置项。所以,报错xxx is not in the sudoers file. This incident will be reported.就因为在sudoers中没有配置本地客户的信息。
好了,以上就是我在实操中对于su 和 sudo遇到问题的解决方案以及两者的差异点,希望能给读者以启发。