0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RHEL8でユーザー作成、そのユーザーのみでrootにsuできるようにしてrootのsshログイン禁止

Last updated at Posted at 2022-07-27

必要になったのでメモ。

某環境でVMもらうとpublic IPとrootのパスワードだけ来るので、初期安心設定メモ。

  1. rootになれるユーザーでsshログイン
  2. suでrootになっていろいろ実行

とするように設定。その他にrootにsuせずsudoのみでをコマンドを実行する設定する方法もありますが、本投稿ではそこまではやりません。気になる方はググってください。
一時期のみ使うマイVM設定って感じです。

確認した環境: RHEL8

# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.6 (Ootpa)

なお以下コマンド実行例は最初にプロンプト#または$がついてますので、それは抜かして実行お願いします。

1. rootになれるユーザー作成

最初はしょうがないのでrootでsshログインして実施します。

ssh root@<VMのIPアドレス>

wheelというgroupのidのみrootにsuできるようにします。
wheelというgroupがあるか確認。

# cat /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:
cdrom:x:11:
mail:x:12:
(・・・以下続く)

wheel:x:10:があります! group idは10ですね。

このwheelというgroupでユーザーを作成します。
ここではadm001というidを作ります。ここお好みidで作ってください。
意識高いシステムはuidの数字に決まりがあって指定したりしますが、ここでは空いてるのを自動で使うこととします。

# adduser adm001 -g wheel
# 

エラーがなければ、特に出力もなく、終わります。

確認は/etc/passwdにidがあればOKです。またgroup idはwheelの10で設定されてますね。

# cat /etc/passwd | grep adm001
adm001:x:1000:10::/home/adm001:/bin/bash
# 

パスワードを設定しないと、sshログインできないので、パスワードを設定します。

# passwd adm001
ユーザー adm001 のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: すべての認証トークンが正しく更新できました。
# 

これでユーザー作成は完了です。
念の為、rootは一旦exitして、作成したidでsshログインできるか確認します。

# exit
ログアウト
Connection to xxx.xxx.xxx.xxx closed.
~:$ ssh adm001@<VMのIPアドレス>
adm001@xxx.xxx.xxx.xxx's password:
[adm001@xxx ~]$

無事入れたら、exitで抜けて、再度rootでsshログインします。

$ exit
ログアウト
Connection to xxx.xxx.xxx.xxx closed.
~:$ ssh root@<VMのIPアドレス>
root@xxx.xxx.xxx.xxx's password:
# 

2. wheelグループのユーザーのみrootにsuできるようにする

/etc/pam.d/suを編集します。
以下のような行があるので、#authの先頭の#を削除、その行の最後にroot_onlyをつける。
2番目の単語はrequiredの方です。似た行があるので注意。

# Uncomment the following line to require a user to be in the "wheel" group.
#auth		required	pam_wheel.so use_uid

編集後:

# Uncomment the following line to require a user to be in the "wheel" group.
auth		required	pam_wheel.so use_uid root_only

参考情報:

  • root_onlyをつけるとwheelグルーブ以外のuserはrootへのsuのみできないようになります。
  • root_onlyをつけないとwheelグルーブ以外のuserは全てのsuができないようになります。
  • 2番目の単語をsufficientにしてしまうと、wheelグルーブのuserはパスワードなしでrootにsuできます。

3. rootのsshログイン禁止

/etc/ssh/sshd_configを編集します。

PermitRootLogin yes

PermitRootLogin no

に変更して保存します。もし先頭に#がついていたら削除してください。

その後、以下のコマンドでsshd設定ファイルを再読み込みします:

# systemctl reload sshd

これでrootでsshログインできなくななりました!
exitしたら、rootでsshログインできないので、wheelグループのuseridでログイン後、必要に応じてrootにsuしてください。

以上です。

4. 追記

結局rootでログインはできないけど、不正ログイン試行が多すぎるのとおひとりさまサーバーだったためssh鍵認証にしてしまいました。事情によりUSサーバーだったので国内ipのみ制限とか難しかったためです。以下の投稿を参考にさせていただき実施しました:
SSH公開鍵認証で接続するまで

多い!!!!!

最終ログイン: 2022/07/28 (木) 08:19:54 CDT日時 pts/0
最後の失敗ログイン: 2022/07/28 (木) 19:16:40 CDT xxx.xxx.xxx.xxから開始日時 ssh:notty
最後の正しいログインの後に 3599 回の失敗ログインの試行があります
0
3
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
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?