日常我们开发时,我们会遇到各种各样的奇奇怪怪的问题(踩坑o(╯□╰)o),这个常见问题系列就是我日常遇到的一些问题的记录文章系列,这里整理汇总后分享给大家,让其还在深坑中的小伙伴有绳索能爬出来。
同时在这里也欢迎大家把自己遇到的问题留言或私信给我,我看看其能否给大家解决。
- 操作系统:Ubuntu 20.04
- 硬件:Vmware虚拟机 4核 8G内存 200G存储
在我们使用Ubuntu时经常遇到需要使用最高权限执行的命令,这时就需要增加sudo,而sudo命令通常需要我们进行输入一次管理员密码才可以使用,一段时间不操作,或者我们重新SSH连接上去后,再次使用sudo时仍然需要重新输入管理员密码,这种限定在生产环境下是一种很好的方式,在开发环境下就为我们的操作带来了一定的时间浪费,尤其是密码比较复杂时更加麻烦。
因此我们能否使用sudo时不输入密码呢?答案是肯定的。下面我们就开始实现它。
cnhuashao@cnhuashao:~$ sudo visudo2、在打开的文件下面增加如下代码
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测试,就会发现不需要输入密码了。
本文声明:知识共享许可协议
本作品由 cn華少 采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可。



