1
0

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.

Rlogin接続をssh公開鍵認証にする

Last updated at Posted at 2024-02-25

Rloginの設定ファイルを更新するたびにパスワードを再設定するのが手間なので公開鍵認証に変更する。
以下のクライアント側設定は特にRloginとは関係なく、通常の公開鍵認証のやり方と同じ。

確認環境 

sshクライアント:windows10
sshサーバ:CentOS7.6

※この後ubuntu22.04.3TLSをsshサーバとしてこの方法でssh-rsaで公開鍵認証を試したところ、認証できなかった。最近のSSHバージョンではssh-rsaキーは古い暗号化アルゴリズムがデフォルトで受け入れられなくなっている可能性があり。ED25519を使うことで問題なく認証できるようになった。

クライアント側で鍵ペアを生成

ssh-keygeで鍵生成 (ssh-rasの場合)
C:\Users\user>ssh-keygen
ssh-keygeで鍵生成 (ED25519の場合)
C:\Users\user>ssh-keygen -t ed25519

※windowsのユーザディレクトリの直下に.sshフォルダが無ければ作成しコマンド実施
※パスフレーズなど聞かれるが無しで問題ないのですべてEnterキーを押して完了

C:\Users\xxx\.ssh>dir
2024/02/25  12:33    <DIR>          .
2024/02/25  12:33    <DIR>          ..
2024/02/25  12:33             2,610 id_rsa
2024/02/25  12:33               574 id_rsa.pub

以下の鍵が生成されてることを確認(ssh-rasの場合)
id_rsa・・・秘密鍵
id_rsa.pub・・・公開鍵

以下の鍵が生成されてることを確認(ED25519の場合)
id_ed25519・・・秘密鍵
id_ed25519.pub・・・公開鍵

サーバ側へ転送

(ssh-rasの場合)
type C:\Users\user\.ssh\id_rsa.pub | ssh xxxcom@192.168.x.x "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
xxxcom@192.168.x.x's password:(サーバのパスワードを投入)
(ED25519の場合)
type C:\Users\user\.ssh\id_ed25519.pub | ssh xxxcom@192.168.x.x "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
xxxcom@192.168.x.x's password:(サーバのパスワードを投入)

※xxxcomはサーバのログインIDを入れる
※192.168.x.xはサーバの接続先IPアドレスを入れる

これで完了。

鍵生成&公開鍵転送(ssh-rasの場合)ログ




 Directory of C:\Users\xxx\.ssh

2024/02/25  12:33    <DIR>          .
2024/02/25  12:33    <DIR>          ..
2024/02/25  12:33             2,610 id_rsa
2024/02/25  12:33               574 id_rsa.pub
2024/02/18  17:33             2,100 known_hosts
               3 File(s)          7,169 bytes
               2 Dir(s)  33,473,454,080 bytes free

C:\Users\xxx\.ssh>
C:\Users\xxx\.ssh>
C:\Users\xxx\.ssh>type C:\Users\xxx\.ssh\id_rsa.pub | ssh xxxcom@192.168.x.x "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
xxxcom@192.168.x.x's password:

C:\Users\xxx\.ssh>

サーバに公開鍵が登録されているのことを確認(ssh-rasの場合)ログ

ユーザディレクトリの.ssh配下のauthorized_keys に追加される。

[xxxcom@cent76sv1 ~]$ cd .ssh
[xxxcom@cent76sv1 .ssh]$ ls
authorized_keys  known_hosts
[xxxcom@cent76sv1 .ssh]$ ls -altr
合計 8
drwx------. 3 xxxcom xxxcom 232  8月 31 15:43 ..
-rw-r--r--. 1 xxxcom xxxcom 454  2月 18 00:11 known_hosts
drwx------. 2 xxxcom xxxcom  48  2月 25 12:37 .
-rw-------. 1 xxxcom xxxcom 574  2月 25 12:43 authorized_keys
[xxxcom@cent76sv1 .ssh]$ 
[xxxcom@cent76sv1 .ssh]$ cat authorized_keys 
ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx= xxx@LAPTOP-xxxxxxx
[xxxcom@cent76sv1 .ssh]$ 

Rloginの設定

以下のSSH認証鍵(K)でid_rsa、またはid_ed25519を選択する。以上。

image.png
※鍵要録することで、図にあるパスワードorパスフレーズの設定は投入不要になる。

Rloginの設定ファイル(.rlg)を更新する場合

以下のとおりEntry.IdKeyに公開鍵のパスid_rsaまたはid_ed25519を指定する。
Entry.IdKey="C:\Users\xxx\.ssh\id_rsa";

認証がうまくいかない時のデバック方法

クライアント側でデバック

C:\Users\ntt>ssh -v user@192.168.x.x

サーバ側でデバック

tail -f /var/log/auth.log
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?