Windows上通过ssh和VNC Viewer分别连接本地Ubuntu(WSL)的终端和图形界面

前言

前一篇文章已经讲了如何在windows10中直接下载、配置Ubuntu(以及图形界面);这一篇文章主要讲述:如何在windows上通过ssh以及vncviewer分别连接本地的Ubuntu终端和图形界面。

背景介绍

前一篇文章:
Windows10系统上直接安装、使用Ubuntu系统(不是双系统)
https://dalewushuang.blog.csdn.net/article/details/103376631

现在准备更进一步探索。

1 ssh连接

1.1 报错环节

理论上,直接在windows的cmd中输入:
ssh localhost就行。

但是,我的一直报错:

C:\Users\Administrator>ssh localhost
ssh: connect to host localhost port 22: Connection refused

以及这样的错:

C:\Users\Administrator>ssh -v localhost
OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5
debug1: Connecting to localhost [::1] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file C:\Users\Administrator/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\Users\Administrator/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\Users\Administrator/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\Users\Administrator/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\Users\Administrator/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\Users\Administrator/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\Users\Administrator/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\Users\Administrator/.ssh/id_ed25519-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\Users\Administrator/.ssh/id_xmss type -1
debug1: key_load_public: No such file or directory
debug1: identity file C:\Users\Administrator/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_7.7
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
debug1: match: OpenSSH_7.6p1 Ubuntu-4ubuntu0.3 pat OpenSSH* compat 0x04000000
debug1: Authenticating to localhost:22 as ‘administrator’
debug1: SSH2_MSG_KEXINIT sent
Connection closed by ::1 port 22

还有这样的错:

C:\Users\Administrator>ssh dale@localhost
Connection closed by ::1 port 22
C:\Users\Administrator>ssh dale@127.0.0.1
Connection closed by 127.0.0.1 port 22

1.2 原因

我是怎么找到问题的呢,看了一个网页,说要在Ubuntu本机上ssh localhost一下,如果连不上,就说明openssh-server有问题。(所以问题不在windows。)

原因真的很多。
我大概总结了下:
1)我在上一篇文章中配置Ubuntu的时候,它自动安装了openssh-server这个程序,然而呢,这个程序似乎并没有安装完整,导致我后面ll /etc/ssh的时候发现下面没有key,这太离谱了。
我也不知道为什么会出现这样的情况。

2)我不应该胡乱调整/etc/ssh/ssh_config下的设置,比如port之类的,至少应该提前备份一下(这个确实要注意,修改文件之前记得先备份)

1.3 解决方案

1)在Ubuntu上卸载、重装openssh-server:

sudo apt-get purge openssh-server #先清除这个程序
sudo apt-get update #更新一下,,以防万一
sudo apt-get install openssh-server #再重新下载

sudo service ssh --full-restart #重新启动ssh服务

ssh localhost #仍旧在Ubuntu上运行这个指令,此时发现已经可以连接(需要输入用户、密码这些,还好)

把前面胡乱配置的文件都给重置掉。

2)xshell设置:

两种方案:

  • 直接在xshell中输入 ssh localhost,输入用户密码就能连接
  • 新建一个会话即可,如下:
    在这里插入图片描述

1.4 小结

虽然解决方案很简单,但是解决问题的过程真的不轻松。

错误尝试:
1)对windows各种改,尝试各种ip地址;
2)把windows防火墙也关了,没用
3)各种配置/etc/ssh/ssh_config,改port端口为2222,没用
4)等等其他招,,,很折腾总之。

希望以后不要这么折腾,能够一开始就抓到问题的关键、根本,就好解决了。

2 VNC Viewer连接

1)得先卸载之前的桌面。= =(反正我是这么做的,有可能不需要,毕竟我也是在探索。)

sudo apt-get purge xorg 
sudo apt-get purge xfce4 
sudo apt-get purge xrdp #保险起见,我都卸了。

sudo apt-get remove xubuntu*
sudo apt-get autoremove

2)装Ubuntu原生桌面:

sudo apt-get install ubuntu-desktop #安装桌面软件

sudo apt-get install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal

3)配置vnc server:

sudo apt-get update
sudo apt-get install vnc4server -y #安装vnc4server
vnc4server  #开启vnc服务

nano ~/.vnc/xstartup #把里面内容全部删掉,替换为如下:
#!/bin/sh
# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &
gnome-panel &
gnome-settings-daemon &
metacity &
nautilus &
gnome-terminal &

而后运行:

vncserver -kill :1    #杀掉原来的桌面进程(假设桌面号为:1)
vncserver :1    # 生成新的会话

具体参考:
本地Windows远程连接阿里云服务器图形界面(使用vnc viewer)
https://dalewushuang.blog.csdn.net/article/details/94554206

4)在vnc viewer中新建connection,配置如下:
在这里插入图片描述5)按ok,然后连接,需要输入密码,打开是这样的情况:

在这里插入图片描述

(看着有点怪,没有桌面背景,很尬,但是其他功能都能用。先就这样吧,将就下)

参考文献

[1] 关于apt-get remove 与 apt-get purge
https://www.jianshu.com/p/f6176973b56f

apt-get remove 会删除软件包而保留软件的配置文件
apt-get purge 会同时清除软件包和软件的配置文件

很简单明了的解释,nice。

[2] WSL的openssh-server使用报错:Could not load host key: /etc/ssh/ssh_host_rsa_key
https://blog.csdn.net/zhangpeterx/article/details/95810789

这个激发我去删除openssh-server,然后重装。

[3] 启动sshd时,报“Could not load host key”错
http://blog.chinaunix.net/uid-26168435-id-5732463.html

这个激发我发现openssh-server的问题(没有key),所以需要重装这个程序。

[4] SSH: localhost closes connection after successful login
https://stackoverflow.com/questions/12044958/ssh-localhost-closes-connection-after-successful-login

这个对我无用,我还差点上当,去尝试里面的方法,那时候估计就难以回滚了,幸好稳了一下。

[5] ubuntu16.04 ssh连接出现connect to host localhost port 22: Connection refused 解决办法
https://blog.csdn.net/xiaolan39/article/details/82853455

这个还是比较严谨的,很给力。

[6] Using the Linux Subsystem (WSL) in Windows 10
https://blog.netsarang.com/1884/using-the-linux-subsystem-in-windows-10/

这个博客倒是描述了一个较为完整的流程,但是我照着里面的ssh设置还是没用。

[7] Windows下通过ssh连接Linux
https://blog.csdn.net/G_66_hero/article/details/97971023

教了我一个指令去检查ssh是否运行:ps -e | grep ssh
ps -e | grep ssh
其中 ps -e的含义是:查看全部进程。
grep ssh的含义是:搜索、找到包含ssh的进程。

[8] ssh: connect to host 127.0.0.1 port 2222: Connection refused
https://askubuntu.com/questions/673597/ssh-connect-to-host-127-0-0-1-port-2222-connection-refused

说要改port端口,合理,但是不合适我的情况。

参考文献2

主要是vnc那部分的。

  • https://dalewushuang.blog.csdn.net/article/details/94554206 (本地Windows远程连接阿里云服务器图形界面(使用vnc viewer))
  • https://dalewushuang.blog.csdn.net/article/details/86549600 (给Ubuntu服务器安装图形化界面)
  • https://dalewushuang.blog.csdn.net/article/details/103376631 (Windows10系统上直接安装、使用Ubuntu系统(不是双系统))

原来这个windows中的Ubuntu叫做:

WSL,全称:Windows subsystem Linux。

很有意思。

Tip:如何重启WSL

参考:

方法1:
在这里插入图片描述
具体如下:
在这里插入图片描述
方法二:
在cmd(administrator)中输入:
Get-Service LxssManager | Restart-Service
即可。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值