在使用Linux的过程中,当使用普通用户执行某些程序被告知没有权限时,需要临时升级权限,但又不想切换到root用户,这时可以使用sudo
命令,比如这样。
sudo service mysqld start
但是,使用sudo也是需要权限的,如果你的管理员(可能是你自己)没有给你的普通用户开放权限,就会遇到下面的问题。
[gap@localhost Desktop]$ sudo service msyqld start
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 gap:
gap is not in the sudoers file. This incident will be reported.
这时,需要管理员使用root用户操作,给普通用户增加这个权限,主要是修改/etc/sudoers
文件,但这个文件对root用户也是只读的,所以要给这个文件增加写权限。
[root@localhost etc]# ll sudoers
-r--r-----. 1 root root 4000 Mar 6 2015 sudoers
[root@localhost etc]# chmod u+w sudoers
[root@localhost etc]# ll sudoers
-rw-r-----. 1 root root 4018 Oct 21 23:49 sudoers
使用vi
编辑sudoers文件。
[root@localhost etc]# vim sudoers
在root ALL=(ALL) ALL
的下一行增加一个普通用户的授权,如下。
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
gap ALL=(ALL) ALL
最后,把sudoers
文件的权限恢复。
[root@localhost etc]# chmod u-w sudoers