パスワードなしでsudoスクリプトを実行できませんか?

パスワードなしでsudoスクリプトを実行できませんか?

私はArch Linux(i3)を使用しています。実行するスクリプトがありますrmmod hid-multitouch && sudo modprobe hid-multitouch。 i3でショートカットを編集し、パスワードなしでスクリプトを実行するように例外を設定し、コメントをsudo visudo外した後、次の行を追加しました。

%wheel ALL=(ALL) NOPASSWD: /home/hieuc/i3script/rmod_hid.sh
hieuc ALL=(ALL) NOPASSWD: /home/hieuc/i3script/rmod_hid.sh
しかし、うまくいかないようですが、数日前にはまだ働いていました。これは出力です。/etc/sudoers

## sudoers file.
##
## This file MUST be edited with the 'visudo' command as root.
## Failure to use 'visudo' may result in syntax or file permission errors
## that prevent sudo from running.
##
## See the sudoers man page for the details on how to write a sudoers file.
##

##
## Host alias specification
##
## Groups of machines. These may include host names (optionally with wildcards),
## IP addresses, network numbers or netgroups.
# Host_Alias    WEBSERVERS = www1, www2, www3

##
## User alias specification
##
## Groups of users.  These may consist of user names, uids, Unix groups,
## or netgroups.
# User_Alias    ADMINS = millert, dowdy, mikef

##
## Cmnd alias specification
##
## Groups of commands.  Often used to group related commands together.
# Cmnd_Alias    PROCESSES = /usr/bin/nice, /bin/kill, /usr/bin/renice, \
#               /usr/bin/pkill, /usr/bin/top
# Cmnd_Alias    REBOOT = /sbin/halt, /sbin/reboot, /sbin/poweroff

##
## Defaults specification
##
## You may wish to keep some of the following environment variables
## when running commands via sudo.
##
## Locale settings
# Defaults env_keep += "LANG LANGUAGE LINGUAS LC_* _XKB_CHARSET"
##
## Run X applications through sudo; HOME is used to find the
## .Xauthority file.  Note that other programs use HOME to find   
## configuration files and this may lead to privilege escalation!
# Defaults env_keep += "HOME"
##
## X11 resource path settings
# Defaults env_keep += "XAPPLRESDIR XFILESEARCHPATH XUSERFILESEARCHPATH"
##
## Desktop path settings
# Defaults env_keep += "QTDIR KDEDIR"
##
## Allow sudo-run commands to inherit the callers' ConsoleKit session
# Defaults env_keep += "XDG_SESSION_COOKIE"
##
## Uncomment to enable special input methods.  Care should be taken as
## this may allow users to subvert the command being run via sudo.
# Defaults env_keep += "XMODIFIERS GTK_IM_MODULE QT_IM_MODULE QT_IM_SWITCHER"
##
## Uncomment to use a hard-coded PATH instead of the user's to find commands
# Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
##
## Uncomment to send mail if the user does not enter the correct password.
# Defaults mail_badpass
##
## Uncomment to enable logging of a command's output, except for
## sudoreplay and reboot.  Use sudoreplay to play back logged sessions.
# Defaults log_output
# Defaults!/usr/bin/sudoreplay !log_output
# Defaults!/usr/local/bin/sudoreplay !log_output
# Defaults!REBOOT !log_output

##
## Runas alias specification
##

##
## User privilege specification
##
root ALL=(ALL) ALL

## Uncomment to allow members of group wheel to execute any command
%wheel ALL=(ALL) ALL

## Same thing without a password
%wheel ALL=(ALL) NOPASSWD: /home/hieuc/i3script/rmod_hid.sh
## User
hieuc ALL=(ALL) NOPASSWD: /home/hieuc/i3script/rmod_hid.sh

## Uncomment to allow members of group sudo to execute any command
# %sudo ALL=(ALL) ALL

## Uncomment to allow any user to run sudo if they know the password
## of the user they are running the command as (root by default).
# Defaults targetpw  # Ask for the password of the target user
# ALL ALL=(ALL) ALL  # WARNING: only use this together with 'Defaults targetpw'

## Read drop-in files from /etc/sudoers.d
## (the '#' here does not indicate a comment)
#includedir /etc/sudoers.d

私のスクリプト:

#! /bin/bash

sudo rmmod hid-multitouch && sudo modprobe hid-multitouch

i3のショートカットは次のとおりです。bindsym $mod+Shift+o exec $HOME/i3script/rmod_hid.sh

ベストアンサー1

/home/hieuc/i3script/rmod_hid.shスクリプトであると仮定すると、sudo /home/hieuc/i3script/rmod_hid.shパスワードなしで実行できる必要があります。 sudo権限は、sudo構成で指定されたスクリプトから呼び出されたコマンドではなく、指定されたコマンドに適用されます。

一方、スクリプトはすでにrootとして実行されているため、sudoは必要ありません。

#! /bin/bash
rmmod hid-multitouch && modprobe hid-multitouch

sudoを使用せずにスクリプトを呼び出すには、ユーザーIDを確認してください。

#! /bin/bash
test "$(id -u)" != 0 && exec sudo /home/hieuc/i3script/rmod_hid.sh "$@"
rmmod hid-multitouch && modprobe hid-multitouch

sudo呼び出されない場合は、自分自身を呼び出しますsudo。呼び出されたスクリプトは、sudo最後の行のコマンドをルートとして実行します。

おすすめ記事