0
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?

SSHで鍵認証

Last updated at Posted at 2024-07-02

サーバーの環境

Linux
// ホスト名確認
[root@momonga ~]# hostname
momonga

// OSバージョン確認
[root@momonga ~]# cat /etc/redhat-release
Rocky Linux release 9.3 (Blue Onyx)

// OpenSSL バージョン確認
[root@momonga ~]# openssl version
OpenSSL 3.0.7 1 Nov 2022 (Library: OpenSSL 3.0.7 1 Nov 2022)

// ユーザー名確認
[momonga@momonga ~]$ whoami
momonga

// ホームディレクトリパーミッション確認
[momonga@momonga ~]$ ls -l /home/ | grep momonga
drwx------. 2 momonga      wheel          62 Jul  2 06:50 momonga

サーバー側の作業

// 設定ファイルを検索
[root@momonga ~]# find / -name "sshd_config" -print
/etc/ssh/sshd_config

// 編集
[root@momonga ~]# vi /etc/ssh/sshd_config
以下の設定がコメントアウトされていたらコメントアウトを外す
Port 22
PubkeyAuthentication yes
:wq

// サーバー起動
[root@momonga ~]# systemctl start sshd

// 起動確認
[root@momonga ~]## systemctl status sshd
● sshd.service - OpenSSH server daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: enabled)
     Active: active (running) since Sat 2024-06-15 13:25:52 JST; 49min ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: 750 (sshd)
      Tasks: 1 (limit: 61691)
     Memory: 6.4M
        CPU: 175ms
     CGroup: /system.slice/sshd.service
             └─750 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
# systemctl enable sshd
# systemctl is-enabled sshd
enabled

// ポート確認
[root@momonga ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3 enp0s8
  sources:
  services: cockpit dhcpv6-client ssh

クライアント側の作業(DOS)

環境確認

  • ユーザーアカウント: post
  • ホームディレクトリ: C:\Users\post
DOS
Microsoft Windows [Version 10.0.22631.3737]
C:\Users\post>ssh -V
OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3

鍵作成

作業はホームディレクトリで行います。
DOSから ssh-keygen.exe を使って公開鍵、秘密鍵を作ります。
-t は暗号方式を、-C はコメントを設定するオプションとなります。

DOS
C:\Users\post>ssh-keygen.exe -t ed25519 -C ""
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\post/.ssh/id_ed25519): ← Enter
Created directory 'C:\\Users\\post/.ssh'.
Enter passphrase (empty for no passphrase): ← Enter
Enter same passphrase again: ← Enter
Your identification has been saved in C:\Users\post/.ssh/id_ed25519
Your public key has been saved in C:\Users\post/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:psQNDN65W5lzgtN0hqjy2TH+MHl7hQ33u15jfZctDYc
The key's randomart image is:
+--[ED25519 256]--+
|    .            |
|   . + o .       |
|    . * o o      |
|     o B =. .  . |
|  . . O S .= .E .|
|   o = X +. o .++|
|    o O .  .  .=B|
|       = ..   oo+|
|        o.   .o. |
+----[SHA256]-----+

公開鍵転送

公開鍵をサーバーに転送します。今回は scp コマンドを使います。

DOS
C:\Users\post>scp .ssh\id_ed25519.pub momonga@192.168.56.120:/home/momonga/
The authenticity of host '192.168.56.120 (192.168.56.120)' can't be established.
ED25519 key fingerprint is SHA256:Ic6+ibCq9PytYpzeILv9RHqxzGn8Bx0RlpC7YDRf3gk.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Warning: Permanently added '192.168.56.120' (ED25519) to the list of known hosts.
momonga@192.168.56.120's password:
id_ed25519.pub                                           100%   83    83.4KB/s   00:00

サーバー側で転送された公開鍵を登録

Linux
// SSH関連ディレクトリ、ファイル作成
[momonga@momonga ~]$ mkdir .ssh
[momonga@momonga ~]$ touch .ssh/authorized_keys

// 公開鍵の登録と確認
[momonga@momonga ~]$ cat id_ed25519.pub >> .ssh/authorized_keys
[momonga@momonga ~]$ cat .ssh/authorized_keys
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICoz2okGIjzz9hjq4qtFDhgwutMkAA8zuho2bL23TM/A

// ディレクトリパーミッション設定
[momonga@momonga ~]$ chmod 600 .ssh/authorized_keys
[momonga@momonga ~]$ chmod 700 .ssh

// 転送されてきた公開鍵はもう使わないので削除
[momonga@momonga ~]$ rm id_ed25519.pub

クライアントでテスト(DOS)

DOSから秘密鍵を指定してSSH接続を試みます。パスフレーズなしで接続できればOK。
ssh文法: ssh -i [秘密鍵パス] [接続先ユーザー]@[接続先サーバー]

Linux
C:\Users\post>ssh -i .ssh\id_ed25519 momonga@192.168.56.120
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Tue Jul  2 06:52:38 2024
[momonga@momonga ~]$ hostname
momonga

クライアントでテスト(Putty)

先ほど作成した秘密鍵からPutty用の秘密鍵(.ppkファイル)を作成します。

  1. PuTTYgen 起動
  2. アクション項目の「既存の秘密鍵の読み込み」の「読み込み」ボタンをクリックして、秘密鍵を指定
  3. 「外部の鍵のインポートに成功しました」というダイアログが表示されたら「OK」を押下する
  4. アクション項目の「生成した鍵の保存」の「秘密鍵を保存」を押下
  5. (パスフレーズなしの場合は確認のダイアログが表示されるので「はい」を押下)
  6. 名前を指定して「保存」を押下する。今回はファイル名として id_Ed25519.ppk と入力する
  7. PuTTYgen の画面を×ボタンで閉じる

保存したPPKファイルを使ったPuTTYの設定をします。

  1. PuTTY 起動
  2. 接続先の指定
    1. ホスト名: 192.168.56.120
    2. ポート: 22
    3. 接続タイプ: SSH
    4. 保存済みセッション一覧: momonga
  3. 左側のメニューの設定
    1. 接続 → データ、自動ログインのユーザー名: momonga
    2. 接続 → SSH → 認証 → クリデンシャル、認証のための秘密鍵ファイル: 作成したPPKファイルのパス
    3. セッションを選択
  4. 「保存済みセッションの読み込み、保存、削除」の「保存」を押下
  5. 「開く」を押下

パスフレーズなしでサーバーに接続できればOK。

vscodeでリモート接続

前提

*「Remote Development」がインストールされていること

sshのconfigファイルを作成する

  1. vscodeの左メニューの「リモート エクスプローラー」をクリック
  2. 上部のタブのプルダウンから「リモート(トンネル/SSH)」を選択
  3. 「SSH」をポイントして表示される歯車アイコンをクリック
  4. 画面上部にconfigファイルのパス候補が表示されるので下記を選択
    C:\Users\post.ssh
  5. configファイルを編集して保存
config
# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host momonga
    HostName 192.168.56.120
    User momonga
    IdentityFile C:\Users\post\.ssh\id_Ed25519
    Port 22
    TCPKeepAlive yes
    IdentitiesOnly yes
  

「リモート(トンネル/SSH)」タブのSSHのmomongaをポイントして表示されるアイコンの「新しいウィンドウで接続」をクリックして、
vscodeが立ち上がってリモートサーバーに接続できればOK

0
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
0
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?