LoginSignup
10
6

is not in the sudoers file. This incident will be reported. のエラー対処方法について

Last updated at Posted at 2023-03-22

はじめに

環境

  • Windows 11 home
  • VirtualBox 7.0.2
  • AlmaLinux 9.1

注意点

  • sudoersファイルを編集する際は、誤った変更を加えるとシステムが機能しなくなる可能性があるため、注意が必要です。そのためファイルを編集する前に、事前にバックアップを作成することを強くおすすめします。

このページの説明

「is not in the sudoers file. This incident will be reported.」というエラーメッセージの対処方法について記載しています。
対処後、sudo whoami を実行し root という出力が表示される事を確認して対応完了とします。

【前提】そもそも、このエラーはなに?

is not in the sudoers file. This incident will be reported(イズ ノット イン ザ スーデューユーズ ファイル、ディス インシデント ウィル ビー リポーテッド)、
これは、Linux システム上で特定のユーザーが、sudoコマンドを実行しようとしたときに表示されるものです。
is not in the sudoers file → sudoersファイルにユーザーが登録されていないことを示しています。
sudoersファイルは、特定のユーザーが、sudoコマンドを実行できるかどうかを設定するファイルです。
このエラーメッセージが表示されるということは、ユーザーは sudoコマンドを実行する権限がない。と言う事です。

つまり、sudo を使うために、/etc/sudoers に設定が必要なのに、それが設定されていないよ。というメッセージです。

【前提】毎回手動で設定しないといけないのか

多くの Linux ディストリビューションでは、インストール時に既に、sudo コマンドを実行するための設定が行われていることが多いと思います。
ただ、ディストリビューションやバージョンによって異なる場合がありますので、sudo コマンドが有効になっていない場合は、適宜設定が必要になります。

以降では、sudo を使うために、/etc/sudoers ファイルに、権限設定をする方法について解説します。

手順

1. 事前確認

1.1 sudo yum install 〇〇 などを実行して、該当のエラーが出ることを確認します。〇〇は、今回適当に vim にしました。(この為に vm を作成したので、vim は入ってません)

[test-almalinux@localhost ~]$ sudo yum install vim
[sudo] password for test-almalinux: 
test-almalinux is not in the sudoers file.  This incident will be reported.
[test-almalinux@localhost ~]$ 

1.2 sudo whoami を実行して、 sudoers ファイルに test-almalinux がない事を確認します。

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

1.3 root にログインします。

[test-almalinux@localhost ~]$ su -
Password: 
Last login: Thu Mar 23 00:11:10 JST 2023 on tty1
[root@localhost ~]#

1.4 ls で、 /etc 配下に sudoersファイルがある事を確認します。量が多いので パイプで繋ぎ、 grep で絞り込みます。

[root@localhost ~]# ls /etc | grep "sudoers"
sudoers
sudoers.d
[root@localhost ~]#

1.5 cp でコピーしてバックアップファイルを用意しておきます。

[root@localhost ~]# cp /etc/sudoers /etc/backup_sudoers
[root@localhost ~]#

1.6 バックアップ用の、「backup_sudoers」が出来ている事を確認します。

[root@localhost ~]# ls /etc | grep "sudoers"
backup_sudoers
sudoers
sudoers.d
[root@localhost ~]#

2. 本作業

2.1 root ユーザである事を確認して、sudo visudo を実行し、/etc/sudoers ファイルを開きます。

[root@localhost ~]# sudo visudo
Defaults    match_group_by_gid

# Prior to version 1.8.15, groups listed in sudoers that were not
# found in the system group database were passed to the group
# plugin, if any. Starting with 1.8.15, only groups of the form
# %:group are resolved via the group plugin by default.
# We enable always_query_group_plugin to restore old behavior.
# Disable this option for new behavior.
Defaults    always_query_group_plugin

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += "HOME"

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

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

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

## Allows members of the users group to mount and unmount the
## cdrom as root
# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

## Allows members of the users group to shutdown this system
# %users  localhost=/sbin/shutdown -h now

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d

2.2 Shuft + G を押してファイルの末尾に移動し、「i」を押してインサートモードになります。その後、以下のように行を追加します。(usernameの部分は、sudo権限を付与するユーザーのユーザー名に置き換えて下さい)

username ALL=(ALL) ALL

この行で、追加したユーザーが権限付与され、sudo を実行できるようになります。
私の環境では、username が test-almalinux なので以下のように記述してます。

test-almalinux ALL=(ALL) ALL

2.3 追加したら、Esc を押して「:wq」と入力し Enter で保存します。Enter を押すと先程の画面に戻ってくる事を確認します。

2.4 tail コマンドを実行して、ファイルの末尾に先程記述した箇所が追加されている事を確認します。( -n をつけて1行だけ表示されるようにします)

[root@localhost ~]# tail -n 1 /etc/sudoers
test-almalinux ALL=(ALL) ALL
[root@localhost ~]# 

2.5 exit で抜けます。root から一般ユーザになったことを確認します。

[root@localhost ~]# exit
logout
[test-almalinux@localhost ~]$ 

これで本作業は完了してますので、該当のユーザーは sudo 権限がついていますので、sudoコマンドを実行することができます。

3. 正常性確認

3.1 sudo whoami コマンドを実行します。

[test-almalinux@localhost ~]$ sudo whoami
[sudo] password for test-almalinux: 
root
[test-almalinux@localhost ~]$ 

「項番 1.2」で上記コマンドを実行したら、test-almalinux is not in the sudoers file. と表示されてましたね。
今回は root と表示されてますので、想定通り追加作業が完了しました。

おまけ

  • sudoers を編集する際、vi etc/sudoers ではなく、sudo visudo 実行する理由

sudo visudoコマンドを使用する理由は、/etc/sudoersファイルを安全に編集する事が出来ます。
vi /etc/sudoers を使用すると、複数のユーザーが同時に /etc/sudoers ファイルを編集することを防止したり、ロックがかけられます。又、構文エラーを検出しやすくなるように、ファイルの検証も自動的に行われます。
visudo コマンドは、sudoers ファイルを編集するための専用のコマンドであり、ファイルのパスを指定する必要はありません。

という理由により、vi ではなく sudo visudo を使いました!

まとめ

お疲れ様でした!
今回は、is not in the sudoers file. This incident will be reported. のエラー対処方法についてまとめてみました。
ここまで見ていただき、ありがとうございました!

10
6
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
10
6