将该虚拟机的网口设置成桥接模式(Bridged Adapter),以确保主机可以ping通虚拟机:
2、安装ssh 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.88,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 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()