Ubuntu Server Configuration
Ubuntu Server Configuration
1. Prerequisites
1.1. Generate an SSH Key Pair
⚠️ NOTE: I choose to leverage Ed25519 keys instead of RSA. Not all hosting
providers support this, so if you’re using something other than Hetzner,
you may need to generate an RSA key pair instead.
1. For Windows:
2. For macOS:
In this guide, I’ll be using my favorite SSH Client, Termius as it offers several
advantages. It supports SSH, Mosh, and Telnet protocol, making it versatile for
different tasks. It has a user-friendly interface, making it easy even for beginners
to establish secure connections. Furthermore, it has robust features like built-in
SFTP client, SSH key agent forwarding, and the ability to remember hosts and
servers, making repeated connections quicker and more efficient. Its multi-
platform support allows you to use it on different operating systems, ensuring
1. Log in to Hetzner Cloud and create a New Project called Personal Website
Toggle Screenshot
3. Paste your SSH Public Key and set it as the default key.
4. On the left-hand menu, click on Firewalls and then create a new one. Leave the
default inbound rules but give your firewall a sensical name before saving it. I
called mine web-standard
b. For the Image select Ubuntu 22.04 under the OS Images section.
c. Select the x86 (Intel/AMD) option under the Shared vCPU Type
d. Choose a size based on your needs. It’s best to start with the
lowest/cheapest option since you can easily scale up the server later if
needed, so in this case CPX11 should work just fine.
e. Ensure you have both Public IPv4 and Public IPv6 checked for Networking.
f. For the SSH Keys portion, ensure you select the one you added to the
project.
h. It’s best to select the Backups option. This will eventually save you.
j. Click on the Create & Buy now button and you’re all done.
a. In this guide, I choose to leverage an Ed25519 key pair, but not all
hosting providers allow it.
If you’re following along with the tech stack I’ve chosen, then your connection
steps are:
4. Install Updates
sudo apt update && sudo apt -y full-upgrade
Inside this file we’ll add the following contents then save & exit:
granted.
is the username for which the sudo privileges are being
The first occurrence of ALL signifies that this rule applies to all hosts.
If you had a network of computers using the same sudoers file, you
could restrict certain commands to specific hosts.
“newusername” is allowed to act as. The first ALL here means that
"newusername" can act as any user. The second ALL means that
"newusername" can act as any group. So, together, this means that
“newusername” can act as any user and any group.
The last ALL means that the "newusername" can run all commands.
Each line in this file corresponds to one public key and should be
formatted as <key-type> <base64-encoded-key> .
The permissions for the .ssh directory should be 700 (drwx------) , which
means that only the owner of the directory can read, write, and
execute files in that directory.
⚠️ If you’re using a different SSH Client than Termius, then try to ssh into
your server using the newusername along with the SSH Key you created
earlier.
If you’re following along with my preferred tech stack, then do the following:
1. Edit the identity you created previously in Termius and update the username.
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
ChallengeResponseAuthentication yes
Allow OpenSSH
Enable UFW
Install Fail2Ban
Configure a Jail
Use nano to create /etc/fail2ban/jail.local and paste the following in it:
💡 Since you modified a configuration file, you’ll need to start the service
associated with it in order for changes to take effect.
NOTE: in this case, we are also enabling the service. In Linux, this means
that the service will start automatically after a reboot.
Install ClamAV
sudo apt -y install clamav clamav-daemon && sudo systemctl enable clamav-fres
The first asterisk represents the minute value (0-59), the second
represents the hour value (0-23), the third represents the day of the
month value (1-31), the fourth represents the month value (1-12), and
the fifth represents the day of the week value (0-7 where both 0 and
7 represent Sunday).
To edit the crontab for the current user, you can use the command
crontab -e . To view the current user's crontab, you can use crontab -l .
#!/bin/bash
/usr/bin/clamscan -r / --exclude-dir="^/sys" --exclude-dir="^/proc" --exclud
File Modification: Sysmon monitors and logs changes to file creation time.
This can be a key indicator of malicious activity, as some malware will attempt
to manipulate the timestamps of files.
By installing and properly configuring Sysmon on our server, we can gain a much
more detailed view of server activity, which can be invaluable in diagnosing
problems or identifying security threats.
💡 While the command technically “installs” the Sysmon binary, the process
& service haven’t been executed yet, so Sysmon is not yet running on
our system.
sysmon -accepteula -i
nano /opt/sysmon/sysmon-config.xml
<Sysmon schemaversion="4.81">
<EventFiltering>
<!-- Capture all process creation events -->
<ProcessCreate onmatch="include">
<Rule name="ProcessCreation">
<Image condition="contains">/bin/</Image>
<Image condition="contains">/usr/bin/</Image>
<Image condition="contains">/sbin/</Image>
<Image condition="contains">/usr/sbin/</Image>
</Rule>
</ProcessCreate>
<!-- Capture raw read access events (indicative of low-level system acces
<RawAccessRead onmatch="include">
<Rule name="RawAccessRead">
<Image condition="contains">/bin/</Image>
<Image condition="contains">/usr/bin/</Image>
<Image condition="contains">/sbin/</Image>
<Image condition="contains">/usr/sbin/</Image>
</Rule>
</RawAccessRead>
It operates with its own user and group ( syslog ) to manage log
files securely and consistently.
2. File Ownership:
it.
It ensures that only the syslog service, which is trusted and runs
with appropriate privileges, can write to these logs.
1. Separation of Concerns:
3. System Compatibility:
2. Change Ownership:
Change the ownership of the log file to syslog:
3. Set Permissions:
Set appropriate permissions to allow syslog to write to the log file:
4. Configure syslog:
Create a new syslog configuration file to direct Sysmon logs to the correct file:
5. Restart syslog:
Restart the syslog service to apply the changes:
Summary
By giving syslog ownership of the sysmon.log directory and file, we ensure that the
syslog service can properly manage and write to the log files. Configuring syslog