Debianでsudoが使えなかったことのメモ

Debianを使ってるのだけれど、アップデートしたらsudoresの設定を間違って置換してしまって再設定しないといけなかった。そしてまたやり方を忘れてたから無駄な時間を取られたorz。ちなみエラーはこんなエラーだった。

$ username@hoge:~$ sudo apt-get install sudo
username is not in the sudoers file.  This incident will be reported.

なぜdebianはsudoをデフォルトで有効にしてないんだろうか。もしかしたら別のやり方を推奨してるのかもしれないけれどよくわからない。それか新しいもっと新しいdebianだと設定されてるんだろうか。
とにかくまた忘れて再設定は面倒なのでメモを残しておく。

sudoの設定の仕方

$ su -

この「-」は重要。「-」がないとあとで出てくるコマンド"visudo"が実行できず、
"bash: visudo: コマンドが見つかりません"とか
"bash: visudo: command not found."とか表示されてできないかもしれない。

$ apt-get install sudo

sudoがインストールされていない場合はこれが必要かも。

$ 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:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(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

「←ここ」と書いてある場所に自分のユーザー名の設定を入れる

#Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL)ALL
username ALL=(ALL:ALL)ALL

このときnanoというテキストエディタが起動していた場合、書き込み終わったらCtrl + o を押して、Enterを押すと保存ができる。保存が終わったら Ctrl + x で終了させる。

ちなみにnanoの「^X」とかの「^」はCtrlを押すことです。
こうすることでsudoが使えるようになるはず。

suコマンドで入った時のrootを抜けるには

$ exit

で抜ける事ができる。

メモ

・sudoresファイルは /etc/sudores の場所にある
・visudo を使わない場合のやり方
$ cd /etc/
$ chmod 777 sudores
$ vim sudores
として編集したあと
$ chmod 440 sudores
パーミッションを変更して、sudoを有効にするとかもできる。

危険なのでやっちゃいけない。