nmapやiptablesなどを使用したスクリプトを作成して実行したくてもrootでしか動作できなかったので、打開策としてvisudoがある。
一般ユーザに決まったコマンドのみ実行権限をあたえるコマンドです。
sudo の動作設定は /etc/sudoers に記述されおり、このファイルを編集することで sudo の動作を変更することができる。
/etc/sudoers ファイルは root ユーザでも読み出し権限しかなく、さらに記述内容を間違えると sudo コマンドが正常に動作しなくなることもあるため、/etc/sudoers 編集専用の visudo コマンドを使い編集する。
$visudo
と打つとテキストエディタのような画面になるので、以下に編集
www-data ALL=(root) NOPASSWD: /sbin/iptables,/usr/bin/nmap
www-data:設定をするユーザ
ALL=:すべてのホストから
(root):rootユーザに返信できる。(ALL):はすべて
最後のコマンドはALLにするとすべてとなるが危険
NOPASSWD:はrootのパスワードを求めない。
Ctl+Oで保存、その後、Ctl+Xで終了
Perlなどでは、system(“sudo /sbin/iptables -L”); などと記述すると使える。
実際の設定
sudo su
visudo
# 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 secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:$ #Host alias specification #User alias specification #Cmnd alias specification #User privilege specification root ALL=(ALL:ALL) ALL www-data ALL=(root) NOPASSWD: /sbin/iptables,/usr/bin/nmap #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