비밀번호 없이 sudo 스크립트를 실행할 수 없나요?

비밀번호 없이 sudo 스크립트를 실행할 수 없나요?

저는 아치 리눅스(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 구성에 지정된 스크립트에서 호출된 명령이 아니라 지정된 명령에 적용됩니다.

반면에 스크립트는 이미 루트로 실행 중이므로 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마지막 줄의 명령을 루트로 실행합니다.

답변2

hieuc ALL=(ALL) NOPASSWD: /home/hieuc/i3script/rmod_hid.sh

sudoers 파일의 이 줄은 사용자가시옥를 실행할 /home/hieuc/i3script/rmod_hid.sh때 비밀번호를 묻는 메시지를 표시하지 않습니다 sudo. 예를 들어:

hieuc$: sudo ~/i3script/rmod_hid.sh

sudo스크립트는 이미 루트로 실행 중이므로 스크립트 내에서 사용할 필요가 없습니다 .

sudo로 실행할 수 있는 사용자 쓰기 가능 파일이 있다는 것은 사용자가 스크립트를 수정하여 루트로 모든 명령을 실행할 수 있다는 것을 의미합니다.

답변3

당신이 언급한 스크립트 sudoers는 당신이 실행할 스크립트입니다 sudo.

그러니 하고 싶으면 그걸 언급해야 해 sudo /home/me/my-script./home/me/my-scriptsudoers

상황에 따라 추가하시면 modprobe hid-multitouch됩니다sudoers


포함하도록 script1을 작성할 수도 있습니다. sudo script2

스크립트 2를 sudoers.

예를 들어

script1핫키 코드로 호출됩니다. 다음이 포함되어 있다고 언급하지 않습니다 suderes .

#!/bin/bash
script_called_as="$0"
script_full_name="$(readlink -e "$script_called_as")"
script_dir="$(dirname "$script_full_name")"

sudo "$script_dir/«script2»"

«script2»스크립트 이름으로 바꾸세요 . 그런 다음 sudeersscript2(script1 아님)에게 말하세요.

관련 정보