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

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

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

sudo apt-get install openssh-server

安装结束后,确认本机ssh服务是否打开,输入

ps -e | grep ssh

如果看到sshd说明ssh服务已经打开了,如果没有sshd,可以输入以下命令开启ssh服务:

sudo /etc/init.d/ssh start

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

ifconfig

会出现下面的东西:


找到拥有inet的那一行,后面的地址即为该虚拟机的IP地址。这里可以看到,该虚拟机的IP地址为10.66.182.88

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

wubijiadeMacBook-Pro:~ wooka$ ssh [email protected]
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.

[email protected]'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

这里ssh [email protected]wu为虚拟机的用户名。

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

wu@ubuntu:~$ exit
logout
Connection to 10.66.182.88 closed.

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

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

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

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
[email protected]
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]-----+

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

ssh-copy-id [email protected]
/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
[email protected]'s password:

Number of key(s) added:  1

Now try logging into the machine, with: "ssh '[email protected]'"

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

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

猜你喜欢

转载自www.cnblogs.com/bjwu/p/9853498.html