大家都知道,普通用户执行系统程序时,需要临时提升到管理员权限,因此需要用到sudo命令
但在使用sudo命令的过程中,时常会碰得到如下状况:
当前用户不在sudoers文件中的提示信息,我们该如何解决呢?
[ccbipj@mycentos Desktop]$ sudo ls /root
[sudo] password for ccbipj:
ccbipj 不在 sudoers 文件中。此事将被报告。
下面就是解决方法:
1.切换到root权限,修改sudoers文件的权限为最大,目的是为下面的修改保存建立条件
[ccbipj@mycentos Desktop]$ su
密码:
[root@mycentos Desktop]# ll /etc/sudoers
-r--r-----. 1 root root 4188 3月 31 2016 /etc/sudoers
[root@mycentos Desktop]# chmod 777 /etc/sudoers
[root@mycentos Desktop]# ll /etc/sudoers
-rwxrwxrwx. 1 root root 4188 3月 31 2016 /etc/sudoers
## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
##
## This file must be edited with the 'visudo' command.
## Host Aliases
## Groups of machines. You may prefer to use hostnames (perhaps using
## wildcards for entire domains) or IP addresses instead.
# Host_Alias FILESERVERS = fs1, fs2
# Host_Alias MAILSERVERS = smtp, smtp2
## User Aliases
## These aren't often necessary, as you can use regular groups
## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
## rather than USERALIAS
# User_Alias ADMINS = jsmith, mikem
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults env_keep += "HOME"
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
## 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
## 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
## Allows members of the users group to shutdown this system
# %users localhost=/sbin/shutdown -h now
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d
上面是/etc/sudoers文件的部分内容,请在18行"Allow root to run any commands anywhere"下面
添加下面这一行,给普通用户ccbipj也包含进去,许可其能root用户一样在任何地方执行.
ccbipj ALL=(ALL) ALL
#格式说明(用户名 网络中的主机=(执行命令的目标用户) 执行的命令范围)
修改完,按:x保存退出.
3. 还原sudoers文件的权限为初始值,避免执行sudo操作时有一些不必要来自系统机制约束信息提示出现
[root@mycentos Desktop]# chmod 440 /etc/sudoers
下面是该文件权限没有还原的执行结果
[ccbipj@mycentos Desktop]$ sudo ls /root
sudo:/etc/sudoers 可被任何人写
sudo:没有找到有效的 sudoers 资源,退出
sudo:无法初始化策略插件
下面是还原权限后的执行结果
[ccbipj@mycentos Desktop]$ sudo ls /root
[sudo] password for ccbipj:
anaconda-ks.cfg bak_encode.py encode2.py encode.py py3encode.py