🐡

SSH接続のメモ

2024/03/17に公開

はじめに

  • Windows11
  • Ubuntu 22.04 (proxmox上のコンテナ)
    WindowsからUbuntuに接続する。

Windows

以下のスクリプトを.ps1形式で保存して、PowerShellで実行する。

powershell
do {
	Write-Host	"Please input your profile name: "
	$profileName = PSConsoleHostReadLine
} while ($profileName -eq "")

do {
	Write-Host	"Please input host name: "
	$hostName = PSConsoleHostReadLine
} while ($hostName -eq "")

Write-Host	"Please input the port number: "
$port = PSConsoleHostReadLine

do {
	Write-Host	"Please input user name: "
	$userName = PSConsoleHostReadLine
} while ($userName -eq "")

do {
	Write-Host	"Please input the key name: "
	$keyName = PSConsoleHostReadLine
} while ($keyName -eq "")

#鍵生成
ssh-keygen -t ed25519 -f "${Home}\.ssh\$keyName"

$con = @"

Host $profileName
	HostName	$hostName
	User		$userName
	IdentityFile	`"${Home}\.ssh\$keyName`"
"@

#configファイルへの書き込み
Write-Output "$con" >> "${Home}\.ssh\config"
if ($port -ne "") {
	Write-Output "	Port		$port" >> "${Home}\.ssh\config"
}
手動で行う場合
  1. 鍵を生成する。
コマンドプロンプトの場合
cmd.exe
ssh-keygen -t ed25519 -f "%HOMEPATH%\.ssh\<鍵の名前>"
PowerShellの場合
powershell
ssh-keygen -t ed25519 -f "$Home\.ssh\<鍵の名前>"
  1. .sshディレクトリのconfigureファイルに以下の内容を追記する。
configure
Host <任意の名前>
	HostName	<ホスト名>
	Port		<接続に使用するポート番号>
	User		<ユーザ名>
	IdentityFile	<秘密鍵のパス>

Ubuntu

openssh-serverのインストールと実行

sudo apt install openssh-server
sudo systemctl start sshd.serivce

<鍵の名前>.pubの内容を追記。

vi ~/.ssh/authorized_keys

PasswordAuthenticationnoに変更。

vi /etc/ssh/sshd_config

その他

Discussion