そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

AlmaLinux9.6でSSH公開鍵認証に変更しパスワード認証の無効化

※[PR]当ブログの記事の中にはプロモーションが含まれています。

・AlmaLinux9.6でSSH公開鍵認証に変更しパスワード認証の無効化する方法を知りたい。
・具体的な手順を教えてほしい。

こういった疑問に答えます。

本記事の内容

  1. AlmaLinux9.6でSSH公開鍵認証に変更しパスワード認証の無効化する手順

サトナカ (@souiunogaii)

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

こういった私が、解説していきます。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

AlmaLinux9.6でSSH公開鍵認証に変更しパスワード認証の無効化

管理用ユーザーをwheelグループに追加しrootユーザーをログイン無効化

rootユーザーのログイン無効化

/etc/ssh/sshd_config
[root@b-bidns01 ~]# vi /etc/ssh/sshd_config

 

     37 # Authentication:
     38
     39 #LoginGraceTime 2m
     40 PermitRootLogin no  ←★★★変更
     41 #StrictModes yes
     42 #MaxAuthTries 6
     43 #MaxSessions 10

管理者ユーザーをwheelグループに追加

usermod -g wheel [ユーザー名]
id [ユーザー名]
[root@b-bidns01 ~]# usermod -g wheel satonaka
[root@b-bidns01 ~]# id satonaka
uid=1000(satonaka) gid=10(wheel) groups=10(wheel)

su でrootになれるユーザーをwheelグループに制限

/etc/pam.d/su
[root@b-bidns01 ~]# vi /etc/pam.d/su
      1 #%PAM-1.0
      2 auth            required        pam_env.so
      3 auth            sufficient      pam_rootok.so
      4 # Uncomment the following line to implicitly trust users in the "wheel" group.
      5 #auth           sufficient      pam_wheel.so trust use_uid
      6 # Uncomment the following line to require a user to be in the "wheel" group.
      7 auth            required        pam_wheel.so use_uid ←★★★コメント解除して有効化
      8 auth            substack        system-auth
      9 auth            include         postlogin
     10 account         sufficient      pam_succeed_if.so uid = 0 use_uid quiet
     11 account         include         system-auth
     12 password        include         system-auth
     13 session         include         system-auth
     14 session         include         postlogin
     15 session         optional        pam_xauth.so

SSH公開鍵認証に変更しパスワード認証の無効化

PubkeyAuthenticationをyesに変更

/etc/ssh/sshd_config
[root@b-bidns01 ~]# vi /etc/ssh/sshd_config
     37 # Authentication:
     38
     39 #LoginGraceTime 2m
     40 PermitRootLogin no
     41 #StrictModes yes
     42 #MaxAuthTries 6
     43 #MaxSessions 10
     44
     45 PubkeyAuthentication yes  ←★★★変更

ユーザーホームに.sshディレクトリ作成してパーミッション700に変更

[satonaka@b-bidns01 ~]$ mkdir ~/.ssh
[satonaka@b-bidns01 ~]$ chmod 700 ~/.ssh

鍵ファイルをアップロードしてパーミッション600に変更

[satonaka@b-bidns01 ~]$ chmod 600 ~/.ssh/authorized_keys

Tera Termで公開鍵認証での接続テスト

従来のパスワード認証を無効化する

必ず、公開鍵認証の接続テストでOKになってから、従来のパスワード認証を無効化してください。
そうでないと、リモートでログインができなくなってしまい、
物理マシンに直接アクセスしたり、Proxmoxのコンソール画面から直接操作するか、
が必要になってしまいます。
[root@b-bidns01 ~]# vi /etc/ssh/sshd_config
     64 # To disable tunneled clear text passwords, change to no here!
     65 #PasswordAuthentication yes
     66 PasswordAuthentication no  ←★★★変更
     67 #PermitEmptyPasswords no

WinSCPの変更

sftpサーバ利用時はsudo時にパスワード不要に変更

[root@b-bidns01 ~]# visudo
    119 ## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
    120 #includedir /etc/sudoers.d
    121 satonaka  ALL=(ALL:ALL) NOPASSWD: /usr/libexec/openssh/sftp-server ←★★★追加