Setup A Server With Ubuntu (18.04, 20.04, 22.04)

This guide will give you a good head start with the basics of setting up your server. What it can and could do, is all up to you. Whether you prefer a network/media server, a fully operational web server or a combination of both, it is all possible with Ubuntu Server. You will find that getting every detail just right is about trial and error.

If you are new to Ubuntu Server, don't worry. You will find a wealth of information throughout the community that can help you with any issue.

Tip: Be sure to document every step you do. This will help in troubleshooting down the line.

Ubuntu 22.04 included (See end of sections to adapt for 22.04)

Prerequisites

When Ubuntu has been installed, come back here and follow these steps.

When editing files, we will be using vim. More specifically tiny vim which is actually called vi. You can use any other text editor but for simplicity vi will be used here. If not installed type:

$ sudo apt install vim

To open a file for editing where the file name is filename.txt:

vi filename.txt

How to use:

Keys Usage
i Start editing(Insert mode)
Esc Exit insert mode(Back to command mode)
←↑↓→ Navigation(Keyboard arrow keys)
: + w Save
: + q Close editor when no changes made
: + wq Save and close editor
: + q! Close editor without saving

Setup Root Password, Timezone and Update the System

Set root password

Note: This step is not necessary if Proxmox LXC container.

We are going to setup the root user's password. By default the root user is disabled and logging in with this account is impossible. Now this is sometimes not recommended for security reasons and at the time of installing Ubuntu server, the user you have created has already been granted superpowers (administrator privileges). You can skip this step and continue without any issues, but for practicality I will include the process as in some few occasions I preferred having access to the root account.

Tip: Using sudo before your first command after a user session has begun gives the user administrator privileges if he/she is part of the sudo group. By default, the user that was setup during installation is already part of the sudo group. The password will be required the first time and NOT for up to 5 minutes of inactivity. To add a user to this group type sudo usermod -aG sudo username where username is the name of the user.

  • To check whether or not you are part of the sudo group, type:

    $ sudo -l

    If we see the following at the bottom of output, then we are part of sudo group:

    user may run the following commands on ubuntu:
    (ALL : ALL) ALL
  • Assuming you have configured a user during the installation process, we now can set the root password. Type:

    $ sudo passwd root
  • Now type your user's password.

  • Next type new UNIX password(this will be the root user password). Make sure it is different then user password you created when you installed Ubuntu. Press Enter when done.

  • Retype the password and press Enter.

  • Now that you have a root password, let's try it by typing:

    $ su 
  • Type root user password and press Enter.

  • You should now be in root and the line should look like this:

    root@ubuntu:/home/user#

    Notice the # which means you are logged as using the root account. A $ is when logged as user account.

Add User or Change Username

Add User

  • While in root or user with sudo privileges type adduser followed by the username (Change username with name of user):
    adduser username
  • Give the new user a password and re-type.
  • Optionally, answer questions. Press enter to leave blank.
  • Add sudo privileges for the user if needed:
    usermod -aG sudo username

Change Username

  • If you are unsatisfied with the username chosen at the beginning of your setup here's how you can change it and your home folder to their new names while you are still in root user:
    usermod -l newuser -d /home/newuser -m olduser
  • We must now change the users' group name:
    groupmod -n newuser olduser
  • Now change permissions to the home folder:
    chown newuser:newuser /home/newuser

Timezone

Setting the proper timezone will help troubleshooting log files, keeping good records of system and user files identifying their created, modified and accessed times. This also holds true for backup and restore files as well.

  • Let's make sure your Timezone is properly set. To check, type:

    $ timedatectl

    The output should look like this:

                        Local time: Mon 2019-10-14 16:08:00 EDT
                    Universal time: Mon 2019-10-14 20:08:00 UTC
                          RTC time: Mon 2019-10-14 20:08:00
                         Time zone: America/Montreal (EDT, -0400)
         System clock synchronized: yes
    systemd-timesyncd.service active: yes
                   RTC in local TZ: no

    As you can see by the output my Timezone is America/Montreal, which is true for myself. Time and date are also confirmed as correct. If your Local time and Time zone lines are confirmed correct, proceed to Update and Upgrade . If not continue.

  • List item To check which time zones are available:

    $ timedatectl list-timezones
  • Scroll down until you find the timezone closest to you, take note of it and press the Q key to exit output.

  • Type the following command to set your timezone where Your_continent/Your_city is relevant to yours:

    $ sudo timedatectl set-timezone Your_continent/Your_city
  • Now reboot the system:

    $ sudo reboot

    Tip: To power-off type sudo poweroff or graceful sudo shutdowm -h now. To restart gracefully type sudo shutdown -r now.

  • When your server has rebooted, login and confirm timezone:

    $ timedatectl

Now your timezone should be set.

Update and Upgrade

  • Let's update the system and get the latest packages. Type:

    $ sudo apt update
  • Once completed, type:

    $ sudo apt upgrade

    If using Ubuntu 22.04 see bottom of section now.

  • When prompted, answer Y and press Enter.

In Ubuntu, security updates and bug fixes are automatically setup with the unattended-upgrades tool.

  • To see if unattended-upgrades is running on your system, type:

    $ sudo systemctl status unattended-upgrades.service


    Output should be showing active (running) as below:

    ● unattended-upgrades.service - Unattended Upgrades Shutdown
       Loaded: loaded (/lib/systemd/system/unattended-upgrades.service; enabled;>
       Active: active (running) since Sun 2023-01-08 09:35:30 EST; 1h 2min ago
         Docs: man:unattended-upgrade(8)
     Main PID: 658 (unattended-upgr)
        Tasks: 2 (limit: 4575)
       Memory: 11.9M
          CPU: 67ms
       CGroup: /system.slice/unattended-upgrades.service
               ─658 /usr/bin/python3 /usr/share/unattended-upgrades/unattended->
    
    Jan 08 09:35:30 test systemd[1]: Started Unattended         Upgrades Shutdown.
  • If not, you can install it with this command:

    $ sudo apt install unattended-upgrades


    • * Ubuntu 22.04
  • In Ubuntu 22.04 once the upgrade process has completed you will see the following screen asking you "Which services should be restarted?" press the "Tab" key and "Ok" to accept: after-upgrade.png

    This screen appears because the needrestart command is set to interactive mode in Ubuntu 22.04. This can be useful if you prefer to update your system manually and see which daemons, if not restarted, are outdated. But if you setup the system to update automatically then this can be a problem especially if the system is used in a production environment.

  • Let's configure the needrestart command so that it is set to automatic mode by editing it's configuration file:

    $ sudo vi /etc/needrestart/needrestart.conf
  • Find the line #$nrconf{restart} = 'i';, un-comment it(remove the # symbol) and change the i for an a like below:

    $nrconf{restart} = 'a';
  • Save and close the file.


  • Reboot the system:

    $ sudo shutdown -r now

Our system now has the latest updates.

Next Post