LoginSignup
7
8

More than 5 years have passed since last update.

root権限でのスクリプト実行を強制したい

Posted at

root権限が必要な以下のようなスクリプトを一般ユーザーでそのまま夜間バッチ実行して悲しい思いをしたので、そもそもroot権限なしでスクリプトを実行できなくする。

hoge.sh
#!/bin/bash

_hoge() { # 一般ユーザでも実行できる時間のかかる処理 }

_need_root_permission() {
  sudo hoge # ここでパスワードを聞かれる -> 止まる -> つらい
}

_main() {
  _hoge
  _need_root_permission  
}

_main

以下のようにする。

実行権限と所有者の変更
$ chmod 700 /path/to/hoge.sh
$ sudo chown root /path/to/hoge.sh
# -> -rwx------ root user  hoge.sh

※セキュリティとか環境変数とか余計なコマンドまでrootで実行されて危険とかは別途考慮すること

参考

linux - Can I make a script always execute as root? - Super User

7
8
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
7
8