It's almost impossible to learn ubuntu on a real web server that is running your live application (you shouldn't even toy with anything running a live application), which is why it's a good idea to utilize virtualization that is indistinguishable from a real server, to put it in another perspective, it's recommended to learn/run an experiment on a local machine.
In this guide, we would virtualize Ubuntu server on Windows 7, 8, and 10, with the help of Virtualbox, this way we would be able to run Ubuntu OS on our local machine, this should work for Mac users too as Virtualbox supports Mac.
Before anything, Let's download VirtualBox and Ubuntu Server 18.04:
Download VirtualBox & Ubuntu Server, You can also use the below image as an aid:
Since we would be virtualizing Ubuntu in VirtualBox, we don't have to do anything with the Ubuntu file.
For now, just note down the location you downloaded the Ubuntu Server file.
Let's install VirtualBox:
Double-click on the downloaded VirtualBox file to start the installation process:
Click on the subsequent next to finish the installation, wait, untick this boxes if you don't want VirtualBox to start whenever you restart your system or you don't want the icon hanging in the quick launch bar:
Having done that, you should receive a warning stating the Oracle VirtualBox Network features will reset your network connection temporarily:
Please proceed and Install.
One last thing!
You would be prompted to install the VirtualBox driver:
Install and Finish VirtualBox Installation:
Good! We have VirtualBox installed, it is time to create our new Virtual Machine.
If you are used to creating servers on DigitalOcean, think of Creating a Virtual Machine as your droplets, and if you aren't used to DigitalOcean, think of creating a virtual Ubuntu operating system.
Creating A New Virtual Machine
- Open VirtualBox, Goto Machine and Click Add:
- The next thing is to choose a descriptive name, your destination folder for your new machine, and the other two would automatically update themselves base on your machine name, for example, if I have the word ubuntu in the field name, it automatically knows I want Ubuntu installed:
- Use the slider to choose your memory size, 2GB or 4GB is better if your system can handle it, I'll be going with 2GB!
- The next step is to add a virtual add disk. Since this is a new machine, check "Create a Virtual Hard-disk Now" and click on Next.
- Select Virtual Disk Image as your hard disk type
- Choose dynamically allocated, so it can use space on your hard disk as it fills up.
- Select the size of the virtual hard disk, it's 10GB by default, so depending on what you plan working on, it might be different, I'll go with 20GB for safe measure
- Final Result:
- Bonus: When working with intensive applications, it is recommended you increase the number of cores, this way your machine can perform multiple processes simultaneously, to do this, click on the Setting at the Upper section, Navigate to System at the left, click on the Processing Tab and change the number of cores to two:
Now, let's boot up our machine and Install Ubuntu Server:
Installing Ubuntu Server In VirtualBox
Before start our server, there are a few things we need to know:
Normal Start: The Normal Start would open your Virtual Machine and displays a virtual monitor.
When you close the VM machine window, it would give you three options:
Do you want to Save the Machine State (this way, you can go back to the task you are doing), or you want to Send the Shutdown Signal (This way, your system is shut down correctly, an example is when you properly shutdown your Windows 10 system), and the Last option is Power off the Machine (This is like unplugging the power cable from a physical computer).
Headless Start: This option would open your Virtual Machine with no Virtual monitor, so instead of starting the Virtual Machine from the VirtualBox GUI, it would be started from the command line, this is useful if you are planning to connect through a remote desktop or SSH connection.
Detachable Start: This is the combination of the normal and headless start, so, when you close the VirtualBox Virtual Machine window, one more option is added, which is "Continue running in the background", this is useful if you want to close the VirtualBox VM window and allow the server to run in the background.
That said, I would be going with Detachable because sometimes I'll want to allow the server to continue running in the background.
- Click on the Start Icon arrow pointing down and select Detachable:
- The next thing is to select the location you stored your Ubuntu server.
Note: You might see “FATAL: No bootable medium found! System halted.” This took me longer than I thought to fix, this is what I did, Close the Virtual Machine by Clicking the X icon, then Goto Setting:
Navigate to Storage, Click on Empty and Tick the Live CD/DVD and Click Ok, Start from Detachable and Reset the Machine
Reset the Machine to Start the installation, doing the above would avoid trouble with the installation:
- Now, let install Ubuntu:
- The first option would be to select your preferred language, you can use the Up and Arrow to Navigate, and Enter Key to select your Language
- In the next section Navigate with Up or down Key To Identify Keyboard, Use the Enter Key To Select. You will be asked a few questions when you are done, click Ok to confirm
- This section would ask you to confirm one interface the server can use to talk to other machines, choose the default and proceed.
- Unless you have a proxy address you use to connect to the internet, please Leave the proxy address blank and proceed
- Choose the Default Ubuntu mirror address and proceed
- Choose use an Entire Disk
- Choose the Disk to Install the Server, this would be the one we created before, mine is 20Gb, so, I'll be choosing that
- Review the details and click Done, you will be prompted about the warning of a disk, just Continue as this is a new OS
- This next stage is about the user info such as Your name, Username, Password, e.tc, fill in the details and proceed (You can use the Tab Key to Jum to the Next field)
- Install OpenSSH and proceed, this way you would be able to enable secure remote access to your server, you don't have to import SSH from Github, Select No for Now and Proceed
- The next option would show different snaps (Snaps are self-contained software packages that work across a range of GNU/GNU/Linux distributions.), select or deselect with the Space Bar, It depends on your use case, but we don't need those, for now, Select Done, and wait while it installs.
- When the installation completes, Please Reboot
- Once you've rebooted, you will be asked to remove the installation medium, many guides said Ubuntu can do that automatically, but that didn't work for me, so remove it to play safe:
- Once you are done removing, press Enter key and you would be prompted to log in with the User and Password you created the other time, log in with your details, Let's configure the network so we would be able to access external Networks
Network Configuration in VirtualBox
If you've already logged in, please use the Command
logoutand Close the machine. Once you've closed the machine, you will be prompted different option, Choose, Send the Shutdown Signal, and Click Ok
If you are familiar with Droplets, you would know whenever we create one, we are given a server IP address, this way, you would be able to reach your server remotely from anywhere in the World or perhaps use it to access a web server. The same logic applies to this, we need to give our Machine a Static IP address, doing this would give you the full experience of running an actual live server.
To start with,
- Goto Setting, and Navigate to Network
- Tick the "Enable Network Adapter" and Select Bridged Adapter from the Dropdown, this way your Virtual Machine would be exposed to the external network the host machine(Physical) connects to.
- Navigate to Adapter 2, Tick the "Enable Network Adapter and Choose "Host-only Adapter" from the dropdown, this is useful in the scenario where you would like to expose network between host machine (Physical) and your guest machine (Virtual Machine). You can even network between multiple virtual machines running on the same host with the "Host-only Adapter" you can't just access external Networks.
- Lastly, Click File -> Preference or use Ctrl + G on your keyboard, then go to Network and finally create a new one:
Congratulation, you've just configured your Network, now start your virtual machine in a detachable mode. Once everything is in place, log in and let's find our new server IP address.
But before we do, let's update and upgrade necessary packages, use the below command:
sudo apt-get update sudo apt-get upgrade
To save you a lot of headaches we would be using Putty to connect to our server, this way we would be able to copy and paste from the terminal (Don't sweat it, you can't copy from say your windows into the terminal if you rely on Virtualbox, Putty would help in this case), you can also Navigate Up and Down using Putty as that won't work if you rely on your virtual machine and lastly, you would be able to perform a real web server task remotely with Putty, let's get downloading and installing putty:
Goto PuTTy website, and download the setup:
Install PuTTy on your local computer by double-clicking the putty program file,
Upon clicking on Next in the above image, click on the subsequent Next and wait for PuTTY to complete the installation.
Once that is done, search for PuTTYgen or locate it inside this folder:
C:\Program Files\PuTTYand open puttygen.exe
Go to this folder on a 32-bit system:
C:\Program Files (x86)\PuTTY
Click Generate and move your mouse randomly over the blank area. This is called the entropy of mouse motion, bring in thousands of users, and no one would ever predict your mouse movement, this means no one can reproduce your keys. If you are curious you can read this StackExchange answer .
Your key looks like this, once it is generated:
The good thing is that you can also password protect your ssh key, by filling the Key passphrase field, this way, when someone gains access to your computer, they will need to provide a passphrase to access it and on top of that, you must provide this passphrase every time you use this key. You can skip using a password, but this is what I will always do on a real server, so I recommending adding password as another security layer
Save your Private Keys in a folder in your local computer:
We are getting there!
Copy the Public Key and follow the next step:
Leave it open, and open your Virtual Machine, use the
ifconfig command to show your IP address, this is mine:
Now, Input the IP address in Putty Hostname or IP address field, note that you can't copy from the terminal, just write it out and input it in Putty's field:
Once you are done with the above steps, you can open up putty and access your server.
Note: The first time you connect to the Droplet, PuTTY security alert would pop-up asking you to confirm that you trust the server. Please choose yes to save the server host key as this is the first time you are connecting or No to connect without saving the identity.
Log in with the VM server details, and the next step is to copy the public key we generated
Now, create a new directory called .ssh and restrict its permissions using the following command:
mkdir ~/.ssh chmod 700 ~/.ssh
mkdir - this means, make a new directory called .ssh
~/ - This means the home directory
chmod 700 - the user can read, write and execute
The next step is to copy the public key we generated from putty into file authorized_key in the .ssh directory.
Go into .ssh directory using:
create .authorized_keys & open authorized_keys file: (Note: You don't have to create a new file, using the below command would automatically do it for you)
Paste the public key you generated from putty in the file: (Make sure you copy everything from Top to down in the Public Key box below)
Use Ctrl + X to exit, then press Y to save and lastly press the enter key to confirm.
Now, restrict the authorize_keys permission with the following command:
chmod 600 authorized_keys
You might get directory not found if you copy and paste the command above, type the above command by hand instead.
Restart Putty and you should be fine.
Bonus 1: Save Your Putty Session, So You Won't Have To Type Your IP Adress Every Time You Login
It would be a pain in the ass to type the IP address in the putty field every time you plan on logging into your server, let's save our session:
- Input The IP Address in Putty IP address field
- Save your username in the auto-login username field, so, you won't have to input your username anytime you log in:
- Secure Login With SSH Key: Goto SSH -> Auth and Browse To The Location of your Private SSH Key
- Select The Private SSH Key and Open
- Lastly, Let's Save Our Session, Goto Session and Type The Name of Your New Session and Hit Save:
Whenever you plan to log in, just click on the Saved session, putty would automatically pull in your username, all that is left is just to enter the passkey of the ssh key.
It would show you something like this:
Using username "user". Authenticating with public key "rsa-key-20200114" Passphrase for key "rsa-key-20200114":
So, just enter the passkey you use to secure the ssh.
Bonus 2: Disable Password Authentication
Since we are now using ssh to log in, there is no point using the password authentication, so let's remove it to make our system more secure
Note: If you have any question about ssh, please comment using the comment box below
Login to your server and open the sshd_config
sudo nano /etc/ssh/sshd_config
You might be prompted to verify the user, enter your passkey and you should be presented with the below image:
Press Ctrl + W and paste in PasswordAuthentication to find that line.
Change it's value to no, most times, it is changed by default, but it doesn't hurt to cross-check:
Use Ctrl + X to exit, then press Y to save and lastly press the enter key to confirm.
sudo systemctl reload sshd
Bonus 3: Disable root login with SSH
The root user is the default administrative user in a GNU/GNU/Linux environment system that has the most superior privileges. The best practice is to disallow root to log in over SSH or password and create a new user that would act in place of the root (which we already have).
The main reason why you shouldn't use root is that there are lots of bots that try to take advantage of the root, they try guessing if you are using the root user and then they enter some guessed password.
Although, the root is disabled by default when we created the Ubuntu server, but it doesn't hurt to recheck, so, we won't have a problem when working on an actual real server in the future.
Open sshd_config using:
sudo nano /etc/ssh/sshd_config
Find this line:
PermitRootLoginUsing Ctrl + W, and you would notice it has the word - "prohibit_password"
You don't have to do anything as the password has been disabled by default, in case you see "Yes", change it to "No" or "prohibit-password"
Save with Ctrl + X and restart the SSH daemon with:
sudo service ssh restart
This sums up the guide on Installing Ubuntu Server 18.04 Using Virtual Box on Windows, so, whenever you want to log in, you fire up your virtual machine and you perform your various task on Putty.
I hope you enjoyed this guide, and if you have any questions, please ask. See you in the next phase!