ワークステーションにSSH接続をする方法を時系列でまとめました。
基本はクライアントの操作のみ行い、他は管理者に任せて良いと思います。
環境
リモート側 Ubuntu 20.04.4 LTS
クライアント側 Windows 11 OS
OpenSSH
1.SSHサーバの立ち上げ(リモート)
2023年10月現在では設定済みなためスキップ可能
terminalを立ち上げ、sudoでOpenSSH Serverを導入。インストール後に自動的にサーバが起動する。
remote@Ubuntu:~$ sudo apt update
remote@Ubuntu:~$ sudo apt install openssh-server
SSHサーバの状態を確認する。アクティブになっていれば良い。
remote@Ubuntu:~$ sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: e>
Active: active (running) since Fri 2022-04-01 17:20:28 JST; 1min 7s ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 1491287 (sshd)
Tasks: 1 (limit: 38046)
Memory: 1.6M
CGroup: /system.slice/ssh.service
└─1491287 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
4月 01 17:20:28 Ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
4月 01 17:20:28 Ubuntu sshd[1491287]: Server listening on 0.0.0.0 port 22.
4月 01 17:20:28 Ubuntu sshd[1491287]: Server listening on :: port 22.
4月 01 17:20:28 Ubuntu systemd[1]: Started OpenBSD Secure Shell server.
2.鍵生成(クライアント)
公開鍵を生成する。キーを聞かれるが、何も入力せずエンターキーを押す。
~/.sshにid_rsaとid_rsa.pubが生成される。前者が秘密鍵、後者が公開鍵である。
C:\Users\local>ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\local/.ssh/id_rsa):
C:\Users\local/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\local/.ssh/id_rsa.
Your public key has been saved in C:\Users\local/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:k804VB/olzwjBSf8EsSAf2nXkQsYWz+kdO91N4p5vZ4 local@DESKTOP-EA08HO2
The key's randomart image is:
+---[RSA 4096]----+
| ..=*== o. |
| . .*Bo*o. |
| ....*o+o+=|
| ..=B @ =o=|
| SooB = ..|
| o . .|
| . |
| . .|
| E |
+----[SHA256]-----+
公開鍵をリモートに転送する(SCPコマンドなど)。管理者権限でPowerShellを開き、以下を実行する。
1行目の転送先のユーザー名@ipアドレスは適宜変更する。以下転送例。
PS C:\WINDOWS\system32> cat ~/.ssh/id_rsa.pub | ssh remote@130.xx.xx.xx `
>> " `
>> mkdir -p ~/.ssh && chmod 700 ~/.ssh && `
>> cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys `
>> "
3.公開鍵の設定(リモート)
上のcatコマンドの方法で転送していればスキップ可能(なはず)
接続するアカウントの~/.sshに公開鍵が移動させている。
remote@Ubuntu:~$ ls -l ~/.ssh --time-style="+"
合計 4
-rw-r--r-- 1 remote remote 568 authorized_keys`
authorized_keysのパーミッションを600に変更する。
remote@Ubuntu:~$ chmod 600 ~/.ssh/authorized_keys
remote@Ubuntu:~$ ls -l ~/.ssh --time-style="+"
合計 4
-rw------- 1 remote remote 568 authorized_keys
4.SSH接続(クライアント)
sshコマンドで接続する。この時に秘密鍵のパスと接続先を指定する。
ssh -i [秘密鍵のpath] -p 22 [リモートのユーザー名]@[IPアドレス]
コマンド実行後にパスフレーズが聞かれるが、何も入力せずEnterキーを押す。その後リモート先のパスワードを聞かれるので、設定しているパスワードを入力してログインをする。
コマンドラインが[ユーザー名]@Ubuntu:~$となれば接続完了である。
C:\workspace\project2022-1\training>ssh -i C:\Users\local\.ssh\id_rsa -p 22 remote@130.xx.xx.xx
Warning: Identity file C:\Users\local\.ssh\id_rsa not accessible: No such file or directory.
Enter passphrase for key 'C:\Users\local/.ssh/id_rsa':
remote@130.xx.xx.xx's password:
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-109-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
0のアップデートはすぐに適用されます。
NVIDIA CUDA Version 10.1.243
Web console: https://Ubuntu:9090/ or https://192.xx.xx.xx:9090/
Last login: Thu Apr 28 18:23:38 2022 from 192.xx.xx.xx
remote@Ubuntu:~$
SSH接続してvscodeを使用する場合
以下のリンク先を参照してください。
https://www.server-memo.net/memo/vscode/vscode_ssh.html
Anaconda環境の方はSpyderやJupyter Notebookを使われているかもしれませんが、筆者は詳しくないので各自で調べるようお願いします。
SSH接続できない原因として考えられるもの
- OpenSSH ServerがActivateされていない
- 公開鍵がリモート側に転送されていない
- ファイアウォールがSSH接続を阻害している
- ポートが解放されていない
- リモート端末がルーター直下に接続されていない