Ubuntu 18.04 Initial Server Setup

Ubuntu 18.04 Initial Server Setup

Last updated on

Ubuntu Server 18.04 LTS (Bionic Beaver) is finally here and is being rolled out across all Cloud Hosting/VPS providers such as DigitalOcean and AWS. In this guide we will configure your new Ubuntu 18.04 server and implement some best practices. This guide is aimed for both beginners and advanced users.

Prerequisites

If you are accessing Ubuntu Server 18.04 remotely and don’t know how to access terminal via SSH, please see one of the following guides:

1. Create User with Superuser Privileges

If you already set up a superuser account during installation under your own name, you may skip this step.

The default root user is the administrative user in a Linux environment that has superuser privileges and you are discouraged from using it on a regular basis. For that reason, it is highly recommended that you set up an alternative account under your own name and assign it superuser privileges.

If you are logged in as root, you should see something like [email protected]:~$  . The $ sign here indicates an account with superuser privileges.

In this example we are going to use the adduser command to add a new user called john. The sudo command at the beginning means  “superuser do!” and tells Linux to run the ensuing command with elevated superuser privileges, otherwise you may get an access denied error.

sudo adduser john

You will be prompted to enter a new password. Generate a password and keep it safe. Note that as you enter passwords in Linux, there are no * stars or dots and it will appear that nothing is being typed.

Once you’ve entered your new password, you will also be asked to enter contact details. You don’t have to fill these in, just press ENTER for defaults.

Once the new user is created, give it superuser privileges using the usermod command. The -aG parameter means append to Group, and the name of the superuser group is sudo.

sudo usermod -aG sudo john

Once you run the command, you won’t get any feedback. Linux often works like this, if there’s no error, you can assume it worked!

We can now switch to our new account john using the su command (substitute user).

sudo su - john

Enter the password you created previously.

Password:
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

[email protected]:~$

Great, we are now logged in as john. From now on, you should only log into Ubuntu using this new user.

2. Set up SSH Key-Based Authentication (optional)

You may skip this step but it is recommend for added security.

As well as offering additional security, SSH key authentication can be more convenient than the more traditional password authentication. Please see one of the following guides:

3. Set Up Firewall

It’s always a good idea to set up a firewall to make sure only connections to certain services are allowed. The default firewall configuration tool for Ubuntu 18.04 is ufw. It provides a user friendly way to create an IPv4 or IPv6 host-based firewall.

If you are connected via SSH, allow OpenSSH as a firewall rule first so you don’t get locked out.

sudo ufw allow OpenSSH

If successful, you will see “Rules updated”.

Now enable the firewall and press y and ENTER if prompted with a warning “Command may disrupt existing ssh connections.”.

sudo ufw enable

To check the status of the firewall, run:

sudo ufw status

Here we can see the OpenSSH rule we just added.

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)

4. Check the Timezone

To check if the server time and timezone are correct for your region, run:

date

If the date, time or timezone are incorrect, please see article:

5. Update Ubuntu

Run the following string of commands to download and install the latest packages. Commands are separated with && and will run in succession. If prompted to install packages, press y and ENTER .

sudo apt update && sudo apt upgrade && sudo apt dist-upgrade

Once updated, you may need to reboot the server:

sudo reboot

For more information on how packages are updated in Ubuntu, see:

6. Create Swap Space

Swap space? Why would I need that? If your server ever runs out of physical memory during heavy load, some critical services such as MySQL can crash. It’s important to have some swap space where memory can expand to if really necessary.

What Next?

Now that you’ve completed the Ubuntu 18.04 Initial Server Setup, you may wish to set up a web server next.

You should decide whether you want to run a LAMP Stack (Linux/Apache/MySQL/PHP) or a LEMP Stack (Linux/Nginx/MySQL/PHP).

Apache is the most popular web server and has been around the longest whereas Nginx is newer but is catching up. As of 2018, Apache is used on 47% of web servers and Nginx is not far behind with 37%. By 2020 if trends continue, Nginx will be the most popular!

Personally I run all my sites on Nginx because it tends to handle higher traffic better. Bear in mind that if you go with Nginx there are quite a few differences in how Virtual Hosts are set up. Also, Nginx does not interpret .htaccess files the way Apache does. If you’re used to working with Apache-based servers, go with that. Otherwise, why not give Nginx a try and learn something new?

Apache Guides

We have one single guide for installing a LAMP stack or if you prefer you can do them separately for Apache, MySQL and PHP.

You may also want to install phpMyAdmin for administering your MySQL server.

To set up a free SSL cert for your domain:

You may want to install and configure an FTP server:

More Apache guides will be rolled out during April/May 2018.

Nginx Guides

We also have one single guide for installing a LEMP stack or separate guides for Apache, MySQL and PHP.

You may also want to install phpMyAdmin for administering your MySQL server.

To set up a free SSL cert for your domain:

You may want to install and configure an FTP server:

Let me know in the comments if this helped. Follow me @DevAnswers or read more.

1 Star2 Stars3 Stars4 Stars5 Stars 4.92 (25 votes)

Feedback

Your email address will not be published. Required fields are marked *

We use Markdown to style comments, like on Github and Reddit.
To do a line break, type two spaces after the sentence.
You can add inline code by wrapping it in backticks: `code here`

    To do an entire block of code  
    type four spaces before the line
    and it will appear in a block like this.
    <-- four empty spaces

9 replies

one of the best tutorials i found on the net for setting up an ubuntu server from scratch!

everything works exactly as descriped! with the help of your guides i set up my superuser, firewall, apache, mysql, phpadmin, sftp etc.

please keep on your good work!

can you please also make a short guide to set up a mailserver and a cloud (owncloud or anything else)?

greets from germany
^CHAMPi^

Glad they helped! 😉

I don’t think I’ll be doing a mail server or cloud guide. I mainly only do guides for myself so I can refer to them later and I haven’t needed to set up a mail server yet. I use Mailgun and Gmail to handle all my mail, I might do a guide on that soon.

is an onscreen keyboard installed by default? is it possible to avoid using it?

I’m not aware of any onscreen keyboard in Ubuntu Server 🤔

Thank You So much for these guides man, they’ve helped me out with my development environment 😀

The second step seems to be missing?
1. Create User with Superuser Privileges
3. Set up SSH Key-Based Authentication (optional)

I suppose this second step is “install OpenSSH Server” because until this is done, ufw will not have a profile for OpenSSH.

That's fixed now, thanks. There was no step missing there, it was just numbered incorrectly.

On Ubuntu Server, OpenSSH Server is installed and enabled by default and a ufw profile is already present. This is not the case for Ubuntu Desktop.