SSH into CentOS 7 guest from host machine using VirtualBox
I have mostly used Ubuntu and Debian-based distributions till now which means the other side of the Linux world, where the RHEL based distributions reside is still a grey area to me. Recently I needed to install CentOS server in VirtualBox to test something out. But soon I found that it's a little cumbersome to work in the VirtualBox vm window because I can't copy/paste texts normally to/from the VirtualBox as I can do in Terminal and I can't cycle through the open windows in my guest machine using Alt+Tab when I am using the vm. So I decided to ssh to the CentOS guest from the host machine and work from the host Terminal instead.
After I did a little searching on the Internet, it turned out to be rather easier than I thought. I had the CentOS 7 minimal image installed and running in VirtualBox already. Here's what I did to get the ssh working:
- Added a VirtualBox Host-only network. The option can be found at VirtualBox -> File -> Preferences -> Network tab -> Host-only network tab. After that, I restarted the vm.
- Since it was a minimal install of CentOS, I had to install net-tools package to use the ifconfig command. The below command took care of the installation.
sudo yum install net-tools
- I ran the ifconfig command and got the below output:
The network interface in the middle with name enp0s8 is the one that I set up in step 1.
- Next I created a file named "ifcfg-enp0s8" in "/etc/sysconfig/network-scripts/" location. The second part of file name after the hyphen (-) had to match with the network interface name in step 3. And then typed the below content in the file:
The "NAME" should match with the network interface name. And the "IPADDR" could be any valid IPv4 address value and it will be used to ssh to the guest vm from host machine.
- After saving the file I just had to reboot the vm. I could have only restarted the networking service though instead of rebooting. Anyway, the next step is quite simple. Ssh into the CentOS vm from the host. And it's done!