私のシェルスクリプトは、sudoersファイルのNOPASSWDパラメータを使用してrootとして自動的に実行されません。

私のシェルスクリプトは、sudoersファイルのNOPASSWDパラメータを使用してrootとして自動的に実行されません。

私のスクリプトがUbuntu 22.04でrootとして実行されないのはなぜですか?私はいかなる種類の認証も必要とせず、それを実行しているユーザーに関係なくこれを実行したいと思います。スクリプトはrootのようにすべてのコマンドを実行する必要がありますが、実際のrootユーザーだけが読み書きできます。これを達成するために、私はこの記事の指示に従いました。

シェルスクリプトを常にrootとして実行する方法はありますか?

だから私がしたことは次のとおりです。

1)次の内容(およびchmod + x)を含むcheck-nvidia-audio.desktopというデスクトップファイルを/ etc / xdg / autostartに配置しました。

[Desktop Entry] 
Version=1.0
Type=Application
Name=check_kernel
GenericName=Check the kernel version and unbind the NVIDIA audio driver
Comment=Check the kernel version and unbind the NVIDIA audio driver
Exec=sudo check-kernel
Icon=applications-biology
Path=/usr/sbin
Terminal=false
StartupNotify=false

2) /usr/sbin で次の内容(および chmod +x)を含む check-kernel というスクリプト:

if [ "`id -u`" -ne 0 ]; then
 echo "Switching from `id -un` to root"
 exec sudo "$0"
 exit 99
 fi

# Lets check the kernel version

function kernel-check() {
  CURRENT_KERNEL_VERSION=$(uname --kernel-release | cut --delimiter="-" --fields=3-4)
    echo CURRENT_KERNEL_VERSION = $CURRENT_KERNEL_VERSION
    if [ "${CURRENT_KERNEL_VERSION}" = "liquorix-amd64" ]; then
    echo "Kernel in use is already patched with the ACS patch and each NVIDIA audio device works great"
    exit
  else
    echo "Kernel in use is not patched with the ACS patch so I have to unbind each NVIDIA audio device from its driver"
    audio_device_list=( $(/usr/bin/iommu_viewer.sh | grep "Audio device.*: NVIDIA" | awk '{ print $3 }' ) )
    audio_group_list=( $(/usr/bin/iommu_viewer.sh | grep "Audio device.*: NVIDIA" | awk '{ print $2 }' ) )
    echo "audio nvidia gpu n. 1 =" ${audio_device_list[0]}
    echo "audio nvidia gpu n. 2 =" ${audio_device_list[1]}
    echo "iommu group nvidia gpu n. 1 =" ${audio_group_list[0]}
    echo "iommu group nvidia gpu n. 2 =" ${audio_group_list[1]}

# Lets check the audio of the nvidia gpu

  if [ ${audio_group_list[0]} -eq ${audio_group_list[1]} ]; then
    for audio_device in "${audio_device_list[@]}"
        do echo "$audio_device" > "/sys/bus/pci/devices/$audio_device/driver/unbind"
    done
fi
  fi
}
kernel-check

3)これは私の/ etc / sudoersファイルの外観です:

Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

Defaults    use_pty
# Cmnd alias specification
ALL     ALL = NOPASSWD: /usr/sbin/check-kernel

# 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

コンピュータを再起動すると何も起こらず、スクリプトを手動で実行しようとすると、次のことが起こります。

:~$ check-kernel
Switching from ziomario to root 
[sudo] password di ziomario:

or

:~$ sudo check-kernel
[sudo] password di ziomario:

これは予想される動作ではありません。 sudoersファイルに次のように指定したため、パスワードを要求しないでください。

すべてすべて = NOPASSWD:/usr/sbin/check-kernel

完全に無視しているようです。さらに、この行動はここで述べた内容を無視します。

シェルスクリプトを常にrootとして実行する方法はありますか?

具体的には次のような仮定をします。

オプションB:sudoからスクリプトを実行します。

ALL     ALL = NOPASSWD: /usr/local/bin/reset-swap

その後、リセット - スワップの代わりにsudoリセット - スワップを呼び出すことができます。

興味がある場合は、スクリプトがrootとして実行されていない場合は、sudoプレフィックスを指定せずに実行する独自の権限を増やすことができます。

#!/bin/sh
if [ "`id -u`" -ne 0 ]; then
 echo "Switching from `id -un` to root"
 exec sudo "$0"
 exit 99
fi

swapoff /dev/sdb
shred -n0 -z /dev/sdb
mkswap /dev/sdb
swapon /dev/sdb

何が間違っているのか想像できますか?ありがとうございます。

NB1:提供されたリンクは、次のように提案するのに役立ちませんでした。

myusername ALL = (root) NOPASSWD: /path/to/my/program

残念ながら、私のプロジェクトでは私のユーザー名を入力することはできません。その理由は、それが何であるか分からないからです。私はそれをインストールしたユーザーだけがユーザー名が何であるかを知ることができるカスタムディストリビューションを作成しています。私は成功せずにこれを試しました。

echo $USER ALL = (root) NOPASSWD: /usr/sbin/check-kernel

NB2:うまくいくようです。

Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
Defaults    use_pty

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

ALL     ALL = NOPASSWD: /usr/sbin/check-kernel
@includedir /etc/sudoers.d

ベストアンサー1

おすすめ記事