当前位置:首页 > 操作系统 > MacOs

Mac OS利用ssh访问ubuntu虚拟机及免密访问操作

1、将该虚拟机的网口设置成桥接模式(Bridged Adapter),以确保主机可以ping通虚拟机:

Mac OS利用ssh访问ubuntu虚拟机及免密访问操作 - 文章图片

2、在ubuntu虚拟机上安装ssh server:

<code>sudo apt-get install openssh-server</code>

3、找到ubuntu的IP地址,即可以通过在terminal输入:

<code>ifconfig</code>

会出现下面的东西:

Mac OS利用ssh访问ubuntu虚拟机及免密访问操作 - 文章图片
找到拥有inet的那一行,后面的地址即为该虚拟机的IP地址。这里可以看到,该虚拟机的IP地址为10.66.182.88

4、在主机上同样安装ssh。安装好后为了连接虚拟机,可以进行如下操作:

<code>wubijiadeMacBook-Pro:~ wooka$ ssh wu@10.66.182.88
The authenticity of host '10.66.182.88 (10.66.182.88)' can't be established.
ECDSA key fingerprint is SHA256:xxx.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '10.66.182.88' (ECDSA) to the list of known hosts.

wu@10.66.182.88's password:

Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-31-generic x86_64)

* Documentation:  https://help.ubuntu.com/

System information as of Thu Oct 25 22:23:38 HKT 2018

System load:  0.0 Processes: 109
Usage of /: 17.3% of 8.50GB Users logged in: 1
Memory usage: 6%  IP address for eth0: 10.66.182.88
Swap usage: 0%

  
Graph this data and manage this system at:
https://landscape.canonical.com/

New release '16.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Last login: Thu Oct 25 22:23:38 2018 from localhost

wu@ubuntu:~$ ls</code>

这里ssh wu@10.66.182.88wu为虚拟机的用户名

至此可以直接从主机访问虚拟机的文件了。通过输入exit可退出访问。

<code>wu@ubuntu:~$ exit
logout
Connection to 10.66.182.88 closed.</code>

5、然而,麻烦的是,每次主机访问虚拟机时都要输入密码。如何进行免密操作?

为主机和虚拟机共同建立一个共享的密码。

即,可输入ssh-keygen,产生一个public/private密码对。

<code>wubijiadeMacBook-Pro:~ wooka$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/wooka/.ssh/id_rsa): y

# 下面一行代表可否用另一个密码代替之前需要输入的密码,为了方便,可以省略直接回车。
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in y.
Your public key has been saved in y.pub.
The key fingerprint is:
SHA256:xxx
wooka@wubijiadeMacBook-Pro.local
The key's randomart image is:
+---[RSA 2048]----+
|.o . o.. .o  |
|. + o..... o |
|o..o .o.o.+  |
|=o..+Eo+oo . + |
|o+.oooooS = .  |
|..o.. .o . o o |
| o o. . o o  |
|. . .o . o |
| . .+ .  |
+----[SHA256]-----+</code>

现在,我们将pubilc key推送到虚拟机上,即

<code>ssh-copy-id wu@10.66.182.88
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/wooka/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
wu@10.66.182.88's password:

Number of key(s) added:  1

Now try logging into the machine, with: "ssh 'wu@10.66.182.88'"

and check to make sure that only the key(s) you wanted were added.</code>

现在,你控制ubuntu虚拟机的时候,就不需要输入密码了哦!


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐