Step by Step Guide To Linux Security For Beginner
Step by Step Guide To Linux Security For Beginner
for beginners
Clément Levallois
2017-04-03
Table of Contents
Ordering the server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
ip tables 6
better: uncomplicated firewall 7
Use anti-intrusion defenses and audit systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Psad 7
fail2ban 8
Lynis 8
the end. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
last modified: 2018-10-01
• Remember to install the Linux version not from the rescue system in the console but from
https://robot.your-server.de/server/index in the "Linux" tab.
(installing from the rescue system provided with the bare server causes a ssh key mess)
Because:
apt-get update
→ refreshes the repositories and fetches information about packages that are available online.
apt-get upgrade
→ downloads and installs updates for all installed packages - as long as it doesn’t bother
dependencies (install new packages, remove old ones or crosses a repo source (switch a package
from one repo to another)).
(source)
Then define your time zone (the one where your server is located):
dpkg-reconfigure tzdata
1
This step helps when your server needs to be synchronized with other servers.
The kernel is the software at the closest of the machine: it is provided by the Linux distribution you
use.
A configuration file offers parameters which tune the kernel to make things harder for an intruder.
Here I rely exactly on the tutorial by Pontikis:
vi /etc/sysctl.d/local.conf
vi /etc/aliases
root:[email protected]
newaliases
vi /etc/ssh/sshd_config
2
Change:
port SSH 22
with a new port (let’s say 1234), write the new port down somewhere
With the root user disabled at the SSH login step, the attackers must guess both the username and
its password to access the connection, and that’s much harder.
Of course, an attacker who aims at you or your server specifically (a "targeted" attack) would expect
a series of usernames (in my case "seinecle", the name I use on all social media), so don’t use it
either.
So the logic is the following: we will create a user with much less priviledges than the root user.
Only this weak user will have the right to connect to the server.
The user will be "enough" for regular tasks on the server. If we need the admin rights of root to
install stuff or else, we will temporarily switch from this weak user to root to execute what we
need, but then revert back to this weak user.
This way, we limit greatly the exposure of root privileges to the outside.
The steps:
1. making sure we have installed the "sudo" command that will allow us to switch from a weak
user to root.
3. giving rights to this user to establish a connection to the server (not just act on it once logged)
3
2. Adding a new user (let’s call it "myUser")
AllowUsers myUser
vi /etc/ssh/sshd_config
PermitRootLogin no
From there on, you cannot login to the server from root, only from myUser!
Let’s try it. Create a new SSH session with myUser. Then:
su -
A SSH private key is not transmitted on the wire. So, it can’t be hacked this way.
4
A detailed explanation is available here.
Logging through SSH rather than passwords can be hair rising because there are so many tiny
details that can go wrong. There is a good chance that if you do it for the first time you will lock
yourself outside the server.
So, do this when you can still erase the server, of if you are confortable waiting that your provider
will unlock it for you.
Steps:
ChallengeResponseAuthentication no
X11Forwarding no
UsePAM no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
LogLevel DEBUG3 (this should be added, the parameter is not listed by default)
5
mkdir ~/.ssh
chmod 700 ~/.ssh
cd ~/.ssh
vi authorized_keys
• make sure you have put the public key in /home/myUser/.ssd/authorized_keys (not just in the
root user folder)
• make sure your key starts with "the "ssh-rsa" (with a space after it, check the first "s" might be
missing …)
• do service sshd restart after each modif to load your new ssh key
Your private key will probably not be recognized the first time because of some problems above not
completely fixed.
Keep trying to log with your SSH key. To find the cause of your issues, inspect the log for auth
operations:
tail -f /var/log/auth.log
Some useful answers to questions from developers lost in making SSH keys works:
4. Finally, when the login via SSH keys work, only then can you disable login via passwords:
PasswordAuthentication no
Setting up a firewall
A firewall gives you control on what can enter and leave your server.
ip tables
The rules for setting up ip tables are logical but quite complicated. Using an ip tables generator
could help.
6
But there is an even easier alternative.
Following @mgilbir's advice, I’ll use ufw: a linux package for "uncomplicated firewall". To install it:
We add a rule to block all incoming traffic, except for SSH connections through the port we defined:
ufw default deny incoming ufw allow 1234/tcp
ufw enable
INFO
this part builds on: http://www.pontikis.net/blog/psad-install-config-debian-wheezy
Psad is an app which bans users which scan ports. Before installing it, we need to make sure the
firewall logs traffic:
vi /etc/psad/psad.conf
Possible values for some interesting parameters (and the source for this section), are here:
http://www.pontikis.net/blog/psad-install-config-debian-wheezy
7
Then we must edit this file to add the address of the server to the whitelist:
vi /etc/psad/auto_dl
127.0.0.1 0; # localhost
xx.xx.xxx.xxx 0; # Server IP (replace xx.xx.xxx.xxx by your actual server IP)
fail2ban
This is an app which bans users which fail to login after a number of times - typically bots trying to
break in.
fail2ban can actually scan logs from a list of apps you decide (MongoDB, Apache server, GlassFish,
etc.) and ban ips mentioned in logs showing a failed access. You need to setup a regex rule specific
for each log format, though.
I’ll cover it later, when I’ll have MongoDB and GlassFish installed.
Lynis
This is an application running on your machine, generating security audits and making suggestions.
Install it:
The report will appear on screen (hit Enter to move on), and in this file:
/var/log/lynis-report.dat
8
the end
Author of this tutorial: Clement Levallois