LoginSignup
19
20

More than 3 years have passed since last update.

CentOS7で一般ユーザがsudoを使えるようにする

Posted at

環境

  • CentOS Linux release 7.6.1810 (Core)

前提

sudoしようとしてパスワードを入力すると、(ユーザ名) is not in the sudoers file. This incident will be reported.となる。

ここでは、sudoできないユーザをnormaluserとする。

[normaluser@localhost ~]$ whoami
normaluser

[normaluser@localhost ~]$ sudo su -
[sudo] password for normaluser:
normaluser is not in the sudoers file.  This incident will be reported.

手順

1. rootでログインする

rootの権限を与える操作なので、rootでログインする必要がある。

以下のコマンドを入力し、rootのパスワードを入力する。

[normaluser@localhost ~]$ su -
Password:

ログインに成功すると、コマンドの頭の文字が$から#に変わる。

[root@localhost ~]#

suが使えない場合は、ログイン画面に戻ってrootでログインする。

2. wheelグループを全コマンド実行可能なグループにする

viコマンドで/etc/sudoersを開く。デフォルトでは100行目付近に、wheelグループのコマンド実行権限についての設定がある。

viエディタをコマンドモードで開いている間に:set nuと打つと、行番号が表示される。

[root@localhost ~]# vi /etc/sudoers

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

パスワードを要求するならそのまま。
要求しないなら以下のように変更する。

(コメントアウトする場所を変更する。)

## Allows people in group wheel to run all commands
# %wheel  ALL=(ALL)       ALL

## Same thing without a password
%wheel        ALL=(ALL)       NOPASSWD: ALL

3. ユーザをwheelグループに追加する

以下のコマンドを実行する。

[root@localhost ~]# usermod -aG wheel normaluser

変更できたか確認する。

[root@localhost ~]# id normaluser
uid=1001(normaluser) gid=1001(normaluser) groups=1001(normaluser),10(wheel)

wheelグループに入っていることが確認できればOK。

4. sudoできるか確認する

一般ユーザに戻り、groupsコマンドを使ってwheelに所属できているか確認する。

exitrootから戻った場合は、再ログインする。

[normaluser@localhost ~]$ groups
normaluser wheel

[normaluser@localhost ~]$ sudo su -
[sudo] password for normaluser:
Last login: Mon Nov 25 01:01:20 PST 2019 on pts/0
[root@localhost ~]#

normaluserのパスワードを入力すると、rootにログインできる。

以上で、sudoを実行できることが確認できた。

参考

CentOSでuserをsudo可能にする
CentOS6の内容ですが、ほぼ同様の手順です。

19
20
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
19
20