Connection refused when attempting SSH to Oracle VirtualBox guests from the host PC. Also Connection Timed Out.

First
It took me FOREVER to solve this problem. So listen carefully. THE first thing that you should do, after downloading and installing 2 Oracle VirtualBox servers, with either Centos 7 or Oracle Enterprise Linux 7 (both are FREE), is view the tutorial, on YouTube below.

linux tutorial for beginners 19 Static IP Address and Networking in RHEL 7


Know your command line commands.

ifconfig, in linux, is the equivalent of ipconfig /all in Windows.

Notice that he leaves one interface as DHCP and changes the second one to manual. If you are getting confused about which is which, look at ifconfig to see which is coming up first. That's your DHCP/NAT address. It will be something like 10.0.2.15. And its completely unimportant if both VMs have the same DHCP address. They will NOT interfere with each other if the IP addresses are the same.

Second

With 2 network interfaces configured properly, you WILL be able to SSH/WinSCP to them AND be able to access the Internet at the same time.

As the above tutorial shows, you will want to make the first interface as a NAT interface; as shown below.

Third

Make the second adapter either Bridged Adapter or Host Only Adapter. Either will work. In the video above he shows Host Only. I use Bridged on my connection. Both the above and below interfaces are available by clicking on one of the guest virtual machines and then on the yellow settings gear shown above and below in the VirtualBox Manager (shown). The VMs will need to be turned off to make changes in this area.

Fourth

The NAT interface will stay as DHCP.

Set the second interface (Host Only or Bridged) to be manual, like is shown in the video. That is, give the second  interface an IP address which is in the same IP range as your host PC; something like 192.158.x.x. If your host is a Windows PC, type ipconfig /all at the command line to get your IP address.

The subnet mask is almost always to stay at /24 or 255.255.255.0. That's 24 bits filled with ones.


Fifth

Configure OpenSSH server
 
To configure an OpenSSH server:
 
    Install or update the openssh and openssh-server packages:
 
    # yum install openssh openssh-server
 
    Start the sshd service and configure it to start following a system reboot:
 
    # systemctl start sshd
    # systemctl enable sshd
 
You can set sshd configuration options for features such as Kerberos authentication, X11 forwarding, and port forwarding in the /etc/ssh/sshd_config file.
 
Edit the ssh config file by typing "vi /etc/ 

Sixth

Disable SELinux

 
sestatus  -----------------  check SELinux status

vi /etc/sysconfig/selinux - open selinux configuration file

Change “SELINUX=enforcing” to “SELINUX=disabled” and save the configuration file

reboot

sestatus ------------------ repeating this, after rebooting, should show disabled.

Seventh

 Change The IP Address, Hostname and Network Configuration

Login to linux server as root, pen terminal and and edit “/etc/sysconfig/network”

vi /etc/sysconfig/network

#Change HOSTNAME

NETWORKING=yes

HOSTNAME=zeus

GATEWAY=192.168.1.1


Eighth

Update /etc/hosts file

vi /etc/hosts

Make sure that hosts file have the right entries (remove or comment out lines with ipv6), make sure there is correct IP and hostname, edit /etc/hosts as root:

127.0.0.1           localhost.localdomain  localhost

192.168.2.120   zeus.zion.local    zeus


Ninth

Alter IPADDR and remove HWaddr, edit /etc/sysconfig/network-scripts/ifcfg-eth0

After disabling SELinux and setting the two interfaces with the nmtui interface; as shown in the video...

Go back into the /etc/sysconfig/network-scripts/ifcfg-eth0 file with an editor, as in "vi /etc/sysconfig/network-scripts/ifcfg-eth0" and look and make sure that the interface is set to be eth0 and that an IP ADDRESS is shown there.

If it says eth1, change it to eth0. If there's no IP address, put one in as per the example below.

vi /etc/sysconfig/network-scripts/ifcf-eth0

TYPE=Ethernet

BOOTPROTO=none

#HWADDR=00:33:E1:2A:FA:D8 # comment out or remove this line

IPADDR=192.168.2.120

PREFIX=24

GATEWAY=192.168.2.1

DNS1=8.8.8.8

DEFROUTE=yes

IPV4_FAILURE_FATAL=yes

IPV6INIT=no

NAME=eth0

ONBOOT=yes

The DNS address is probably unimportant.

Your gateway should be the cable modem, or other router, that you use to connect to the internet.

By this point it should be working.

If not, go back and ping each VM from the other VM.

And ping both (guest) VMs from the the host PC.

Remember you're pinging the IP addresses that are in the same range, on the 2 guest VMs, as that which is on your host PC; not the DHCP IP addresses that are probably something like 10.0.2.15

And, as I said before, the 10.0.2.15 / DHCP address CAN be the same IP address on both VMs. Having them the same won't cause any issues. You can verify this be reading the official VirtualBox documentation out on the web. 


Tenth

Configuring Network Settings

This section is included strictly for informational purposes. None of it is absolutely necessary.

I include things like ssh 127.0.0.1, the local address, which you can use to see if SSH is working on each VM.

I also include commands for giving the VM a hostname. As long as you use IP addresses everywhere, this isn't absolutely necessary. But it would be done on a job. So do it regardless.

If you are using something other than Centos 7 or Oracle Enterprise Linux 7, your network interface configuration file may be in a different location with that Linux distro.

#/bin/hostname – will give you the computer name

#/bin/hostname New_server_name – will rename your server.

#ifconfig – shows you your network configuration

#dhclient – releases and renews your IP address (if set up for DHCP)

#/etc/init.d/network restart – will restart the networking service

#vim /etc/network/interfaces – edit the file that controls the network configuration

Use the a key to go into insert mode. Then cursor over to dhcp and change that to static.

Add the following (on the next line/s):

address 10.1.10.54 – give it an IP address.

netmask 255.255.255.0 – give it a subnet mask. This is almost always a class C address

network 10.1.10.0 – give it a network address.

broadcast 10.1.10.255 – give it a broadcast address.

gateway 10.1.10.1 – give it a default gateway.

Esc to get out of insert mode.

:wq to write the changes to the interfaces file

#vim /etc/resolv.conf – put in the IP addresses for your DNS server; if it’s not already in this configuration file.

System / administration / firewall – to configure your firewall settings

system-config-firewall – get to the firewall from the command line
 
netstat -an - what ports is it listening for?

netstat -lnpt | grep 22

iptables -L - list the iptables.

ip addr show

ssh 127.0.0.1

iptables -nvL INPUT

ifconfig -a

ifconfig eth1

nmtui - initiates the RHEL network manager GUI

service network restart - restarts the network interfaces
service NetworkManager restart - redirects to the command below
systemctl restart NetworkManager.service
After making network interface changes, run the service and systemctl commands above to reset the network interfaces and the NetworkManager itself. If it 
fails, and it will, just type reboot at the command line (as root of course) to reset the VM. Then bring up a terminal session, then elevate your permissions to
root with su - root and use ifconfig to see if your changes took effect. 

VM # netstat -lnpt | grep 22

tcp   0  0  0.0.0.0:22  0.0.0.0:*  LISTEN  2361/sshd - this tells us that we have a service/process with PID(2361) called sshd (OpenSSH daemon) listening to port 22.

Bonus Scenario
After getting everything completely working, and then shutting down, the next day I started everything up and I couldn't use WinSCP to get to either server. Here were the two problems that remained.

First:

One of the servers just wasn't working. After some time it just started working without any changes. So, do other things and come back and try later. It just might start working.

Second:

Look at the image below.

You will notice that there are different options that you can choose for the chosen interface; for it to use. In the shown image you see ENP0s3 and eth0 and Turn Off as three different options for setting the enp0s3 interface.

The first thing that I did, in this case, as the one server that was still not working, was go to the command line and type ifconfig.

On the working server I didn't see any issues.

On the server that I couldn't WinSCP to, I noticed a static IP address (the same one) was being used for both the NAT and the Bridged adapters.

I knew that the first interface, the one configured as NAT, was supposed to be using DHCP (ie: 10.0.2.15). So I looked at the configuration file, /etc/sysconfig/network-scripts/ifcfg-eth0, and didn't see a problem there.

Next I came here and chose ENP0s3, instead of eth0, for this interface.

Then I went back and typed ifconfig again, at the command line, and all was good.

And my WinSCP started working.

So, the lesson is, if, after fixing the CONNECTION REFUSED issue is completed, and you restart the VM guests, and things stop working
1) start looking around at config files
2) use the ifconfig command to check your network interfaces.
3) look in the dropdown menu (shown below) to see if changing the configuration, that the DHCP interface uses, changes what is shown when you ifconfig at the command line.

If anything is out of order
1) check to see if all of the interfaces come up as CONNECTED.

Sometimes, when the VM is turned on, one of the network interfaces isn't connected. If it's not connected, start it, then check. If this still doesn't fix it, change the configuration that it's using with the dropdown shown below.

These issues should show up with using either:
1) ifconfig, or
2) in these drop downs next to the power button on the top right hand side of your Centos 7, RHEL 7 or Oracle Enterprise Linux 7 based VM.


Back to Mike Beaver's Hypnotherapy Home Page

If you are the original creator of material featured on this website and want it removed, please contact the webmaster.
Copyright © 1998-2006 Charles Michael Beaver.