Xshell连接虚拟机中的Ubuntu

配置虚拟机网络
  1. 设置网络适配器连接为 桥接
    在这里插入图片描述

  2. ping baidu.com,看一下虚拟机是否可以联网。可以ping通说明虚拟机已经可以联网,配置没有问题

    root@ubuntu:~$ ping www.baidu.com
    PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.
    64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=1 ttl=45 time=29.8 ms
    64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=2 ttl=45 time=28.8 ms
    64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=3 ttl=45 time=28.8 ms
    64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=4 ttl=45 time=30.2 ms
    
  3. 获取当前虚拟机的ip。

    root@ubuntu:~$ ifconfig
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.10.119  netmask 255.255.255.0  broadcast 192.168.10.252
            inet6 fe80::dc87:af4d:a711:a1c4  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:04:3e:bf  txqueuelen 1000  (Ethernet)
            RX packets 11261  bytes 3981183 (3.9 MB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 2072  bytes 199955 (199.9 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    

    当前获取的IP 一般是动态IP。Ubuntu 下可以通过命令

    root@ubuntu:~$ cat /etc/network/interfaces
    # interfaces(5) file used by ifup(8) and ifdown(8)
    auto lo
    iface lo inet loopback
    
    auto ens33  
    # 此时为 dhcp ip动态分配
    iface ens33 inet dhcp
    
  4. 本地系统使用cmd命令窗口,如果可以ping通(192.168.10.119)说明已经可以连接了。

    在这里插入图片描述
    如果在本地可以ping通 虚拟机的ip,就可以通过xshell连接了。

如果没有ping通可以看一下linux防火墙是否关闭。

Xshell 连接

一般我们连接linux时使用ssh连接协议。

理想的步骤

点击【文件】->【新建】打开新建会话属性弹框,输入虚拟机的主机ip。
在这里插入图片描述

切换选项卡【用户身份验证】。输入用户名,密码
在这里插入图片描述

需要注意的是该用户名不是系统的登录用户名。
在这里插入图片描述
而是新建一个命令窗口后通过who指令显示的当前用户名。

root@ubuntu:~$ who 
root:0           2019-12-10 23:50 (:0)

本例就是: root

设置完属性 点击【确定】按钮就不出意外可以连接上了。
在这里插入图片描述

意外后的步骤
Connection failed

如果出了意外呢??
在这里插入图片描述
目前就处于 ip可以ping通,但是xshell连接失败。



  1. 检查虚拟机 ssh是否启动。

    root@ubuntu:~$ ps -e | grep ssh
      1405 ?        00:00:00 ssh-agent
    

    没有看到sshd就说明未启动,选择下面的一种方式手动启动就好了

    sudo service sshd start
    sudo /etc/init.d/ssh start
    

    正常启动没有提示(可以ps查看是否启动)。如果未安装则会报出以下错误

    Failed to start sshd.service: Unit sshd.service not found.
    

    使用下面的命令安装即可,安装过程中可能因为(openssh-client)版本不兼容的问题。

    sudo apt install openssh-server
    

    安装成功后默认就会启动服务。

  2. 如果ssh已启动,还是无法连接,那么需要查看ssh的配置文件。监听端口号等信息是否修改

    cat /etc/ssh/ssh_config 
    

    SSH远程登录配置文件sshd_config详解

出现弹框

在这里插入图片描述
用户名或者密码有误。请检查用户名、密码是否正确。

尤其是用户名 要保证和 打开终端(Terminal)时前面提示的用户名一致。

root@ubuntu:~$ 

本例就是:root

如果要修改密码,点击左上角菜单【文件】–>【打开】。弹出以下弹框
在这里插入图片描述
选中要修改的连接右键点击【属性】,弹出会话框。切换【用户身份验证】,修改用户名、密码
在这里插入图片描述

错误
安装 openssh-server错误
root@ubuntu:~$ sudo apt install openssh-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 openssh-server : Depends: openssh-client (= 1:7.6p1-4ubuntu0.3)
                  Depends: openssh-sftp-server but it is not going to be installed
                  Recommends: ssh-import-id but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

这是因为openssh-server 依赖于openssh-client 。ubuntu是自带openssh-client的但是版本不匹配,所以在蛞后后面给出了它依赖的版本(= 1:7.6p1-4ubuntu0.3)。

这样我们只需要安装1:7.6p1-4ubuntu0.3版本的openssh-client就可以了。

root@ubuntu:~$ sudo apt install openssh-client=1:7.6p1-4ubuntu0.3

之后就会打出一些日志信息,并询问你是否需要继续。输入 y(不区分大小写)就会进行依赖包的下载

After this operation, 4132 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

之后再安装 openssh-server 就可以了。

安装完成后,查看进程信息,如果显示sshd 则说明成功安装openssh-server。

root@ubuntu:~$  ps -e |grep ssh
  1405 ?        00:00:00 ssh-agent
  4593 ?        00:00:00 sshd

参看文章:
Xshell连接虚拟机中的Ubuntu

  • 33
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值