LoginSignup
1
0

More than 1 year has passed since last update.

パスワードなしで sudo で apt を実行できるようにする (ubuntu 21.04)

Last updated at Posted at 2021-07-24

パスワードなしで sudo で apt を実行できるようにする (ubuntu 21.04)

手順

echo $USER ALL=NOPASSWD: /usr/bin/apt | sudo tee /etc/sudoers.d/apt
sudo chmod 440 /etc/sudoers.d/apt

説明

こちらの記事 でパスワードなしで sudo を実行する方法を説明しました。

apt のプロキシ対応 の記事で少し触れたとおり、ubuntu 21.04 では /etc/sudoers が readonly に設定されています。そのうえで /etc/sudoers の最後で /etc/sudoers.d 以下のファイルを include するようになっています。

このため /etc/sudoers を直接編集する方法ではなく、/etc/sudoers.d 以下に新しいファイルを作成して行います。

/etc/sudoers.d/README の説明によれば /etc/sudoers.d 以下のファイルのファイルの permission を 440 とすべきとあるので、permission を明示的に指定します。

sudoers 関連のファイルを編集するときの注意

sudoers 関連の設定ファイルを編集する場合、操作を間違うと sudo が機能しなくなる恐れがあります。

このため sudoers 関連の設定ファイルを編集する前に、別に terminal を起動して以下コマンドでroot のシェルを起動したうえで、行うことをお勧めします。

sudo -i

別で root 権限の terminal を起動しておくと、sudoers 関連の設定ファイルの操作を誤った場合でも root 権限で元に戻すことができます。

参考

/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:/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

/etc/sudoers.d/README

#
# As of Debian version 1.7.2p1-1, the default /etc/sudoers file created on
# installation of the package now includes the directive:
#
#       #includedir /etc/sudoers.d
#
# This will cause sudo to read and parse any files in the /etc/sudoers.d
# directory that do not end in '~' or contain a '.' character.
#
# Note that there must be at least one file in the sudoers.d directory (this
# one will do), and all files in this directory should be mode 0440.
#
# Note also, that because sudoers contents can vary widely, no attempt is
# made to add this directive to existing sudoers files on upgrade.  Feel free
# to add the above directive to the end of your /etc/sudoers file to enable
# this functionality for existing installations if you wish!
#
# Finally, please note that using the visudo command is the recommended way
# to update sudoers content, since it protects against many failure modes.
# See the man page for visudo for more information.
#
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0