LAMP 2018Linux

Install Linux – Building a LAMP Server

This is the third article in a series on building the ultimate LAMP Server. This covers how to install Linux, focusing on a variety of different distributions. Since the purpose of this machine will be to act as a server, we will focus on setting up a headless system.

Estimated time (this post only): 40 minutes
* varies by distro

Why a Headless Installation?

A web server does not need a desktop and installing one only consumes resources and possibly introduces stability issues. Also, by using SSH to remotely manage the server, you can simply copy/paste commands from this guide, simplifying the entire process.

Which Distro Do I Want?

Most web servers on the Internet run CentOS or Ubuntu. CentOS has ruled this market for years, but Ubuntu is quickly gaining popularity. So if you are developing for shared hosts, you will most likely want to focus on these two distros. Fedora is a an alternative to CentOS which may contain features not yet included in CentOS. Debian is to Ubuntu as Fedora is to CentOS. OpenSUSE only has a small share of the web server market, but is also increasing in popularity. I only recommend Arch Linux for advanced users. Of the top one million web servers, less than 2% run Windows, so we won’t even bother with looking at a WAMP stack (the Windows variant of a LAMP stack).

Installing Operating System

Important: these instructions assume you are installing on a system without UEFI support (such as a VirtualBox or Hyper-V Generation 1 VM). If installing on a physical machine with UEFI enabled or a virtual machine with UEFI support, you will need to create an additional EFI System Partition at least 100 MiB in size (some guides suggest as much as 500+ MiB, so you may need to allocate more). You can easily do so via the installer for all distributions listed here (with the exception of Arch Linux). See this article if you want more information.

CentOSFedoraUbuntuDebianopenSUSEArch LinuxArch Linux (UEFI)

Download your desired ISO (DVD image). Get the minimal CentOS ISO from their website or a Torrent (recommended). If installing to a physical machine, burn the ISO to DVD. If installing to a virtual machine, mount the ISO:

Mount CentOS ISO 1 Mount CentOS ISO 2

Boot to the DVD or start the VM to begin installation. First, configure the network settings. The following screenshots will work for a VirtualBox installation configured as in the previous post. CentOS does not enable network adapters by default, so be sure to enable them. The second adapter in the screenshots is the host-only adapter mentioned in the last post. Setting this to a static IP is highly recommended. Your network settings may differ.

CentOS Network Setup 1 CentOS Network Setup 2 CentOS Network Setup 3 CentOS Network Setup 4 CentOS Network Setup 5

Setup your timezone. You should not enable the NTP client (Network Time) if running as a virtual machine (let VirtualBox set the VM clock):

CentOS Timezone 1 CentOS Timezone 2

Change the automatic partitioning! CentOS allocates most of the drive to a partition for the /home directory. This is inefficient for a server, since very little should be kept there. It is recommended that you allocate more space for directories reserved for software installations. You could just allocate everything to the root directory, but if the file system became corrupt on root, you would lose everything. This is how I set up my 100 GB drive.

CentOS Partitioning 1 CentOS Partitioning 2 CentOS Partitioning 3 CentOS Partitioning 4
CentOS Partitioning 5 CentOS Partitioning 6 CentOS Partitioning 7 CentOS Partitioning 8

Unless you need to change them, ignore the other options on this page and just begin the installation. During installation you should set a root password. A separate user is also recommended unless you plan to setup Active Directory or LDAP support later (not covered in this guide series). When creating this user be sure to check the box "Make this user administrator" to automatically set them up as a sudo user (having the ability to run administrator commands via sudo).

After installation you may need to eject the DVD or unmount the ISO to boot to the hard drive.

Download your desired ISO (DVD image). Get the Fedora Server ISO from their website or via a Torrent. If installing to a physical machine, burn the ISO to DVD. If installing to a virtual machine, mount the ISO:

Fedora Mount ISO 1 Fedora Mount ISO 2

Boot to the DVD or start the VM to begin installation. First, configure the network settings. The following screenshots will work for a VirtualBox installation configured as in the previous post. The second adapter in the screenshots is the host-only adapter mentioned in the last post. Setting this to a static IP is highly recommended. Your network settings may differ.

Fedora Network Configuration 1 Fedora Network Configuration 2 Fedora Network Configuration 3 Fedora Network Configuration 4

Setup your timezone. You should not enable the NTP client (Network Time) if running as a virtual machine (let VirtualBox set the VM clock):

Fedora Timezone 1 Fedora Timezone 2

Change the automatic partitioning! You could just allocate everything to the root directory, but if the file system became corrupt on root, you would lose everything. This is how I set up my 100 GB drive for this guide.

Fedora Partitioning 1 Fedora Partitioning 2 Fedora Partitioning 3 Fedora Partitioning 4 Fedora Partitioning 5 Fedora Partitioning 6 Fedora Partitioning 7 Fedora Partitioning 8

Unless you need to change them, ignore the other options on this page and just begin the installation. During installation you should set a root password. A separate user is also recommended unless you plan to setup Active Directory or LDAP support later (not covered in this guide series). When creating this user be sure to check the box "Make this user administrator" to automatically set them up as a sudo user (having the ability to run administrator commands via sudo).

After installation you may need to eject the DVD or unmount the ISO to boot to the hard drive.

Download the latest Ubuntu Server LTS image from their website or via BitTorrent (recommended). If installing to a physical machine, burn the ISO to DVD. If installing to a virtual machine, mount the ISO:

Boot to the DVD or start the VM to begin installation. The install process is very simple. Basically just go with defaults until you reach the section on partitioning. As noted on their download page, the default installer no longer supports LVM, so you will need to download the alternate installer if you want to create LVM partitions during the initial installation (apparently the new installer will still install to existing LVM partitions if created via another method). The following screenshots assume you are using the new (18.04) installer, but check out the instructions for Debian if using the traditional installer. Here is how I configured a 200 GiB drive for Ubuntu Server 18.04:

Since this will be a headless install, all you need is the smaller CD image from their website or via BitTorrent. Most users will want the amd64 version. Burn the ISO to CD if you need a physical disc or just mount in your virtual machine. Although you could download the full set of DVD images, I recommend only downloading the CD image or the first DVD image. The reason for this is the package manager will look for the discs every time you install a new package rather than looking online first if it knows you have the full DVD set, which can get annoying because you will be constantly swapping discs/images.

Boot to the disc or start the VM to begin installation. Choose the graphical installation option to make things a little easier. The install process is very simple. Just do what it says until you reach the section on partitioning. Following is how I assigned partitions so that my compiled software was in separate partitions.

Debian Partitioning 1 Debian Partitioning 2 Debian Partitioning 3 Debian Partitioning 4 Debian Partitioning 5 Debian Partitioning 6 Debian Partitioning 7 Debian Partitioning 8 Debian Partitioning 9 Debian Partitioning 10 Debian Partitioning 11 Debian Partitioning 12 Debian Partitioning 13 Debian Partitioning 14 Debian Partitioning 15 Debian Partitioning 16 Debian Partitioning 17 Debian Partitioning 18 Debian Partitioning 19 Debian Partitioning 20 Debian Partitioning 21 Debian Partitioning 22 Debian Partitioning 23 Debian Partitioning 24 Debian Partitioning 25 Debian Partitioning 26 Debian Partitioning 27 Debian Partitioning 28 Debian Partitioning 29 Debian Partitioning 30 Debian Partitioning 31 Debian Partitioning 32 Debian Partitioning 33 Debian Partitioning 34 Debian Partitioning 35 Debian Partitioning 36

If installing from the CD image rather than the DVD image, be sure to select the option for enabling a network mirror for downloading software packages! After the initial installation, you will be presented with a screen to install additional packages, such as a desktop. Since we want a headless installation, but we do want SSH, change the options as in the following screenshot (your choices may vary, but this is all you need for now):

Debian Software Selection

Download the openSUSE Leap DVD image from their website directly or via BitTorrent. I recommend not using Tumbleweed for servers since it is a rolling release. If installing to a physical machine, burn the ISO to DVD. If installing to a virtual machine, mount the ISO.

Mount openSUSE ISO 1 Mount openSUSE ISO 2

Boot to the DVD or start the VM to begin installation. When you get to the step about partitioning, change the defaults to allocate partitions for where we will be installing our compiled software. The following is how I setup my 100 GiB drive in a virtual machine.

openSUSE Partitioning 1 openSUSE Partitioning 2 openSUSE Partitioning 3 openSUSE Partitioning 4 openSUSE Partitioning 5 openSUSE Partitioning 6 openSUSE Partitioning 7 openSUSE Partitioning 8 openSUSE Partitioning 9 openSUSE Partitioning 10 openSUSE Partitioning 11 openSUSE Partitioning 12 openSUSE Partitioning 13 openSUSE Partitioning 14 openSUSE Partitioning 15 openSUSE Partitioning 16 openSUSE Partitioning 17 openSUSE Partitioning 18 openSUSE Partitioning 19 openSUSE Partitioning 20 openSUSE Partitioning 21 openSUSE Partitioning 22 openSUSE Partitioning 23 openSUSE Partitioning 24 openSUSE Partitioning 25 openSUSE Partitioning 26 openSUSE Partitioning 27 openSUSE Partitioning 28 openSUSE Partitioning 29 openSUSE Partitioning 30 openSUSE Partitioning 31 openSUSE Partitioning 32 openSUSE Partitioning 33 openSUSE Partitioning 34

On the "Clock and Time Zone" page, if this is a virtual machine, you may want to click the "Other Settings" button and disable the NTP client (enabled by default).

openSUSE NTP

On the "Desktop Selection" page, choose the option "Server (Text Mode)."

openSUSE User Interface Options

Next you should configure a new user. I recommend not using the same password for the root user or logging in automatically.

openSUSE User Creation 1 openSUSE User Creation 2

On the final page, you may want to enable the firewall and SSH:

openSUSE Firewall and SSH

Unlike the other distros I cover, Arch Linux does not include an installer. So this is not for the faint of heart. I will simplify the process as much as possible. For more information, you can check out the installation guide on the Arch Linux wiki. For the most part, I will be following the instructions in this article with a few modifications.

Download the Arch Linux ISO. I recommend using the BitTorrent download method. If installing to a physical machine, burn the ISO to DVD. If installing to a virtual machine, mount the ISO.

Boot to the DVD or start the VM. In my case, I was able to skip setting up the network interface since Arch Linux detected the proper default route automatically.

You will need to partition the hard drive from the command line before you do anything else. I had a 100 GiB drive and wanted to use LVM, 20 GiB for the root partition, 500 MiB for the boot partition, 35 GiB for the /usr/local partition, and 4 GiB for the swap partition. The rest I wanted to go to a partition mounted to /opt. The following commands allowed me to do this:

fdisk /dev/sda
o
n
p
1
<Enter>
+500M
n
p
2
<Enter>
<Enter>
t
2
8e
w
pvcreate /dev/sda2
vgcreate vg1 /dev/sda2
lvcreate -L 20G -n root vg1
lvcreate -L 4G -n swap vg1
lvcreate -L 35G -n usr-local vg1
lvcreate -l 100%FREE -n opt vg1
mkfs.ext2 /dev/sda1
mkfs.xfs /dev/vg1/root
mkfs.xfs /dev/vg1/usr-local
mkfs.xfs /dev/vg1/opt
mkswap /dev/vg1/swap
swapon /dev/vg1/swap
mount /dev/vg1/root /mnt
mkdir /mnt/boot
mount /dev/sda1 /mnt/boot
mkdir -p /mnt/usr/local
mount /dev/vg1/usr-local /mnt/usr/local
mkdir /mnt/opt
mount /dev/vg1/opt /mnt/opt

You can now finally start installing Arch Linux to the hard drive. Note that you may need to change things like the locale and/or time zone:

pacstrap /mnt base base-devel
genfstab -U /mnt >> /mnt/etc/fstab
arch-chroot /mnt /bin/bash
sed -i 's/#en_US.UTF-8/en_US.UTF-8/g' /etc/locale.gen
locale-gen
echo 'LANG=en_US.UTF-8' >> /etc/locale.conf
ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime
echo 'arch-lamp' | tee /etc/hostname
echo '127.0.1.1       arch-lamp.localdomain   arch-lamp' | tee -a /etc/hosts
sed -i 's/^HOOKS=(.*/HOOKS=(base udev autodetect modconf keyboard fsck block lvm2 filesystems)/g' /etc/mkinitcpio.conf

Note: for that last line, if you are installing an older version of Arch Linux, you may need to change the parentheses to quotes. This is correct as of version 2017.11.01 (the version that literally came out today). Now continue…

mkinitcpio -p linux
passwd
[Enter new root password]
pacman -S grub
grub-install --target=i386-pc /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg
exit
umount -R /mnt
reboot

Don’t forget to remove the install disc or unmount the ISO to ensure you boot to the new installation.

This section is only applicable if you are installing on a physical or virtual machine with UEFI support. While doing so for other distros is as simple as adding an EFI System Partition, the more complex nature of installing Arch Linux deserves its own section in this case.

Download the Arch Linux ISO. I recommend using the BitTorrent download method. If installing to a physical machine, burn the ISO to DVD. If installing to a virtual machine, mount the ISO.

Boot to the DVD or start the VM. In my case, I was able to skip setting up the network interface since Arch Linux detected the proper default route automatically.

You will need to partition the hard drive from the command line before you do anything else. I had a 100 GiB drive and wanted to use LVM, 20 GiB for the root partition, 500 MiB for the boot partition, 550 MiB for the EFI System Partition (as recommended on the Arch Linux website; though I think you could get away with far less), 35 GiB for the /usr/local partition, and 4 GiB for the swap partition. The rest I wanted to go to a partition mounted to /opt. The following commands allowed me to do this:

gdisk /dev/sda
o
n
1
<Enter>
+550M
ef00
n
2
<Enter>
+500M
<Enter>
n
3
<Enter>
<Enter>
8e00
w
mkfs.fat -F32 /dev/sda1
mkfs.ext2 /dev/sda2
pvcreate /dev/sda3
vgcreate vg1 /dev/sda3
lvcreate -L 20G -n root vg1
lvcreate -L 4G -n swap vg1
lvcreate -L 35G -n usr-local vg1
lvcreate -l 100%FREE -n opt vg1
mkfs.xfs /dev/vg1/root
mkfs.xfs /dev/vg1/usr-local
mkfs.xfs /dev/vg1/opt
mkswap /dev/vg1/swap
swapon /dev/vg1/swap
mount /dev/vg1/root /mnt
mkdir /mnt/boot
mount /dev/sda2 /mnt/boot
mkdir /mnt/boot/efi
mount /dev/sda1 /mnt/boot/efi
mkdir -p /mnt/usr/local
mount /dev/vg1/usr-local /mnt/usr/local
mkdir /mnt/opt
mount /dev/vg1/opt /mnt/opt

You can now finally start installing Arch Linux to the hard drive. Note that you may need to change things like the locale and/or time zone:

pacstrap /mnt base base-devel
genfstab -U /mnt >> /mnt/etc/fstab
arch-chroot /mnt /bin/bash
sed -i 's/#en_US.UTF-8/en_US.UTF-8/g' /etc/locale.gen
locale-gen
echo 'LANG=en_US.UTF-8' >> /etc/locale.conf
ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime
echo 'arch-lamp' | tee /etc/hostname
echo '127.0.1.1       arch-lamp.localdomain   arch-lamp' | tee -a /etc/hosts
sed -i 's/^HOOKS=(.*/HOOKS=(base udev autodetect modconf keyboard fsck block lvm2 filesystems)/g' /etc/mkinitcpio.conf

Note: for that last line, if you are installing an older version of Arch Linux, you may need to change the parentheses to quotes. This is correct as of version 2017.11.01 (the version that literally came out today). Now continue…

mkinitcpio -p linux
passwd
[Enter new root password]
pacman -S grub efibootmgr
grub-install --target=x86_64-efi /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg
exit
umount -R /mnt
reboot

Don’t forget to remove the install disc or unmount the ISO to ensure you boot to the new installation.

Sudo

Sudo allows a standard user to run commands as root. It is highly recommended that you use this rather than logging in directly as root.

CentOS/Fedora/UbuntuDebianopenSUSEArch Linux

Sudo is installed and configured by default, so we can skip that step. But you may want to test it after logging in with:

sudo ls /

Debian (as of this writing) does not install sudo by default, so this is the first thing we need to address. So instead of logging in as the normal user, log in as root using the password you configured during install.

First, you must install sudo:

apt-get install sudo

You may be prompted to re-insert the installation disc. Do so as needed. The default Debian installation of sudo allows sudo access to all users of the "sudo" group (not "wheel" like other distros use). So, to make your normal username capable of running commands via sudo, do (replace "username" with the name of the user you setup):

adduser username sudo
logout

Now you can log back in as your normal user and test sudo with:

sudo ls /

Log in as the user created during installation. OpenSUSE installs sudo but doesn’t configure it the same way as other distros, so for now whenever you type "sudo" you may be prompted to enter the root password rather than your user password. Let’s fix that first…

You may prefer to use nano over vi to make text editing simpler, so install it and then run visudo with nano:

sudo su
zypper install nano
export EDITOR=nano
visudo

Scroll down to find the following lines:

Defaults targetpw   # ask for the password of the target user i.e. root
ALL     ALL=(ALL) ALL   # WARNING! Only use this together with 'Defaults targetpw'!

Comment (add a pound sign and space) to both lines so it reads:

# Defaults targetpw   # ask for the password of the target user i.e. root
# ALL     ALL=(ALL) ALL   # WARNING! Only use this together with 'Defaults targetpw'!

Scroll down to find the following line and un-comment it (remove the pound sign and first space):

# %wheel ALL=(ALL) ALL

Now use CTRL+X to save the file and exit. Then do the following (replace "username" with your normal username):

usermod -G wheel username
exit
logout

Now log back in as your normal user and make sure sudo is setup properly by doing the following (it should ask for your password and not root’s):

sudo ls /

After installation, log in as the root user with the password you created earlier. Now to configure sudo:

export EDITOR=nano
visudo

Scroll down to find the following line and un-comment it (remove the pound sign and first space):

# %wheel ALL=(ALL) ALL

Now create a new username for yourself as a member of that group (replace "username" with the name of your new user):

useradd -m username
passwd username
usermod -a -G wheel username
logout

Now you can log in with this new username. Whenever you need to run administrator commands as that user, simply preface them with "sudo". As a simple test to make sure this is working, run the following to show the files in the root directory:

sudo ls /

Configure Network Adapters

CentOSFedoraUbuntuDebianopenSUSEArch Linux

I’ve noticed several times that even when you enable all network adapters during installation, CentOS doesn’t always actually enable them. You can use nmtui to enable any disabled adapters. Just run the following command and use the arrow keys and Enter to enable any disabled adapters (those without asterisks beside them):

sudo nmtui
CentOS NMTUI 1 CentOS NMTUI 2 CentOS NMTUI 3 CentOS NMTUI 4 CentOS NMTUI 5 CentOS NMTUI 6

Before doing anything else, I recommend installing a simple text editor. By default, CentOS uses vi, which is very powerful but possibly intimidating to new users. So do:

sudo yum install nano

CentOS does not include the host name you chose during installation in the hosts file. Although this shouldn’t be needed, it can cause issues with some software (like Apache). So do:

sudo nano /etc/hosts

Edit the file to include the local host name as well as the host+domain. Then use CTRL+X to exit nano and save the file. Following is an example of what the file contents may look like:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.1.1   lamp lamp.localdomain
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 lamp lamp.localdomain

You could probably skip adding the second loopback address (127.0.1.1), but this has been the recommended method for years due to a bug with IPv4 in Linux.

If you need to change network settings you will either need to learn to use nmcli or install nmtui with:

sudo dnf install NetworkManager-tui

Fedora does not include the host name you chose during installation in the hosts file. Although this shouldn’t be needed, it can cause issues with some software (like Apache). So do:

sudo nano /etc/hosts

Edit the file to include the local host name as well as the host+domain. Then use CTRL+X to exit nano and save the file. Following is an example of what the file contents may look like:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.1.1   lamp lamp.localdomain
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 lamp lamp.localdomain

You could probably skip adding the second loopback address (127.0.1.1), but this has been the recommended method for years due to a bug with IPv4 in Linux.

The latest version of Ubuntu no longer adds the host name to the hosts file (previous versions did), so you may want to do so for Apache to function properly:

sudo nano /etc/hosts

Edit the file to include the local host name as well as the host+domain. Then use CTRL+X to exit nano and save the file. Following is an example of what the file contents may look like:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.1.1   lamp lamp.localdomain
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 lamp lamp.localdomain

Ubuntu now uses NetPlan to manage network interfaces. You may have noticed during the installation process that you had few (if any) options for configuring your interface(s), so you will probably need to make some changes now, especially if using a static IP address (recommended). For headless installations, this means using the networkd renderer. Here’s what I did to set a static IP address for the host-only adapter on my VirtualBox test machine…

First, I obtained a list of the interfaces with:

networkctl status

This showed me an interface named enp0s3 and another named enp0s8. The latter was the one I needed to assign a static IP to, so I created a new configuration file with a high priority specifically for this interface by doing:

sudo nano /etc/netplan/99-netcfg.yaml

In this new file I typed the following to force this interface to always use the static IP address I desired:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s8:
      dhcp4: no
      addresses: [192.168.56.103/24]

I didn’t need DNS or a default gateway, so this was enough for my setup (see the above link if you need to configure more). To apply the new settings, I simply did:

sudo netplan --debug apply

You shouldn’t need to alter the hosts file if you set everything correctly during installation since Debian automatically adds an entry for the host name you set. Let’s see what adapters are enabled and their current IP addresses (note if you’re using an older version of Debian before they updated systemd, the interface names will be "ethx" where x is a number; this may sound simpler, but there are good reasons for this change):

ip addr

Debian IP Addr

As you can see, in my case only the primary interface was enabled by default. So I did the following:

sudo nano /etc/network/interfaces

I then added the following to that file (change to match your configuration):

auto enp0s8
iface enp0s8 inet static
  address 192.168.56.104
  netmask 255.255.255.0
  network 192.168.56.0
  broadcast 192.168.56.255

Restart the network service:

sudo /etc/init.d/networking restart

For some reason, unlike Ubuntu, Debian does not re-enable interfaces setup for DHCP when restarting the networking service, so do:

sudo ip link set dev enp0s3 up
sudo dhclient -r
sudo dhclient

One of the nice things about openSUSE is the handy YaST tool. Many users may not be aware that there is a terminal version of this tool for headless installations. To launch it:

sudo yast

Just use your tab and enter keys (or the Alt shortcuts designated by the highlighted characters). In my case, the second network adapter was not configured and I wanted to give it a static IP accessible via the VirtualBox host-only network:

openSUSE Network Setup 1 openSUSE Network Setup 2 openSUSE Network Setup 3 openSUSE Network Setup 4

You should also use this tool to edit the host name and add it to the loopback hosts entries as follows. Note that I did not check the "Assign Hostname to Loopback IP" option in "Network Settings" but added it separately (this is safer, especially if using IPv6):

openSUSE Network Setup 5 openSUSE Network Setup 6 openSUSE Network Setup 7 openSUSE Network Setup 8

Now exit YaST (usually with F9).

First, get the name(s) of your network interface(s):

ip link

Arch Linux IP Link

I wanted to set the first interface with a dynamic IP address and the second with a static one. First, create netctl profiles for the interfaces (note that I didn’t need a gateway or DNS for the secondary interface, but you could add them if need be):

printf "Interface=enp0s3\nConnection=ethernet\nIP=dhcp" | sudo tee /etc/netctl/enp0s3
printf "Interface=enp0s8\nConnection=ethernet\nIP=static\nAddress=('192.168.56.106/24')\nExcludeAuto=no\nPriority=2" | sudo tee /etc/netctl/enp0s8

The following may seem odd, but it seems to be the only way guaranteed to get the netctl special systemd units to start automatically and with the correct settings on some systems:

sudo netctl start enp0s3
sudo netctl start enp0s8
sudo pacman -S ifplugd
sudo netctl enable enp0s3
sudo netctl enable enp0s8
sudo systemctl start netctl-ifplugd@enp0s3.service
sudo systemctl start netctl-ifplugd@enp0s8.service
sudo systemctl enable netctl-ifplugd@enp0s3.service
sudo systemctl enable netctl-ifplugd@enp0s8.service
sudo systemctl stop netctl-ifplugd@enp0s3.service
sudo systemctl stop netctl-ifplugd@enp0s8.service
sudo systemctl disable netctl@enp0s3
sudo systemctl disable netctl@enp0s8
sudo netctl stop-all
sudo reboot

After restarting, you can check that the interfaces are both running with the correct IP addresses via:

ip addr

Updates

At this point, it is a good idea to update all packages:

CentOSFedoraUbuntuDebianopenSUSEArch Linux

It is recommended that you enable the EPEL repository for installing some packages that may not be available in the default repos and ensure it is enabled and then install a few basic packages:

sudo rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum repolist

Install all updates and reboot. Installation of deltarpm is optional but will save disk space and bandwidth:

sudo yum install deltarpm
sudo yum update
reboot

As of Fedora 26+, the EPEL repository is no longer officially supported (it conflicts with the official Fedora repo), so we won’t be adding it as we did with CentOS.

Install all updates and reboot. Installation of deltarpm is optional but will save disk space:

sudo dnf install deltarpm
sudo dnf update
reboot

Install all updates and reboot:

sudo su -c "apt-get update && apt-get upgrade && apt-get install linux-image-generic"
reboot

You may wish to remove the discs as an installation source. If you don’t, you will be swapping discs every time you install new packages. To do so run the following:

sudo sed -i 's/deb cdrom:/# deb cdrom:/g' /etc/apt/sources.list
sudo apt-get update

Install all updates and reboot. Installation of debdelta is optional but will save download time and disk space:

sudo apt-get install debdelta
sudo su -c "apt-get update && debdelta-upgrade && apt-get upgrade"
sudo reboot

It is recommended that you add the Packman repo, since the official SUSE repos don’t always contain the needed or packages (or contain older versions). I’m using a mirror in Germany, but you can check the list of mirrors and choose another if you wish. Change the SUSE version to match what you are running.

sudo zypper ar ftp://ftp.fau.de/packman/suse/openSUSE_Leap_42.3/ Packman

If the Packman mirror you add doesn’t work for some reason, do the following before trying another one:

sudo zypper rr Packman

Install all updates and reboot:

sudo zypper update
sudo reboot

Install all updates and reboot:

sudo pacman -Syu
sudo reboot

SSH

Now setup SSH securely for remote access:

CentOS/Fedora/openSUSEUbuntu/DebianArch Linux

It is optional, but recommended, that you disable the ability to log in directly as the root user now that you have another administrator account setup. You may wish to skip this if you are less concerned with security and/or are concerned that you may screw up your sudo configuration in the future:

sudo passwd -l root

As an additional security measure, you should restrict access from members of the "wheel" group to the local network. So do:

sudo nano /etc/security/access.conf

Scroll to the bottom of this file and add a line such as the following (this is in keeping with the virtual machine settings as shown previously, but change the subnet to match your network):

-:wheel:ALL EXCEPT LOCAL 192.168.56.0/24

After saving the above file, make sure SSH will comply with those restrictions by doing:

sudo nano /etc/pam.d/sshd

Insert the following line immediately after the first line "#%PAM-1.0" (this placement is important):

auth       required     pam_access.so

It is optional, but recommended, that you disable the ability to log in directly as the root user now that you have another administrator account setup. You may wish to skip this if you are less concerned with security and/or are concerned that you may screw up your sudo configuration in the future:

sudo passwd -l root

As an additional security measure, you should restrict access from members of the "sudo" group to the local network. So do:

sudo nano /etc/security/access.conf

Scroll to the bottom of this file and add a line such as the following (this is in keeping with the virtual machine settings as shown previously, but change the subnet to match your network):

-:sudo:ALL EXCEPT LOCAL 192.168.56.0/24

After saving the above file, make sure SSH will comply with those restrictions by doing:

sudo nano /etc/pam.d/sshd

Un-comment (remove the pound) the following line:

# account  required     pam_access.so

It is optional, but recommended, that you disable the ability to log in directly as the root user now that you have another administrator account setup. You may wish to skip this if you are less concerned with security and/or are concerned that you may screw up your sudo configuration in the future:

sudo passwd -l root

As an additional security measure, you should restrict access from members of the "wheel" group to the local network. So do:

sudo nano /etc/security/access.conf

Scroll to the bottom of this file and add a line such as the following (this is in keeping with the virtual machine settings as shown previously, but change the subnet to match your network):

-:wheel:ALL EXCEPT LOCAL 192.168.56.0/24

After saving the above file, make sure SSH will comply with those restrictions by doing (first install OpenSSH):

sudo pacman -S openssh
sudo systemctl start sshd
sudo systemctl enable sshd
sudo nano /etc/pam.d/sshd

Insert the following line immediately after the first line "#%PAM-1.0" (this placement is important):

auth       required     pam_access.so

You can now use any SSH client to connect to the server. I highly recommend this as opposed to typing commands directly on the server console, partly because you can copy/paste commands from this guide into the SSH client. On Windows, I like the Bitvise SSH Client (free as of this writing), particularly for its built-in SFTP transfer client. Or you could use PuTTY or another client. Just point them to the IP address of this new server and use the username/password you just configured (if using VirtualBox as in the previous post, point to the IP address of the host-only adapter).

Important: When copying/pasting commands in this guide into an SSH terminal, you may be prompted for your password for sudo. So if copying multiple lines at a time, you may want to run sudo first, which will normally prevent this from happening. Or just copy/paste one line at a time.

Firewall

CentOS/FedoraUbuntuDebianopenSUSEArch Linux

Important note on firewall: there have been ongoing bugs where conflicts between firewalld and Network Manager will cause firewall zone settings to be lost (especially with multiple adapters) on reboot. This means all adapters will be in the default zone after reboot. This was fixed in both Fedora and CentOS last year, so it is important that all packages be up to date as mentioned earlier.

If you have multiple network adapters (as I have setup in VirtualBox), you may want to place your local adapter in a separate firewall zone so you can limit which services are accessible to the Internet. First, check which adapters are in which zones:

firewall-cmd --get-active-zones

CentOS Firewall Zones

As you can see, CentOS places all adapters in the "public" zone (Fedora uses the zone "FedoraServer" as the default). Let’s move our local adapter ("enp0s8" in this example) to the "work" zone:

sudo firewall-cmd --zone=work --add-interface=enp0s8 --permanent
sudo reboot

The reason we are rebooting at this point is to ensure the change truly is permanent. This is where the bug shows up if you didn’t install updates. Check that the change really worked by once again running:

firewall-cmd --get-active-zones

Enable HTTP/HTTPS for the "work" zone (or whatever zone you assigned your adapter to) and check that the desired services are enabled:

sudo firewall-cmd --zone=work --add-service=http --add-service=https --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --zone=work --list-all

By default, the firewall is installed but not enabled. Let’s create rules to allow SSH, HTTP, and HTTPS from the local network (default rules will block all other incoming connections) and enable the firewall. Change IP range and ports as required:

sudo ufw allow proto tcp from 192.168.56.0/24 to any port 22 comment 'ssh'
sudo ufw allow proto tcp from 192.168.56.0/24 to any port 80,443,8080:8090 comment 'web app'
sudo ufw enable

By default, only a basic iptables firewall is installed. But you can install the Uncomplicated Firewall that Ubuntu uses:

sudo apt-get install ufw

Let’s create rules to allow SSH, HTTP, and HTTPS from the local network (default rules will block all other incoming connections) and enable the firewall. Change IP range and ports as required:

sudo ufw allow proto tcp from 192.168.56.0/24 to any port 22
sudo ufw allow proto tcp from 192.168.56.0/24 to any port 80,443,8080:8090
sudo ufw enable

This is another thing easiest done with YaST:

sudo yast

By default, even though you enabled the firewall during installation, no adapters are assigned to firewall zones. In my case, I wanted my first adapter assigned to the external zone and the second to the internal zone, as follows:

openSUSE Firewall 1 openSUSE Firewall 2 openSUSE Firewall 3 openSUSE Firewall 4

That was good enough for my VirtualBox installation since the internal zone is only accessible from the host and is completely unprotected while the external zone is locked down by default and I didn’t need to allow anyone on the Internet to access the VM. You can add/remove services as needed here also though.

Note that the default rule for the external zone on newer versions of openSUSE allows the SSH service. This may not be desirable if this server is not behind a firewall. But if you have a physical firewall/router and haven’t setup port forwarding for SSH, this isn’t a problem.

"The Arch Way" of keeping things as simple as possible encourages us not to use a frontend for iptables like other distros do. So let’s clear any existing iptables rules (there shouldn’t be any) and set it up from scratch. Note that I’m limiting some rules to a specific interface (for example, we don’t want to open SSH to the public interface). These steps are based on an article from the Arch Linux wiki:

sudo iptables-restore < /etc/iptables/empty.rules
sudo iptables -N TCP
sudo iptables -N UDP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A TCP -i enp0s8 -p tcp --dport 22 -j ACCEPT
sudo iptables -P INPUT DROP

sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -p 41 -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
sudo iptables -A INPUT -p icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A INPUT -p udp -m conntrack --ctstate NEW -j UDP
sudo iptables -A INPUT -p tcp --syn -m conntrack --ctstate NEW -j TCP
sudo iptables -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
sudo iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
sudo iptables -A INPUT -j REJECT --reject-with icmp-proto-unreachable

sudo iptables -A TCP -i enp0s8 -p tcp --dport 80 -j ACCEPT
sudo iptables -A TCP -i enp0s8 -p tcp --dport 443 -j ACCEPT
sudo iptables -A UDP -p udp --dport 53 -j ACCEPT

Now export the rules to a file and check them:

sudo su - -c "iptables-save > /etc/iptables/iptables.rules"
sudo nano /etc/iptables/iptables.rules

When I did this with the current version of iptables, I noticed it had automatically created a rule I hadn’t added:

-A INPUT -p ipv6 -j ACCEPT

Since these rules only affect IPv4, I assume this refers to IPv6 tunnels in IPv4, in which case this rule would allow all such incoming packets. Since I had no need for this functionality, I deleted the rule:

sudo iptables -D INPUT -p ipv6 -j ACCEPT

Once you have everything set up the way you want, save the rules again if you made any changes:

sudo su - -c "iptables-save > /etc/iptables/iptables.rules"

Now we should setup iptables for IPv6. Start by copying the IPv4 rules:

sudo cp /etc/iptables/iptables.rules /etc/iptables/ip6tables.rules

Several changes need to be made for IPv6 compatibility. In this example, the following commands should fix the rules correctly:

sudo sed -i 's/icmp-port-unreachable/icmp6-adm-prohibited/g' /etc/iptables/ip6tables.rules
sudo sed -i 's/icmp-proto-unreachable/icmp6-adm-prohibited/g' /etc/iptables/ip6tables.rules
sudo sed -i '/-p icmp/d' /etc/iptables/ip6tables.rules
sudo systemctl start ip6tables
sudo ip6tables -A INPUT -p ipv6-icmp --icmpv6-type 128 -m conntrack --ctstate NEW -j ACCEPT
sudo ip6tables -t raw -A PREROUTING -m rpfilter -j ACCEPT
sudo ip6tables -t raw -A PREROUTING -j DROP
sudo su - -c "ip6tables-save > /etc/iptables/ip6tables.rules"

Now restart both services and set them to start after reboot:

sudo systemctl restart iptables
sudo systemctl restart ip6tables
sudo systemctl enable iptables
sudo systemctl enable ip6tables

VirtualBox Guest Additions

If this is a virtual machine installed under VirtualBox, it is recommended that you install the VirtualBox Guest Additions. If running under another hypervisor like Hyper-V, chances are your distribution already contains the guest services installed automatically (though I still find it helpful on some distributions to install the Linux Integration Services, especially for dynamic memory allocation). There are a few basics we need to ensure are installed for the VirtualBox Guest Additions:

CentOSFedoraUbuntu/DebianopenSUSEArch Linux
sudo yum install make gcc wget dkms bzip2 tar kernel-devel-$(uname -r)
sudo dnf install make gcc wget dkms bzip2 tar kernel-devel-$(uname -r)
sudo apt-get install make gcc wget dkms bzip2 tar linux-headers-$(uname -r)
sudo zypper install make gcc wget dkms bzip2 tar kernel-default-devel=$(uname -r | sed 's/-default.*//g')
sudo pacman -S make gcc wget dkms bzip2 tar linux-headers=$(uname -r | sed 's/-ARCH.*//g')

Note that the 5.2.0 release of VirtualBox had some bugs with certain Linux guests, so you will want to download and mount the updated version from their download page.

VirtualBox Guest Additions

Mount it and run the installer:

sudo mkdir -p /media/cdrom
sudo mount /dev/cdrom /media/cdrom
sudo sh /media/cdrom/VBoxLinuxAdditions.run
sudo reboot

You may receive an error about the missing X.Org (desktop). This is expected since this is a headless install. Just ignore it. If you already have a version of the Guest Additions installed, the installer will warn you. In this case, you should remove the existing version using your package manager before doing the above installation. Here are some common examples (the Guest Additions don’t exist in the official CentOS/Fedora repos):

Ubuntu/DebianopenSUSEArch Linux
sudo apt-get remove --purge virtualbox-guest-dkms
sudo apt-get autoremove
sudo reboot
sudo zypper remove virtualbox-guest-kmp-default virtualbox-guest-tools
sudo reboot
sudo pacman -R virtualbox-guest-dkms
sudo pacman -R $(pacman -Qdtq)
sudo reboot

Test that it is installed and running with:

lsmod | grep vboxguest

You now have everything you need for a basic Linux server.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.