そういうのがいいブログ

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

Proxmox7.4の初期設定(3)SSH公開鍵認証に変更しパスワード認証の無効化

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

・Proxmox7.4のインストール後に行う初期設定について知りたい。
・具体的な手順を分かりやすく教えてほしい。

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

本記事の内容

  1. Proxmox7.4の初期設定(3)SSH公開鍵認証に変更しパスワード認証の無効化

サトナカ (@souiunogaii)

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

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

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

Proxmox7.4の初期設定(3)SSH公開鍵認証に変更しパスワード認証の無効化

公開鍵ファイルと秘密鍵ファイルの生成

前回の記事と同じ手順で公開鍵ファイルと秘密鍵ファイルを生成します。

souiunogaii.hatenablog.com

ホストOS側の設定ファイルの変更

/etc/ssh/sshd_config

PubkeyAuthentication yes に変更します。

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

PubkeyAuthentication yes

設定変更後のSSHの再起動

/etc/init.d/ssh restart

sh root@sv-sirius:/etc/ssh# /etc/init.d/ssh restart Restarting ssh (via systemctl): ssh.service.

公開鍵ファイルをホストOSへ配置

ホームディレクトリに「.sshディレクトリを作成

TeraTermSSH SCP機能を使って公開鍵ファイルをアップロード

パーミッション変更

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
$ sudo chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys

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

ログの確認

Jun  3 17:13:36 sv-sirius sshd[10232]: Accepted publickey for satonaka from 192.168.100.101 port 62877 ssh2: ECDSA SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Jun  3 17:13:36 sv-sirius sshd[10232]: pam_unix(sshd:session): session opened for user satonaka(uid=1000) by (uid=0)

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

必ず、公開鍵認証の接続テストでOKになってから、従来のパスワード認証を無効化してください。
そうでないと、リモートでログインができなくなってしまい、物理マシンに直接アクセスしたり、ESXi等のコンソール画面から直接操作するか、が必要になってしまいます。
/etc/ssh/sshd_config

PasswordAuthentication no に変更します。

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no

ついでに、rootユーザーの直接ログインを拒否しておきます。

# Authentication:

#LoginGraceTime 2m
PermitRootLogin forced-commands-only ★変更
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

設定変更後のSSHの再起動

/etc/init.d/ssh restart
root@sv-sirius:/etc/ssh# /etc/init.d/ssh restart
Restarting ssh (via systemctl): ssh.service.

WinSCPの設定変更

SSH認証に秘密鍵ファイルを設定

ついでに、SFTPサーバの設定も追加

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

/etc/sudoers
ユーザー名  ALL=(ALL:ALL) NOPASSWD: /usr/lib/openssh/sftp-server
#
# 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        mail_badpass
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
satonaka  ALL=(ALL:ALL) ALL
satonaka  ALL=(ALL:ALL) NOPASSWD: /usr/lib/openssh/sftp-server