Thu*_*fir 13 windows ssh command-line openssh
如何通过 PowerShell在 Windows 的配置文件中设置主机名和端口OpenSSH?
在 Unix/Linux 上:
通过键入以下内容立即编辑或创建文件:
纳米 ~/.ssh/config
在这里,您可以设置特定于主机的配置选项。要指定新端口,请使用如下格式:
主机 remote_alias 主机名 remote_host 端口 port_num
这将允许您无需在命令行中指定特定端口号即可登录。
小智 19
在带有 PowerShell 的 Windows 10 中,不会创建配置文件,因此我们必须自己创建它们。
这个答案是用:Windows 10 PRO 20H2 (Build 19042.804)
以及来自官方 GitHub 的最后一个OpenSSH - Portable (v8.1.0.0p1-Beta)完成的
注意1:这里我展示如何仅配置文件夹.ssh中的配置文件“config” ,该文件应该位于用户文件夹中$HOME\.ssh,因为它是必需的,通常在我看来其他文件是自动创建的当一台安装 Open-SSH 服务器时。如果不是这种情况,只需调整命令行即可
注意 2:拥有适用于 Windows 的 Git 和 OpenSSH-portable 可能会导致代理配置出现问题,因此您应该知道它是 Windows 服务使用的 SSH-Agent
您可以使用以下命令找出 Windows 服务使用哪个 ssh-agent:
Get-WmiObject win32_service | ?{$_.Name -like 'ssh-agent'} | select PathName
Run Code Online (Sandbox Code Playgroud)
如果该Get-WmiObject命令不再有效,您可以使用该Get-CimInstance命令,该命令应该是新版本 PowerShell 的最终继承者
# Create the config file with Powershell
New-Item -Path $HOME\.ssh\config -ItemType File
# Open config File with Notepad
C:\WINDOWS\System32\notepad.exe $HOME\.ssh\config
# or Open file with Visual Code
code $HOME\.ssh\config
Run Code Online (Sandbox Code Playgroud)
之后,您可以使用与 Linux 上相同的语法来根据需要配置 SSH 配置文件
在上一步创建的文件中config添加以下文本:
# Config for use specific key for github
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
# For server 172.x.x.x
Host 172.x.x.x
User user
Port 2121
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
# For all other servers
Host *
User root
Run Code Online (Sandbox Code Playgroud)
现在您可以按如下方式测试您的配置:
# For Github
ssh -T git@github.com
# For other
# It is possible not to put the user to check,
# if you have indicated a specific user
# in the conf file, to test if the configuration
# will connect well with this user
ssh -T 172.x.x.x
Run Code Online (Sandbox Code Playgroud)
如果 ssh 不起作用,这是因为您的环境变量中没有 OpenSSH 文件夹,如果您将
OpenSSH Binary 安装在C:\Program Files
和文件夹中,则可以像在 Powershell 中一样将其添加到系统环境变量中名称是OpenSSH-Win64
# PowerShell admin
# Add folder OpenSSH to your System Environnement
[System.Environment]::SetEnvironmentVariable('OPENSSH', 'C:\Program Files\OpenSSH-Win64', [System.EnvironmentVariableTarget]::Machine)
Run Code Online (Sandbox Code Playgroud)
# Generate EdDSA Key
ssh-keygen.exe -t ed25519 -a 100 -o -C "user@example.com" -f "$HOME\.ssh\id_ed25519_example.com"
# Config the SSH Agent service
# For start the service when logon
Set-Service ssh-agent -StartupType Automatic
# Start the SSH Agent
Start-Service ssh-agent
# Restart service always when you change the config file
Restart-Service ssh-agent
# Add the key to the SSH Agent
ssh-add $HOME\.ssh\id_ed25519_example.com
Run Code Online (Sandbox Code Playgroud)
OpenSSH的配置和关键文件(包括config,known_hosts,authorized_keys,id_rsa,等),其在* nix前往~/.ssh,在Win32的OpenSSH的他们去%USERPROFILE%\.ssh。
这通常是:
C:\Users\username\.ssh
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28113 次 |
| 最近记录: |