1、桥接模式

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

2、安装ssh

在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、IP地址

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

ifconfig

会出现下面的东西:

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

4、连接

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

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

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

至此可以直接从主机访问虚拟机的文件了。通过输入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
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]-----+

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

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.

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

6、文件云端运行

假如我在主机端 (如 Macbook) 上写代码. 在我的主目录下写好了一个 Python 脚本 test.py,但我想把该脚本用 ssh 远程推送到旁边空闲的 Linux 去运算。

比如这个 Python 脚本是这样的:

import platform
a = 0
for i in range(999):
    a += 1
print("Finish job, result = %i" %a)
print("This is", platform.system())

如果我要在虚拟机上运行的话,可以这样:

wubijiadeMacBook-Pro:~ wooka$ ssh wu@10.66.182.88 python3 < ~/test.py
Finish job, result = 999
This is Linux

可以看到,文件被推送到了linux上运行了。command中的<表示将右边的文件送入左边的python3中

7、文件云端传输

之前只是文件传送到虚拟机上运行,但是现在我要将文件传送到虚拟机里怎么办呢?比如说我要运行一个py文件,但是该py文件依赖于另一个py文件。这样的话,我必须要把两个文件都放在虚拟机下才能运行。

比如文件 a.py:

# This is b.py
def inner_func():
    print("This is the function in b")

还有一个文件需要调用a.py才能运行:

# This is a.py
from b import inner_func
inner_func()

答案是,在本地主机上输入scp(secure copy),加密传输复制~/{a,b}.py,就可将我的主目录里的两个文件复制到虚拟机的temp的目录中。

wubijiadeMacBook-Pro:~ wooka$ scp ~/{a,b}.py wu@10.66.182.88:~/temp
a.py  100% 53  65.4KB/s 00:00
b.py  100% 69  89.5KB/s 00:00

接下来,执行虚拟机上被传送的文件。这时候,相当于在ssh的时候发送一条指令去执行a.py,这条指令用" "框起来,说明是要发送去云端再执行的指令。

wubijiadeMacBook-Pro:~ wooka$ ssh wu@10.66.182.88 "python3 ~/temp/a.py"
This is the function in b

接下来,对于云端虚拟机上产生的结果,我们要返回到本机中怎么办?其实,只要把scp的两个参数改变一下位置就可以了。

wubijiadeMacBook-Pro:~ wooka$ scp wu@10.66.182.88:~/temp/a.py ~/Desktop/
a.py  100% 53  72.8KB/s 00:00

Reference:

[https://www.youtube.com/watch?v=SST5xl4SVA8&index=14&list=PLXO45tsB95cIiLTNZu-v3Y-xotBAjtH2x]

版权声明:本文为bjwu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/bjwu/p/9853498.html