win10 ubuntu1804子系统开机自启

需求: 希望win10的ubuntu1804子系统开机自启并启动sshd

参考: https://www.jiangfei.net/diannao/1183.html

一、安装ubuntu子系统

这个很简单,不说了,大致步骤:设置中启用适用于linux的Windows子系统-->windows商店安装ubuntu1804-->重启电脑

二、ssh配置

需要先卸载原来的ssh,重新装

参考: https://blog.csdn.net/qq_33317586/article/details/83143248

#卸载ssh server
sudo apt-get remove openssh-server
#安装ssh server
sudo apt update && sudo apt-get install openssh-server -y

修改ssh server配置

#下面是我的sshd配置,我是使用公钥登陆的,如果需要密码登陆,需要设置PasswordAuthentication为yes
[root@david rpms]$ egrep -v '^$|^#' /etc/ssh/sshd_config 
Port 2222
ListenAddress 0.0.0.0
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem	sftp	/usr/lib/openssh/sftp-server
#重启ssh服务,不能使用systemctl启动
[root@david rpms]$ service ssh restart

三、xshell连接ubuntu子系统

此时不出意外已经可以连上了,我是用普通用户登陆的,如果使用root登陆,还应该修改上面sshd_config加上PermitRootLogin yes

四、开启启动设置

在你电脑的 “%AppData%\Microsoft\Windows\Start Menu\Programs\Startup” 目录内创建一个文件,并命名为 *.vbs (任意名称的.vbs文件),文件内容:

set ws=wscript.createobject("wscript.shell")
ws.run "C:\Windows\System32\bash.exe",0
ws.run "C:\Windows\System32\bash.exe  -c 'sudo /usr/sbin/service ssh restart'",0

五、sudo免密设置

我的用户名是david

#如果你创建了好几个用户,要确保该用户在默认的sudo组内
[root@david ~]$ id david
uid=1000(david) gid=1000(david) groups=1000(david),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),114(netdev)
#然后修改/etc/sudoers,加一行
[root@david ~]$ vim /etc/sudoers
...
david   ALL=(ALL) NOPASSWD:ALL

六、此时重启电脑,就可以直接xshell连ubuntu了

以后开机不用手动启动ubuntu子系统