Table of Content

install ubuntu server 17.10

download iso

from link

create new vm

  • memory 1024MB
  • disk 50GB dynamic
  • Audio, USB disable
  • first NIC is NAT, 10.0.2.0/24 for Internet access, add port forward in virtualbox setting: 127.0.0.1:2220 to guest 22
  • user vagrant/vagrant

NAT setup

NAT NIC enp0s3 is up,

2: enp0s3:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:ec:18:3d brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3

Internet access is working,

vagrant@ubuntu:/etc/network$ ping -c 2 google.ca
PING google.ca (135.0.199.248) 56(84) bytes of data.
64 bytes from 135.0.199.248 (135.0.199.248): icmp_seq=1 ttl=49 time=23.5 ms
64 bytes from 135.0.199.248 (135.0.199.248): icmp_seq=2 ttl=49 time=49.7 ms

ssh user password

sudo su -
passwd // change root password to vagrant
passwd vagrant // change vagrant password to vagrant

passwordless sudo

add vagrant to sudo group,

usermod -a -G sudo vagrant
sudo visudo

add below line by the end, to make sudo user vagrant without password,

vagrant ALL=(ALL) NOPASSWD: ALL

ssh service

vagrant@ubuntu$ systemctl -l --type service --all|grep ssh
  ssh.service                                loaded    active   running OpenBSD Secure Shell server 

vagrant@ubuntu$ systemctl status ssh.service                                   
● ssh.service - OpenBSD Secure Shell server                                                  
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)         
   Active: active (running) since Fri 2017-12-29 10:36:23 EST; 1h 44min ago                  
 Main PID: 1471 (sshd)                                                                       
   CGroup: /system.slice/ssh.service                                                         
           └─1471 /usr/sbin/sshd -D                                                          

ssh insecure keypair

create public key using above keypair

mkdir -p /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh
curl -Lo /home/vagrant/.ssh/authorized_keys \
https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh

update sshd_config for authorized_keys,

cat /etc/ssh/sshd_config|grep authorized_keys
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2

change to,

AuthorizedKeysFile .ssh/authorized_keys

restart ssh service

systemctl restart ssh.service

local ssh to vm with key

from local shell, it is msys for my case,

$ cd ~/.ssh
$ curl -Lo vagrant https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant
$ ssh -i .ssh/vagrant vagrant@10.110.0.15
or
$ ssh -i .ssh/vagrant vagrant@127.0.0.1 -p 2220

update system

$ sudo apt-get update
$ sudo apt-get upgrade -y
$ sudo apt-get install aptitude
$ sudo apt-get install python -y
$ sudo apt-get install git -y
$ sudo apt-get install dos2unix
$ sudo apt-get install -y ifupdown
$ sudo apt-get install python-setuptools
$ sudo easy_install --upgrade pip
$ pip --version

guest tool

sudo apt-get install -y gcc build-essential

add Guest Tool CD from virtualbox GUI, then mount and install Guest Tool,

vagrant@ubuntu:~/.ssh$ sudo mount /dev/cdrom /mnt
mount: /mnt: WARNING: device write-protected, mounted read-only.
vagrant@ubuntu:~/.ssh$ cd /mnt
vagrant@ubuntu:/mnt$ sudo ./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.30 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Starting the VirtualBox Guest Additions.

Could not find the X.Org or XFree86 Window System, skipping.

manually add shared folder L:\oldhorse to oldhorse from virtualbox setting, reboot vm, will see the mounted share folder,

oldhorse 1.9T 1.3T 564G 70% /media/sf_oldhorse

This is just for testing, by default vagrant will mount current folder as shared folder to vm /vagrant.

defragmentation the vm

sudo dd if=/dev/zero of=/EMPTY bs=1M
sudo rm -f /EMPTY
sudo apt-get autoremove
sudo apt-get clean

and clean history,

vagrant@ubuntu:$ cat /dev/null > ~/.bash_history && history -c

package vagrant box

from local shell, when give vm name ubuntu17.10 we just created, vagrant will compress it as box format,

$ mkdir ~/vagrant/ubuntu17.10
$ cd ~/vagrant/ubuntu17.10
$ vagrant package --output dreamcloud-ubuntu17.10-vagrant.box --base ubuntu17.10
==> ubuntu17.10: Attempting graceful shutdown of VM...
==> ubuntu17.10: Clearing any previously set forwarded ports...
==> ubuntu17.10: Exporting VM...
==> ubuntu17.10: Compressing package to: C:/oldhorse/portableapps/msys64/home/oldhorse/vagrant/ubuntu17.10/dreamcloud-ubuntu17.10-vagrant.box

package.box 731MB, not bad.

test vagrant box

$ cd ~/vagrant/ubuntu17.10
$ vagrant box add dreamcloud/ubuntu17.10 dreamcloud-ubuntu17.10-vagrant.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'dreamcloud/ubuntu17.10' (v0) for provider:
    box: Unpacking necessary files from: file://C:/oldhorse/portableapps/msys64/home/oldhorse/vagrant/ubuntu17.10/dreamcloud-ubuntu17.10-vagrant.box
    box: Progress: 100% (Rate: 12.6M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'dreamcloud/ubuntu17.10' (v0) for 'virtualbox'!

$ vagrant box list
dreamcloud/ubuntu17.10 (virtualbox, 0)

we init box,

$ vagrant init dreamcloud/ubuntu17.10
A Vagrantfile has been placed in this directory. You are now
ready to vagrant up your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
vagrantup.com for more information on using Vagrant.

boot it up,

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'dreamcloud/ubuntu17.10'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntu1710_default_1514581728097_42
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Remote connection disconnect. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/oldhorse/portableapps/msys64/home/oldhorse/vagrant/ubuntu17.10

$ vagrant ssh

NAT Internet access looks good,

vagrant@ubuntu:~$ ping -c 2 google.ca
PING google.ca (135.0.199.248) 56(84) bytes of data.
64 bytes from 135.0.199.248 (135.0.199.248): icmp_seq=1 ttl=49 time=25.2 ms
64 bytes from 135.0.199.248 (135.0.199.248): icmp_seq=2 ttl=49 time=58.0 ms

share folder mounted properly,

vagrant@ubuntu:~$ df -h
vagrant 440G 253G 187G 58% /vagrant