Setup OpenSSH server in VirtualBox guest

I’m working on a few projects at the office that require me to edit some Perl scripts.
The problem is, I don’t know Perl. I took a C++ class in college, so I understand a little about programming, and I know enough to be dangerous, but I’m not fluent in Perl. I figured, “How hard could it be to learn Perl?”.

I didn’t want to run/edit Perl scripts on my work machine, so I setup a CentOS VM on VirtualBox. I chose CentOS because it is the closest thing to RedHat, without paying for RedHat. I also wanted to be able to work on the guest from my work machine through SSH, so I needed to make sure that OpenSSH was installed on my guest.

 

Guest Additions

After installing CentOS on the VM, I installed Guest Additions to make my life easier. Make sure your system is up-to-date with:

sudo yum upgrade

 

Reboot, then make sure you have gcc and kernel-devel installed.

sudo yum install gcc kernel-devel

 

Reboot again, then insert the Guest Additions ISO, navigate to the ISO, and run:

sudo ./VBoxLinuxAdditions.run

 

Check the output, and after a reboot, you should be good to go.

[root@localhost VBOXADDITIONS_4.3.10_93012]# sudo ./VBoxLinuxAdditions.run 
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.10 Guest Additions for Linux............
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [  OK  ]
Building the shared folder support module                  [  OK  ]
Building the OpenGL support module                         [  OK  ]
Doing non-kernel setup of the Guest Additions              [  OK  ]
Starting the VirtualBox Guest Additions                    [  OK  ]
Installing the Window System drivers
Installing X.Org Server 1.13 modules                       [  OK  ]
Setting up the Window System to use the Guest Additions    [  OK  ]
You may need to restart the hal service and the Window System (or just restart
the guest system) to enable the Guest Additions.

Installing graphics libraries and desktop services componen[  OK  ]
[root@localhost VBOXADDITIONS_4.3.10_93012]#

 

Enable SSH access

There’s two different ways to do this, both are explained below.

Port Forwarding

First, shutdown your guest. Then, go to Settings–>Network–>Adapter 1.
20140508_002
If you’re using NAT, you can setup port forwarding to forward SSH from a random port (I used 3022) on your host to port 22 on your guest. If prompted by Windows Firewall, allow the exception. Startup your guest.

20140508_00420140508_003

Host-only network

Instead of setting up port forwarding, you could setup a second network adapter on your VM that only connects your host and guest. First, shutdown your guest. Then, go to File–>Preferences–>Network–>Host-only Networks and edit the Virtualbox Host-only Ethernet Adapter to ensure the DHCP server is enabled.

20140508_007 20140508_008
Then, go to Settings–>Network–>Adapter 2, and enable the Host-only Adapter on the VM. Startup your guest.
20140508_001

 

Install OpenSSH on your guest

FIrst, install OpenSSH on your guest.

sudo yum install openssh-server openssh-clients

 

Start the SSH daemon. Technically, this is all you need to do.

sudo chkconfig sshd on
sudo service sshd start

 

If you want, you can edit the SSH config file to deny root SSH logins. I would backup the config file first.

sudo cp -p /etc/ssh/sshd_config  /etc/ssh/sshd_config.original
sudo vi /etc/ssh/sshd_config

 

Uncomment PermitRootLogin and change yes to no.

Before…

#PermitRootLogin yes

After…

PermitRootLogin no

 

Restart the SSH service.

sudo service sshd restart

 

Login

On your host (mine is a Windows machine), use a SSH client (I like PuTTY) to connect.

If you setup port forwarding, connect like this.
20140508_005

 

If you’re using a host-only adapter, find the IP of your host-only adapter on the guest by using ifconfig, and connect like this.
20140508_006

-Logan

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.