win10安装WSL open-ssh
# wsl版本选择
- 电脑性能不好选wsl1,性能好选wsl2或者虚拟机。
- wsl1选Ubuntu-20.04, Ubuntu-22.04的gdb有点问题。
- pwershell执行
wsl -l -v
查看安装的wsl版本
# win10安装wsl后设置root为默认登录账户
- 应用商店安装Ubuntu-20.04,启动后设置普通用户的用户名和密码
- sudo passwd root, 设置root密码
- 先powershell停止wsl服务
net stop lxssmanager
- 再设置默认登录为root
/c/Users/用户名/AppData/Local/Microsoft/WindowsApps/ubuntu2004.exe config --default-user root
- 重新进入wsl
# 设置软连接方便访问
可以将常用目录软连接至/root下面,方便切换 比如常用目录为/mnt/c/dorit
ln -s /mnt/c/dorit/ /root/dorit
# 切换至root后,可安装以下常用软件:
sudo apt-get update
sudo apt-get install gcc
sudo apt-get install gdb
2
3
4
如果安装源连不上,或者连接速度太慢,可尝试更换apt源 https://www.cnblogs.com/codingbigdog/p/16324800.html (opens new window) 20.04STL源
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
2
3
4
5
6
7
8
9
10
11
12
13
# win10安装wsl-ssh
windows 自带的ssh服务器功能貌似有问题,可以先安装wsl,再在wsl上安装ssh
# 安装WSL:
- https://www.jianshu.com/p/cd4d604e0b44
- windows添加或删除功能,开启WSL
- 从windows应用商店里安装ubantu STL
# ubantu安装ssh
https://blog.csdn.net/qq_38558270/article/details/88531795
- 先设置apt 安装源
# 备份原来的源配置
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
# 编辑
sudo vim /etc/apt/sources.list
# 替换为下面的内容
deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
# 更新软件列表
sudo apt-get update
# 更新软件
sudo apt-get upgrade
# 更新软件(自动处理依赖):
sudo apt-get dist-upgrade
# 下载中文语言包:
sudo apt-get install language-pack-zh-han*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
- 然后卸载自带的,重新安装,因为自带貌似有问题
sudo apt-get remove openssh-server
sudo apt-get remove openssh-client
# 这里很可能安装错误,如果出错看下面的方法
sudo apt-get install openssh-server
sudo apt-get install openssh-client
2
3
4
5
6
安装ssh如果提示错误:E: Could not read response to hello message from hook [ ! -f /usr/bin/snap ]
, 则执行
sudo rm -rf /etc/apt/apt.conf.d/20snapd.conf
- 修改配置文件:
# 备份
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# 修改配置文件
sudo vim /etc/ssh/sshd_config
# 修改下面的内容。 Port设置成2022,是为了不占用windows的22端口。看自己喜好
Port 2022
PasswordAuthentication yes
PermitRootLogin yes
# 修改完成后重启服务
sudo service ssh restart
# ssh加入开机自启动。 注意wsl这样设置似乎没用。想开机启动参考下面的方法。
sudo systemctl enable ssh
# 或者下面的
vi /etc/rc.local
加入:service ssh start
# 注意上面的服务名在ubuntu上是ssh,在centos上是sshd
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 其他设置
- 设置root密码
sudo passwd root
- 设置wsl开机启动
参考 https://zhuanlan.zhihu.com/p/47733615 里面的评论,新建
wsl_start.bat
脚本放在startup文件夹。该脚本即启动了wsl又启动了wsl内的ssh
wsl_start.bat
wsl -u root /etc/init.d/ssh start
# win10安装官方的ssh
之前安装了ssh后vscode的remote-ssh不能用,现在更新后可以了,remote ssh连上的时候回提示选windows/linux。
# windows安装open-ssh官方文档
https://docs.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_install_firstuse
在windows应用-可选功能里安装ssh server
- 默认安装在
C:/Program Files/OpenSSH/
- 配置文件:
C:\ProgramData\ssh
安装完成后管理员打开powershell,启动ssh服务,配置防火墙等
Start-Service sshd
# OPTIONAL but recommended:
Set-Service -Name sshd -StartupType 'Automatic'
# Confirm the Firewall rule is configured. It should be created automatically by setup.
Get-NetFirewallRule -Name *ssh*
# There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled
# If the firewall does not exist, create one
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
2
3
4
5
6
7
8
# 登陆失败时新建用户
默认登陆时用户名和密码可以用windows的账户,如果登陆失败,可尝试在 计算机管理-本地用户和组
中新建用户,赋予管理员权限,然后登陆。
# bad owener or permission on .ssh/config
参考 https://www.cnblogs.com/microestc/p/10840315.html
将~/.ssh/config剪切至c:/programdata/ssh/ssh_config (config重命名为ssh_config)
# id_rsa免密登陆
参考 https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement
假设需要从某个客户单电脑用户zhangsan连接到服务器电脑用户school上
在客户端zhangsan个人文件夹下创建公钥秘钥对
# cd到当前用户目录下的.ssh文件夹, 比如C:\Users\zhangsan\.ssh cd ~\.ssh\ # 创建密钥对, 会生成id_rsa, id_rsa.pub ssh-keygen
1
2
3
4
5复制id_rsa.pub到服务器school用户文件夹下~.ssh\authorized_keys上。如果authorized_keys不存在则创建,如果已存在就追加
服务端启动ssh-agent服务,然后管理员运行powershell,执行ssh-add
cd ~\.ssh\ ssh-add id_rsa
1
2如果提示id_rsa权限too open,可在其'文件属性-安全'中修复id_rsa权限:
属性---安全---高级---禁用继承---保留转换,然后删除其他用户权限,只保留当前用户、administrators、SYSTEM。(当前用户可能不需要勾选完全控制)
同时对authorized_keys也修复权限:
客户端如果配置了
.ssh\config
,并且在读取的时候有问题,也用同样的方法对其修复权限。在客户端尝试使用
ssh -i
登陆,如果仍要求输入密码,可尝试修改sshd_config。ssh -i ~/id_rsa school@localhost
1参考 https://www.cnblogs.com/surplus/p/12466466.html
以管理员权限打开 PowerShell,执行命令
notepad C:\ProgramData\ssh\sshd_config
1注释掉配置文件中的最后两行然后保存:
#Match Group administrators # AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
1
2最后在服务管理器器中重启 OpenSSH SSH Server 服务,然后客户端就可以通过公钥认证的方式登录到远程服务器了。
注意: 一定不要用 Repair-AuthorizedKeyPermission 命令修复 .ssh\authorized_keys 文件的权限。 也不要用
echo "publickey" > .ssh\authorized_keys
的方式创建 .ssh\authorized_keys 文件: 因为不需要用命令修复,所以不用安装Install-Module -Force OpenSSHUtils -Scope AllUsers
,也不用管这个安装的时候签名错误的问题。
# 再之前安装失败的过程
可能是因为win10之前自己的bug,所以不成功。
# 修复.ssh/config权限
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
import-Module E:\OpenSSH-Win64\OpenSSHUtils.psm1
Repair-UserSshConfigPermission ~/.ssh/config
Get-ChildItem ~\.ssh\* -Include "id_rsa","id_dsa" -ErrorAction SilentlyContinue | % {
Repair-UserKeyPermission -FilePath $_.FullName @psBoundParameters
}
2
3
4
5
6
7
# vscode remote-ssh主页:
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh
# win10 open-ssh 秘钥登录
$REMOTEHOST="qiang.chen@localhost"
scp "$env:USERPROFILE\.ssh\siye_rsa.pub" "${REMOTEHOST}:~/tmp.pub"
ssh "$REMOTEHOST" "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat ~/tmp.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && rm -f ~/tmp.pub"
2
3
4
# ssh命令,指定秘钥文件登录:
https://www.ktanx.com/blog/p/4016
ssh -i ~/.ssh/liyddsshkey developer@192.168.1.237 -p 23
2
# powershell 签名验证码错误
https://jingyan.baidu.com/article/36d6ed1f6e18b61bcf4883de.html
set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy Unrestricted
2
这个试了不行。
另一种解放方法: https://www.cnblogs.com/gs0724/p/11763640.html 里面提到了win64-openssh的下载GitHub地址
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
import-Module E:\OpenSSH-Win64\OpenSSHUtils.psm1
Repair-UserKeyPermission ~/.ssh/id_rsa-siye
Repair-UserSshConfigPermission ~/.ssh/config
Repair-AuthorizedKeyPermission ~\.ssh\authorized_keys
2
3
4
5
6
7