SSH 相关
 SSH 相关 
 在Windows上使用sshd
- sudo service ssh
- sudo nano /etc/ssh/sshd_config修改(参考)- 1 2 3 4 5 - Port 2222 # Windows的22端口已经被占用了, 最新版本用不着, 但是要加防火墙入站规则 AllowUsers yourusername # 允许登陆的用户名, 就是WSL的用户名, 不写就不指定, 最新版本用不着 PermitRootLogin no # 最新版本用不着 UsePrivilegeSeparation no # 这一句话需要改一下, 看看能否避免出现bash->ssh->bash出现错误提示的情况(据说是因为无法获取权限).最新版本用不着 PasswordAuthentication yes # 最新版本也用不着 
- Windows防火墙设置允许2222端口, 或者用提权的cmd命令
 netsh advfirewall firewall add rule name="SSHServer" dir=in action=allow localport=2222 protocol=tcp
- 如果找不到RSA key, 要在bash中sudo ssh-keygen -A
- sudo service ssh --full-restart
- ssh密钥添加
 alias:nano ~/.bash_aliases,alias command='complicated command'
 ssh-keygen -t rsa
 ssh-copy-id -i ~/.ssh/id_rsa.pub username@$ip
 如果只有pwsh:$IP='user@server'; type $env:USERPROFILE\.ssh\id_rsa.pub | ssh ${IP} "cat >> .ssh/authorized_keys"(credit: Christopher Hart)
- 权限要求1 2 3 $ chmod go-w $HOME $HOME/.ssh #要求这两个文件夹group和other不能有写权限 $ chmod 600 $HOME/.ssh/authorized_keys $ chown `whoami` $HOME/.ssh/authorized_keys 
- 当设置好了id_rsa之后, 会发现打开命令行输入ssh登陆不需要密码, 但是双击sh文件的方式仍然需要密码
 通过ssh -v模式可以看出, sh文件查找id_rsa文件不是在~/.ssh路径下, 而是%HOMEPATH%\.ssh中, 所以把文件复制过来就可以不用输入密码登陆ssh了.
- 输入~.可以退出远程服务器.
- 遇到REMOTE HOST IDENTIFICATION HAS CHANGED!提示, 说明服务器端的.pub文件更改了. 相关文件在/etc/ssh(*.pub)或者~/.ssh(known_hosts)文件夹中(参考), 替换的时候注意权限
 对于Windows自带的OpenSSH, 应用程序(ssh.exe)在%windir%\System32\OpenSSH; sshd_config和*.pub在%ProgramData%\ssh; known_hosts在%USERPROFILE%\.ssh.
对于Windows 11
- 安装OpenSSH
 设置→应用→可选功能→OpenSSH客户端, OpenSSH服务器
 验证:1 2 3 4 Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Ser*' # 返回 # Name : OpenSSH.Server~~~~0.0.1.0 # State : Installed 
- 设置OpenSSH Server
 有两个服务: ssh-agent, sshd1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 # 启动服务 Start-Service sshd # 设置开机启动 Set-Service -Name sshd -StartupType 'Automatic' # # 设置密码 # Set-LocalUser -Name $env:USERNAME -Password (ConvertTo-SecureString -AsPlainText "yourpassword" -Force) # # 设置允许密码登陆 # New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name "PasswordAuthentication" -Value 1 -PropertyType DWORD -Force # # 设置允许远程连接 # New-NetFirewallRule -DisplayName "OpenSSH Server (sshd)" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 22 -EdgeTraversalPolicy Allow # 验证服务开启 netstat -nao | find /i '":22"' # 返回 # TCP [::]:22 [::]:0 LISTENING 4 # 检查防火墙 Get-NetFirewallRule -Name *OpenSSH-Server* |select Name, DisplayName, Description, Enabled # 返回 # Name DisplayName Description Enabled # ---- ----------- ----------- ------- # OpenSSH-Server-In-TCP OpenSSH SSH Server (sshd) Inbound rule for OpenSSH SSH Server (sshd) True 
- 设置OpenSSH的默认shell
 使用管理员权限打开powershell, 输入以下命令1 2 3 4 5 6 # 设置默认shell New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name "DefaultShell" -Value (Get-Command pwsh).Path -PropertyType String -Force # # 设置默认用户 # New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name "DefaultUser" -Value "yourusername" -PropertyType String -Force # # 重启服务 # Restart-Service sshd 
- 获得本地账号信息1 2 whoami # INTELNUC01\sungkim 密码为Microsoft账号密码 
 如果是管理员账号, 使用authorized_keys时, 将本地的*.pub文件放入服务器的%PROGRAMDATA%\ssh\administrators_authorized_keys中
 为保证权限正确,icacls.exe .\* /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
- 如果要更改进入server的路径, 需要修改%PROGRAMDATA%\ssh\sshd_config中ChrootDirectory d:\
- 测试1 ssh 'INTELNUC01\sungkim@localhost' 
隧道
- ssh -t brainor@隧道电脑 -p 2222 ssh ouwk@服务器(ref)
 ssh username@目标机器IP -p 22 -o ProxyCommand='ssh -p 22 username@跳板机IP -W %h:%p'
- local forwarding
 ssh -L a:B:c D
 从本地a端口出发, 通过D, 到达B的c端口; 访问本地a端口相当于访问B的c端口. 若B=localhost, 则访问本地a端口相当于访问D的c端口
- reverse remote forwarding(ref)
 ssh -R a:B:c D
 从D的a端口出发, 通过本地, 到达B的c端口. 若B=localhost, 则访问D的a端口相当于访问本地的c端口
- dynamic forwarding(ref)
 ssh -D8080 -fCqN user@server
 f: 进入后台, C: 数据压缩, q: 安静模式, N: 不执行远程命令
- 反向动态转发
 ssh -R 12345 user@server
 在server上使用代理
 env ALL_PROXY=socks5h://localhost:12345 PROGRAM [OPTION]...
 env http_proxy=socks5h://localhost:12345 HTTPS_PROXY=socks5h://localhost:12345 ALL_PROXY=socks5h://localhost:12345 PROGRAM [OPTION]...
 本文由作者按照  CC BY 4.0  进行授权