[Virtual OS] VirtualBox NAT + Host-Only Adapter Network Configuration / Solution When SSH from Host OS Fails

Tadashi Shigeoka ·  Sat, September 3, 2011

I installed CentOS 5.6 as a virtual server on VirtualBox, but encountered a problem where I couldn’t SSH from the host OS.

I managed to solve it with advice from @yak0u, so I’ll share the method and network configuration.

VirtualBox Network Configuration

First, for VirtualBox network configuration, you can refer to this:

Network Configuration in Virtual Environment VirtualBox : Asial Blog

Virtual OS Firewall Configuration

First, change the firewall configuration. Refer to “iptables Verification” on the following site for configuration:

Installing CentOS on VMware Player. Building Practice Web Server 4

# vi /etc/sysconfig/iptables
# SSH, HTTP, FTP1, FTP2, MySQL
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 20 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

If port 22 is closed, SSH communication is impossible, and if port 80 is closed, browsers cannot connect to web pages.

After editing, restart iptables:

# /etc/rc.d/init.d/iptables restart

Network Configuration on Virtual OS

First, read this excellent article that explains network configuration:

Network Configuration When Using VirtualBox - Yudoufu Log

Next, use the ping command to find available local IP addresses to assign to the host-only adapter:

How to Use Ping in Mac Network Utility App to Check Network Availability / Inforati

$ ping -c 5 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3

--- 192.168.1.1 ping statistics ---
5 packets transmitted, 0 packets received, 100.0% packet loss

After starting the virtual machine in VirtualBox, run the ifconfig command on the host OS (Mac):

.
.
.
vboxnet0: flags=8843 mtu 1500
  ether 0a:00:27:00:00:00 
  inet 192.168.1.1 netmask 0xffffff00 broadcast 192.168.1.255

I found that 192.168.1.1 is being used by VirtualBox’s vboxnet0.

Also, when I ran the ping command again on the host OS (Mac), this time there was a response:

$ ping -c 5 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=0.052 ms
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.145 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.125 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.055 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.118 ms

--- 192.168.1.1 ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.052/0.099/0.145/0.038 ms

Similarly, running the ping command on the guest OS (virtual machine) also got a response.

This confirmed that the IP address “192.168.1.1” is properly being used by the virtual machine.

Fix local IP address to eth1 corresponding to host-only adapter

Fix the IP address so that the IP corresponding to the host-only adapter doesn’t change every time the virtual machine starts.

You should be able to check the IP address currently assigned to eth1 with the ifconfig command and set that as the fixed IP without problems.

・Reference:Local Development Environment with VirtualBox 2.2 and CentOS 5.3 - Bean Sprout Diary

In my case, eth1 wasn’t displayed even when I ran the ifconfig command.

So, I edited eth1.

First, verify that local IP address 192.168.1.2 isn’t being used.

(If it’s being used, check available local IP addresses in order: 192.168.1.3, 192.168.1.4, …)

Once you find an available local IP address, write the IP address in ifcfg-eth1:

[root@localhost ~]# emacs /etc/sysconfig/network-scripts/ifcfg-eth1

# Intel Corporation 82540EM Gigabit Ethernet Controller
DEVICE=eth1
BOOTPROTO=static                    # Changed from dhcp to static
HWADDR=08:00:27:F7:6C:DE
ONBOOT=yes
IPADDR=192.168.1.2                   # Added
NETMASK=255.255.255.0            # Added

After editing eth1, apply the configuration:

[root@localhost ~]# ifdown eth1
[root@localhost ~]# ifup eth1

I was able to connect from the host OS (Mac) to the host OS (virtual machine) using the ssh command.

How to SSH Connect with Local IP - Makotonon’s Hidamari-zumu

$ ssh [email protected]
ssh: connect to host 192.168.1.1 port 22: Connection refused

$ ssh [email protected]
The authenticity of host '192.168.1.2 (192.168.1.2)' can't be established.
RSA key fingerprint is 48:8e:56:2f:95:59:d9:d9:c0:af:cb:6f:ef:ea:ee:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.2' (RSA) to the list of known hosts.
[email protected]'s password: 
Last login: Wed May 11 13:30:46 2011
[root@localhost ~]#

That’s all.

That’s all from the Gemba.