知识分享之Ubuntu——去除sudo经常输入密码问题

知识分享之Ubuntu——去除sudo经常输入密码问题

背景

全文使用环境:

    操作系统:Ubuntu 20.04 硬件:Vmware虚拟机 4核 8G内存 200G存储

正文

在我们使用Ubuntu时经常遇到需要使用最高权限执行的命令,这时就需要增加sudo,而sudo命令通常需要我们进行输入一次管理员密码才可以使用,一段时间不操作,或者我们重新SSH连接上去后,再次使用sudo时仍然需要重新输入管理员密码,这种限定在生产环境下是一种很好的方式,在开发环境下就为我们的操作带来了一定的时间浪费,尤其是密码比较复杂时更加麻烦。 因此我们能否使用sudo时不输入密码呢?答案是肯定的。下面我们就开始实现它。

1、对于sudoers文件进行编辑,执行如下命令

cnhuashao@cnhuashao:~$ sudo visudo

2、在打开的文件下面增加如下代码

cnhuashao    ALL=(ALL) NOPASSWD:ALL

最终结果如下:

# 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:/snap/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:
cnhuashao    ALL=(ALL) NOPASSWD:ALL

#includedir /etc/sudoers.d

这时我们进行创建一个新的SSH连接,再次进行执行sudo测试,就会发现不需要输入密码了。

本文声明:

本作品由 采用 进行许可。

经验分享 程序员 微信小程序 职场和发展