Tim
Tim

Reputation: 1471

virtual box + vagrant centos8 box (headless) to access from internet

I successfully setup vagrant-virtualbox with centos8. yum pakage manger, openssl-server, openssl-client all updated.

The sshd service is running.

The box starts up without any issue, i am able to connect from my host machine using ssh vagrant@ip-address prompts for password and able to login.

From gitlag-ci.yml file in deploy stage i am trying to access the centos8 box using ssh username@ipaddress/hostname, but it times out on 22 port.

I tired ssh vagrant@ip-address-of-guest and ssh -p 2222 username@ip-address-of-host nothing works.

But i am able to connect using my command windows 10 prompt and also with putty from host machine at 22 port without any issue.

In virtual box, Settings -> Network -> Port Forwarding (removed the host ip address and left it blank)

config

config.vm.network "forwarded_port", guest: 443, host: 8085
config.vm.network "public_network"

Upvotes: 1

Views: 1131

Answers (1)

evalufran
evalufran

Reputation: 189

When you perform manually your first installation you have to:

groupadd vagrant
useradd vagrant -g vagrant -G wheel
echo "vagrant" | passwd --stdin vagrant

# Install vagrant keys (vagrant will change this key at first boot)
mkdir -p /home/vagrant/.ssh

cat <<EOM >/home/vagrant/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8Y\
Vr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdO\
KLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7Pt\
ixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmC\
P3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW\
yLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key
EOM

chown -R vagrant:vagrant /home/vagrant/.ssh
chmod -R u=rwX,go= /home/vagrant/.ssh

you also have to install virtualbox guest additions, to allow vagrant create shared folders

yum install wget kernel-headers kernel-devel perl gcc bzip2 dmks make -y
wget http://download.virtualbox.org/virtualbox/6.1.4/VBoxGuestAdditions_6.1.4.iso
mkdir /media/VBoxGuestAdditions
mount -o loop VBoxGuestAdditions_6.1.4.iso /media/VBoxGuestAdditions
sh /media/VBoxGuestAdditions/VBoxLinuxAdditions.run
reboot

Then you can package your installation in order to make a box:

mkdir boxes
vagrant package --base NAME_OF_YOUR_VM_ON_VIRTUALBOX --output ./boxes/CentOS.box

To make network and ssh work on VirtualBox adapters have to be in this order:

1) Nat
2) Host Only

3) Bridged

So, you have to set the first to nat (if you are using vagrant it will do this automatically) and then

config.vm.network :private_network, ip: "192.168.xx.xxx", netmask: "255.255.255.0", :mac =>"08002782xxxx", name:"vboxnet1", :adapter => 2
config.vm.network :public_network, bridge: "wlp59s0", ip: "192.168.xx.xxx", :mac => "08002726xxxx", :adapter => 3

and then you have to ensure your network interfaces are up, so just try ping google.

If a network interface is not up, and ping does not work, set those parameters in /etc/sysconfig/network-scripts/ifcfg-:

IPV6INIT="yes"
DHCP_HOSTNAME="hostname-here"
IPV6_AUTOCONF="yes"
BOOTPROTO="dhcp"
DEVICE="<device_name_here>"
ONBOOT="yes"
UUID="" #can be omitted

and then reboot

Remember if you have more then one VM they must have different mac address to work properly

I also share my vagrantfile configuration

Vagrant.configure("2") do |config|

  config.vm.define "nodo1" do |nodo1|
    nodo1.vm.box = "./boxes/CentOS.box"
    nodo1.vm.boot_timeout= 3000000
    nodo1.vm.hostname="nodo1server"
    nodo1.vm.network :private_network, ip: "192.168.xx.xxx", netmask: "255.255.255.0", :mac =>"08002782xxxx", name:"vboxnet1", :adapter => 2
    nodo1.vm.network :public_network, bridge: "wlp59s0", ip: "192.168.xx.xxx", :mac => "08002726xxxx", :adapter => 3

    nodo1.vm.provider "virtualbox" do |vb|

      vb.gui = false
      vb.memory = "2048"
      vb.name ="nodo1"

      vb.customize ["modifyvm", :id, "--ostype", "RedHat_64"]
      vb.customize ["modifyvm", :id, "--boot1", "dvd"]
      vb.customize ["modifyvm", :id, "--boot2", "disk"]
      vb.customize ["modifyvm", :id, "--boot3", "none"]
      vb.customize ["modifyvm", :id, "--boot4", "none"]
      vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
      vb.customize ["modifyvm", :id, "--nic1", "nat"]
      vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
      vb.customize ["modifyvm", :id, "--nictype3", "virtio"]
      vb.customize ["modifyvm", :id, "--nictype4", "virtio"]
      vb.customize ["modifyvm", :id, "--acpi", "on"]
      vb.customize ["modifyvm", :id, "--ioapic", "off"]
      vb.customize ["modifyvm", :id, "--chipset", "piix3"]
      vb.customize ["modifyvm", :id, "--vram", 256]
      vb.customize ["modifyvm", :id, "--rtcuseutc","on"]
      vb.customize ["modifyvm", :id, "--hpet","on"]
      vb.customize ["modifyvm", :id, "--bioslogofadein","off"]
      vb.customize ["modifyvm", :id, "--bioslogofadeout","off"]
      vb.customize ["modifyvm", :id, "--bioslogodisplaytime", 0]
      vb.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
      vb.customize ["modifyvm", :id, "--draganddrop", "bidirectional"]
      vb.customize ["modifyvm", :id, "--vrde","on"]
      vb.customize ["modifyvm", :id, "--vrdemulticon", "on"]
      vb.customize ["modifyvm", :id, "--vrdeport", 3390]

      #vb.customize [
      #  "storageattach", :id,
      #  "--storagectl", "IDE",
      #  "--device", 0,
      #  "--port", 0,
      #  "--type", "dvddrive",
      #  "--medium", "${HOME}iso/CentOS-7-ks.iso"
      #]
#
      #vb.customize [
      #  "storageattach", :id,
      #  "--storagectl", "IDE",
      #  "--device", 0,
      #  "--port", 1,
      ##  "--type", "dvddrive",
      #  "--medium", "${HOME}/iso/VBoxGuestAdditions_6.1.4.iso"
      #]
    end

  end
end

Hope it can help!

Upvotes: 1

Related Questions