跳至內容

當我執行 sudo 命令時,如何修正 Amazon EC2 或 Lightsail 執行個體上的 sudo 錯誤?

6 分的閱讀內容
0

我變更了 Amazon Elastic Compute Cloud (Amazon EC2) 或 Amazon Lightsail 執行個體上 /etc/sudoers 檔案的擁有權,或刪除了該檔案。現在,當我執行 sudo 命令時,會收到錯誤。

簡短說明

如果您刪除或錯誤設定 /etc/sudoers 檔案,則可能會收到以下語法或 sudo 錯誤:

  • 「sudo: /etc/sudoers is owned by uid 1000, should be 0」
  • 「sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set」
  • 「sudo: unable to open /etc/sudoers: No such file or directory」
  • 「sudo: no valid sudoers sources found, quitting」
  • 「sudo: error initializing audit plugin sudoers_audit」

如果您收到其中一個錯誤,那麼您將無法授予使用者或使用者群組存取系統資源的權限。若要修正 Amazon EC2 或 Lightsail 上的 /etc/sudoers 與 /usr/bin/sudo 檔案,請使用使用者資料或啟動指令碼來修改檔案權限。或者,將根磁碟連接到救援執行個體,以手動進行變更。

**注意:**如果您刪除了 /etc/sudoers 檔案,則必須透過使用者資料指令碼重新建立該檔案。

解決方法

**注意:**如果您在執行 AWS Command Line (AWS CLI) 命令時收到錯誤訊息,請參閱 AWS CLI 錯誤疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。

透過使用者資料或啟動指令碼修改檔案權限

若要解決「owned by uid 1000」或「must be owned by uid 0」錯誤訊息,您必須修改 /etc/sudoers 和 /usr/bin/sudo 檔案權限。

對於 Amazon EC2 執行個體,請完成以下步驟,透過使用者資料指令碼修改檔案權限:

  1. 開啟 Amazon EC2 console (Amazon EC2 主控台)。

  2. 在導覽窗格中,選擇 Instances (執行個體),然後選取 EC2 執行個體。

  3. 選擇 Instance state (執行個體狀態),然後選擇 Stop instance (停止執行個體)。

  4. 選擇 Actions (動作),然後選取 Instance settings (執行個體設定)。

  5. 選擇 Edit user data (編輯使用者資料)。

  6. 輸入以下指令碼:

    Content-Type: multipart/mixed; boundary="//"
    MIME-Version: 1.0
    --//
    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="cloud-config.txt"
    #cloud-config
    cloud_final_modules:
    - [scripts-user, always]
    --//
    Content-Type: text/x-shellscript; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="userdata.txt"
    
    #!/bin/bash
    # To fix error sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set. use below commands
    chown -R root:root /usr/bin/sudo
    chmod 4755 /usr/bin/sudo
    
    # to fix below sudo errors:
    #sudo: /etc/sudoers is owned by uid 1000, should be 0
    #sudo: no valid sudoers sources found, quitting
    #sudo: error initializing audit plugin sudoers_audit
    
    chown -R root:root /etc/sudoers
    chown -R root:root /etc/sudoers.d/
    sudo service sshd restart
    --//--
  7. Start the instance (啟動執行個體),然後等待它通過狀態檢查。

  8. Connect to the instance (連線到執行個體),然後執行以下命令,以驗證 /etc/sudoers 和 /usr/bin/sudo 檔案的權限是否正確:

    sudo ls -la /etc/sudoers
    sudo ls -la /usr/bin/sudo

    以下範例輸出顯示正確的權限:

    $sudo ls -la /etc/sudoers /usr/bin/sudo
    -r--r----- 1 root root   1714 Jun 25 13:38 /etc/sudoers
    -rwsr-xr-x 1 root root 281624 Jun 27  2023 /usr/bin/sudo

對於 Lightsail 執行個體,請完成以下步驟,使用啟動指令碼修改檔案權限:

  1. 開啟 Lightsail console (Lightsail 主控台)。

  2. 選擇 Snapshots (快照),然後選取執行個體快照。

  3. 選擇 Actions (動作),然後選取 Create new instance (建立新的執行個體)。

  4. 在 Optional (選用) 下,選擇 + Add launch script (+ 新增啟動指令碼)。

  5. 輸入以下啟動指令碼:

    #!/bin/bash
    # To fix error sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set. use below commands
    chown -R root:root /usr/bin/sudo
    chmod 4755 /usr/bin/sudo
    
    # to fix below sudo errors:
    #sudo: /etc/sudoers is owned by uid 1000, should be 0
    #sudo: no valid sudoers sources found, quitting
    #sudo: error initializing audit plugin sudoers_audit
    
    chown -R root:root /etc/sudoers
    chown -R root:root /etc/sudoers.d/
    sudo service sshd restart
    --//--
  6. 選擇 Create instance (建立執行個體)。

  7. Connect to the instance (連線到執行個體),然後執行以下命令,以檢查 /etc/sudoers 和 /usr/bin/sudo 檔案的權限:

    sudo ls -la /etc/sudoers
    sudo ls -la /usr/bin/sudo

    以下範例輸出顯示正確的權限:

    $sudo ls -la /etc/sudoers /usr/bin/sudo
    -r--r----- 1 root root   1714 Jun 25 13:38 /etc/sudoers
    -rwsr-xr-x 1 root root 281624 Jun 27  2023 /usr/bin/sudo

將根磁碟連接到救援執行個體

將您的執行個體設定為可停止並啟動

**注意:**當您停止並啟動執行個體時,執行個體的公共 IP 位址會變更。最佳實務是使用彈性 IP 位址,而不是公共 IP 位址,將外部流量路由至您的執行個體。如果您使用 Amazon Route 53,則在公共 IP 位址變更時,您可能需要更新 Route 53 DNS 記錄。

在您停止並啟動執行個體之前,請採取以下動作:

將根磁碟連接至救援執行個體

請完成以下步驟:

  1. 建立新的金鑰對。

  2. 取得原始執行個體根磁碟區的磁碟區 ID 和裝置名稱。

  3. 停止原始執行個體。

  4. 在相同可用區域中,從具有相同 Linux 作業系統 (OS) 版本的 Amazon Machine Image (AMI) 啟動復原執行個體。

  5. 將原始執行個體的根磁碟區分離,然後將其作為次要磁碟區連接到復原執行個體。

  6. 使用新的 SSH 金鑰對連線到復原執行個體。

  7. 執行以下命令以切換為根使用者:

    sudo su
  8. 若要識別遭封鎖的裝置名稱和分區,請在復原執行個體上執行以下命令:

    lsblk

    範例輸出

    [root ~]$ lsblk
    NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    xvda    202:0    0    8G  0 disk
    └─xvda1 202:1    0    8G  0 part /
    xvdf    202:80   0  101G  0 disk
    └─xvdf1 202:81   0  101G  0 part
    xvdg    202:96   0   30G  0 disk

    在前述範例中,xvda 和 xvdf 磁碟區裝置名稱是已分區磁碟區,而 xvdg 不是已分區磁碟區。
    如果您的磁碟區已分區,請執行以下命令來掛載 /dev/xvdf1 分區,而不是 /dev/xvdf 原始裝置:

    mount -o nouuid  /dev/xvdf1 /mnt

    如果您使用的是建置於 AWS Nitro System 的執行個體,則磁碟區裝置名稱會使用 /dev/nvme[0-26]n1 格式。執行以下命令,將分區掛載到 /mnt 目錄:

    mount -o nouuid  /dev/nvme1n1p1 /mnt

    **注意:**將 /dev/nvme1n1p1 替換為您使用 lsblk 命令識別的裝置名稱。如需詳細資訊,請參閱 Amazon EC2 執行個體上磁碟區的裝置名稱。

  9. 若要從正常運作的執行個體複製 /etc/sudoers 檔案作為備份,請執行以下命令:

    cp /etc/sudoers /mnt/etc/sudoers.bak
  10. 若要在 /mnt 目錄中建立 chroot 環境,請執行以下命令:

for i in dev proc sys run; do mount -o bind /$i /mnt/$i; done; chroot /mnt

前述命令會將原始根檔案系統中的 /dev、/proc、/sys 和 /run 目錄繫結掛載。此組態可讓在 chroot 環境內執行的程序存取這些系統目錄。 若要編輯 chroot 環境內的 /etc/sudoers 檔案,請執行以下命令:

visudo

請確認檔案具有以下預設設定:

# 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:
#includedir /etc/sudoers.d

**注意:**visudo 命令會鎖定 sudoers 檔案。
如果您在執行前述命令時收到錯誤,請執行以下 diff 命令,將 /etc/sudoers 檔案與新檔案 /etc/sudoers.bak 進行比較:

diff /etc/sudoers /etc/sudoers.bak

**注意:**如果您在編輯檔案之前未進行自訂變更,請將 /etc/sudoers.bak 檔案還原為 /etc/sudoers。 若要結束 chroot 環境,請執行以下命令:

exit
  1. 若要卸載根磁碟區,請執行以下命令:
umount -fl /mnt
  1. 將您作為次要磁碟區連接到復原執行個體的根磁碟區分離。然後,使用步驟 2 的裝置名稱,將其重新連接到原始執行個體。
  2. 啟動原始執行個體,然後執行以下命令,以確認 sudo 命令可正確運作:
sudo su

透過使用者資料修改檔案內容或重新建立檔案

**注意:**若要使用以下疑難排解方法,您必須為根使用者設定密碼。

重新建立檔案

若要以根使用者身分存取執行個體,以便重新建立檔案,請完成以下步驟:

  1. 開啟 Amazon EC2 console (Amazon EC2 主控台)。

  2. 在導覽窗格中,選擇 Instances (執行個體),然後選取執行個體。

  3. 選擇 Instance state (執行個體狀態),然後選擇 Stop instance (停止執行個體)。

  4. 在 Stop instance? (停止執行個體?) 下,選擇 Stop (停止)。

  5. 選擇 Actions (動作),然後選取 Instance settings (執行個體設定)。

  6. 選擇 Edit user data (編輯使用者資料)。

  7. 輸入以下指令碼,為根使用者設定暫時密碼:

    Content-Type: multipart/mixed; boundary="//"            
    MIME-Version: 1.0
    
    --//
    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="cloud-config.txt"
    
    #cloud-config
    cloud_final_modules:
    - [scripts-user, always]
    
    --//
    Content-Type: text/x-shellscript; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="userdata.txt"
    
    #!/bin/bash
    chpasswd <<<"root:root"
    --//

    **重要:**此密碼使用純文字傳遞祕密,因此並不安全。完成疑難排解步驟後,請務必從根使用者移除暫時密碼。

  8. Start the instance (啟動執行個體),然後以 ec2-user 身分登入。

  9. 若要重新建立 /etc/sudoers 檔案,請執行以下 pkexec 命令:

    pkexec /usr/sbin/visudo
  10. 輸入根使用者密碼,然後使用以下值修改 /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
  1. 執行 sudo 命令,以驗證 sudo 可正確運作。
  2. 停止執行個體。

刪除使用者資料指令碼

若要使用 Amazon EC2 主控台刪除使用者資料值 (包括根使用者密碼),請完成以下步驟:

  1. 開啟 Amazon EC2 console (Amazon EC2 主控台)。
  2. 在導覽窗格中,選擇 Instances (執行個體),然後選取執行個體。
  3. 選擇 Actions (動作),然後選擇 Instance settings (執行個體設定)。
  4. 選擇 Edit user data (編輯使用者資料),然後刪除所有使用者資料。

若要使用 AWS CLI 刪除使用者資料值 (包括根使用者密碼),請執行以下 modify-instance-attribute 命令:

aws ec2 modify-instance-attribute --instance-id example-instance-id --user-data Value=""

**注意:**將 example-instance-id 替換為您的執行個體 ID。

若要驗證您已移除使用者資料,請執行以下 describe-instance-attribute 命令:

aws ec2 describe-instance-attribute --instance-id example-instance-id --attribute userData

**注意:**將 example-instance-id 替換為您的執行個體 ID。

刪除使用者資料後,啟動執行個體。

相關資訊

為什麼我無法在 EC2 Linux 執行個體上執行 sudo 命令?

AWS 官方已更新 9 個月前