普通用户登录Ubuntu一般使用的是普通账户而非管理员账户,因此运行有管理员权限的程序时都要输入sudo [command],这样的命令输入非常频繁,关键是每次都要输入一次用户自己的密码,非常烦人,下面就教你使用sudo不用输入密码的小技巧。
解决方案:
假设我的用户名为hadoop,使用sudo不用密码的方法如下:
运行命令:sudo
visudo 或者sudo
vi /etc/sudoers,如果vi来编辑,则保存时记得用"wq!"强制保存,否则会提示只读不能保存的。
上述命令就是用来编辑/etc/sudoers这个文件的。
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
默认情况我们会看到有"root ALL=(ALL:ALL) ALL"这么一句话,意思就是root用户可以从任何主机连接进来以任意身份执行任意命令,但是需要输入密码,如果不想输入密码只要在后面的ALL之前加上NOPASSWD即可,关于该文件的格式及参数详解请看参考文献或运行"man sudoers"看帮助文档吧。下面直接给出方法吧:
如果想让hadoop用户使用sudo不用输入密码只要在该文件后面添加"hadoop ALL = NOPASSWD: ALL"这样一行即可。
如果想让 hadoop用户sudo不用密码即可执行某几个命令,可这样写"hadoop ALL = NOPASSWD: /usr/bin/abc.sh, /usr/sbin/adduser"。
注意:这里有个问题需要注意,就是后面的配置可能会覆盖前面的配置,所以你添加的配置最好是在最后一行。
参考文献:
非常Linux——sudoers文件的写法