我有一个Ubuntu虚拟机运行在我的Windows 7机器上。我如何设置它,以便我可以通过SSH从外部访问web服务器?
我发现步骤(在VirtualBox主机和客户虚拟机之间设置SSH访问)能够从我的主机SSH到我的客户,但这仍然留给我通过路由器访问它的问题。
我想我可以在我的Windows机器上安装一个SSH服务器,然后隧道几次(虽然我不是100%确定在本地,动态等方面使用什么,或者如何设置多个隧道?),但是有没有一种方法可以让虚拟机直接访问我的路由器,这样我就可以直接端口转发到它?
我有一个Ubuntu虚拟机运行在我的Windows 7机器上。我如何设置它,以便我可以通过SSH从外部访问web服务器?
我发现步骤(在VirtualBox主机和客户虚拟机之间设置SSH访问)能够从我的主机SSH到我的客户,但这仍然留给我通过路由器访问它的问题。
我想我可以在我的Windows机器上安装一个SSH服务器,然后隧道几次(虽然我不是100%确定在本地,动态等方面使用什么,或者如何设置多个隧道?),但是有没有一种方法可以让虚拟机直接访问我的路由器,这样我就可以直接端口转发到它?
当前回答
登录Linux VirtualBox虚拟机的最佳方式是端口转发。缺省情况下,您应该已经有一个正在使用NAT的接口。然后进入“网络设置”,单击“端口转发”按钮。添加一个新规则。插入“ssh”作为规则名。作为“主机端口”,插入3022。作为“来宾端口”,插入22。规则的其他部分可以留空。
或者从命令行
VBoxManage modifyvm myserver --natpf1 "ssh,tcp,,3022,,22"
其中“myserver”是创建的虚拟机名称。查看已添加的规则:
VBoxManage showvminfo myserver | grep 'Rule'
这是所有!请确保您不要忘记在虚拟机中安装SSH服务器:
sudo apt-get install openssh-server
SSH进入客户虚拟机,如下所示:
ssh -p 3022 user@127.0.0.1
其中user是您在虚拟机中的用户名。
其他回答
保留NAT适配器并添加第二个仅限主机的适配器效果非常好,而且对于笔记本电脑(外部网络总是在变化)至关重要。
http://muffinresearch.co.uk/archives/2010/02/08/howto-ssh-into-virtualbox-3-linux-guests/
记住要在virtualbox本身中创建一个主机专用网络(GUI -> settings -> network),否则您无法在客户端上创建主机专用接口。
优麒麟 18.04 LTS
配置桥接以查看服务器ip,并且连接时没有“端口转发”
VirtualBox >右键单击服务器>设置> Network > enable适配器2 > select " bridging " >混杂模式:允许所有>检查>启动服务器连接的线缆
在ubuntu服务器上编辑sudo nano /etc/netplan/*init.yaml文件,
我的示例文件:
network:
ethernets:
enp0s3:
addresses: []
dhcp4: true
enp0s8:
addresses: [192.168.0.200/24]
dhcp4: no
dhcp6: no
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
version: 2
这些命令将帮助你
nano /etc/netplan/file.yaml # file to specify the rules of network
reboot now # restart ubuntu server right now
netplan apply # do after edited *.yaml, to apply changes
ifconfig -a # show interfaces with ip, netmask, broadcast, etc...
ping google.com # to see if there is internet
在Ubuntu 18.04 LTS服务器上配置静态IP地址-使用NetPlan
在主机上使用putty(不支持端口转发)登录运行在虚拟机中的ubuntu虚拟机:
On Virtualbox manager select the vm, click on settings icon. Then go Networks and enable two adaptors as below: Adaptor 1 (For internet access): Attached to -> NAT, Advanced -> Check the cable connected. Adaptor 2: Attached to -> Host only adaptor, Advanced -> Check the cable connected and Promiscuous mode -> Allow all. Start the ubuntu vm. Login to the VM as root. Edit the file '/etc/network/interfaces' as below and save it: auto lo iface lo inet loopback auto eth0 iface eth0 inet dhcp auto eth1 iface eth1 inet dhcp Restart the VM. Login to the VM and run below command to check the IP allocated to eth1: ifconfig Use this IP to open putty session for the VM.
如何为Solaris 10和Ubuntu 16.04做主机专用网络(比桥接更好)
添加纯主机接口
Virtualbox >文件>参数>网络>主机专用网络>添加 关闭虚拟机。 虚拟机设置>网络。第一个适配器应该是Nat,第二个适配器只能是主机。 打开cmd.exe,执行ipconfig /all命令。你应该看到以下几行: 以太网适配器VirtualBox主机专用网络: ... IPv4地址. . . . . . . . . . .: 192.168.59.1 guest中的第二个适配器也应该在192.168.59.*中。 启动虚拟机。
Solaris 10
检查ifconfig -a设置。您应该看到e1000g0和e1000g1。我们对e1000g1感兴趣。 Ifconfig e1000g down Ifconfig e1000g 192.168.56.10 netmask 255.255.255.0 up 从主机上检查该接口是否可达:ping 192.168.56.10
重新启动时保留这些设置
# vi /etc/hostname.e1000g1
192.168.56.10 netmask 255.255.255.0
# reboot
配置ssh服务(管理)以root身份登录(不建议)
检查ssh是否开启
# svcs -a | grep ssh
online 15:29:57 svc:/network/ssh:default
修改/etc/ssh/sshd_config
PermitRootLogin yes
重启ssh服务
svcadm restart ssh
从主机检查它
ssh root@192.168.56.10
Ubuntu 16.04
列表界面:
ip addr
您应该看到像lo、enp0s3、enp0s8这样的三个接口。我们将使用第三种。
编辑/etc/network/interfaces
auto enp0s8
iface enp0s8 inet static
address 192.168.56.10
netmask 255.255.255.0
然后sudo ifup enp0s8。检查enp0s8的地址是否正确。你应该看到你的ip:
$ ip addr show enp0s8
...
inet 192.168.56.10/24 brd 192.168.56.255 scope global secondary enp0s8
如果不是,可以运行sudo ifdown enp0s8 && sudo ifup enp0s8
https://superuser.com/questions/424083/virtualbox-host-ssh-to-guest/424115#424115
VirtualBox文档中有一个关于如何配置NAT端口转发的很好的解释: http://www.virtualbox.org/manual/ch06.html#natforward