- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2018-02-21T21:17:38+09:00","","")
#navi(../)
* sudoでリダイレクトや複数のコマンドを実行したい場合の記述方法 [#q1a8d815]
一般ユーザに書き込み権限が無いディレクトリでsudoコマンドを使い、以下のようなコマンドを実行するとエラーになります。
sakura@ubuntu:/var/www$ sudo echo "hello world" > hello.html
-bash: hello.txt: 許可がありません
本記事はsudoコマンドでリダイレクトしたり、複数のコマンドをsudoの権限で動作させる方法を記します。
#contents
#htmlinsertpcsp(linux_ads_top.html,linux-sp.html)
* 関連記事 [#b66be8fc]
-[[Debian/一般ユーザーをsudoコマンドにて管理者として動作するようにする設定]]
-[[Ubuntu/Ubuntu Serverで管理者権限があるユーザーを追加する手順]]
-[[Ubuntu/追加済み一般ユーザーを管理者権限に変更する方法・GUI編]]
-[[Ubuntu/追加済み一般ユーザーを管理者権限に変更する方法・CUI編]]
-[[Ubuntu/Ubuntuでrootになる方法]]
-[[sudoコマンドのパスワードを自動的に入力する方法>逆引きUNIXコマンド/sudoコマンドのパスワードを自動的に入力する方法]]
* man sudoで確認すると書いてある [#h8ae8610]
manコマンドでsudoを見ると書いてありました。~
以下、man sudoの抜粋です。
To make a usage listing of the directories in the /home partition.
Note that this runs the commands in a sub-shell to make the cd and file
redirection work.
$ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"
つまり、リダイレクトを可能にするにはsh -cを利用しコマンド群はダブルクォートで囲めば良いわけです。
sudo sh -c "echo 'hello world' > hello.html"
実際に実行した時の出力です。
sakura@ubuntu:~$ cd /var/www/
sakura@ubuntu:/var/www$ sudo sh -c "echo 'hello world' > hello.html"
[sudo] password for sakura:
sakura@ubuntu:/var/www$ ls hello.html
hello.html
つまり、sudoでリダイレクトやsudoで複数コマンドを利用したい場合は、以下のようにすれば動作します。
sudo sh -c "コマンド..."
以上、sudoでリダイレクトや複数コマンドを実行する方法でした。
#htmlinsertpcsp(linux_ads_btm.html,linux-sp.html)