Linux: 无需输入密码执行sudo命令

陪她去流浪 桃子 2015年06月25日 阅读次数:2528

我电脑就我一个人使用,加上Ubuntu默认禁用了root账户,所以每次在执行需管理员权限的命令时就需要sudo+命令,并且需要输入当前账户的密码,有点繁琐,于是乎,想看看有没有不用输入密码就可以sudo的。

Google了一下,果然还是有的,可以用以下办法做到:

  1. 执行“sudo visudo”命令编辑“/etc/sudoers”文件
  2. 在文件的最后一行加入“你的用户名 ALL=(ALL) NOPASSWD: ALL

为什么要用这种格式添加不太清楚,没去研究。修改后大概就像下面这样:

twofei@ubuntu:~/Desktop$ sudo cat /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
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

twofei ALL=(ALL) NOPASSWD: ALL

注意到最后我添加的那一行。可以看到,我在执行sudo命令时已经没有输入密码了。

至于成功没有,你可以在修改完“/etc/sudoers”后退出并重新打开一个新的终端,然后执行“sudo -i”看是否需要密码,如果不需要,那么,恭喜,你成功了~

参考

  1. http://askubuntu.com/a/147265

标签:linux