天天看點

知識分享之Ubuntu——去除sudo經常輸入密碼問題

知識分享之Ubuntu——去除sudo經常輸入密碼問題

背景

日常我們開發時,我們會遇到各種各樣的奇奇怪怪的問題(踩坑o(╯□╰)o),這個常見問題系列就是我日常遇到的一些問題的記錄文章系列,這裡整理彙總後分享給大家,讓其還在深坑中的小夥伴有繩索能爬出來。

同時在這裡也歡迎大家把自己遇到的問題留言或私信給我,我看看其能否給大家解決。

全文使用環境:

  • 作業系統: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測試,就會發現不需要輸入密碼了。