Server Konfiguration

So, neuer Server ist bestellt und wird nun Stück für Stück von mir konfiguriert werden.

Das Ziel ist ein System mit den folgenden Komponenten:

  • Debian Stretch
  • Mailserver
    • Dovecot / Postfix (oder eventuell Mailcow)
  • Webserver
    • Nginx
    • PHP 7.2

Setup

Zuerst wird über das Control Panel ein Debian Netinstall Image eingelegt und die Installation gestartet.

  1. Deutsche Sprache und deutsches Tastaturlayout
  2. Geführte Partitionierung -> Gesamte Festplatte und Einrichten von LVM (für spätere Snapshots)
  3. Bei der Paketauswahl nur [ssh-Server] und [Systemwerkzeuge]
  4. Neustart.

SSH Verbindung vorbereiten

SSH Key auf dem Client erstellen:

ssh-keygen -t rsa -b 4096

Dann SSH Verbindung herstellen und Public Key zu den authorized Keys hinzufügen:

$ ssh username@94.16.XXX.XXX
# su
# mkdir ~/.ssh
# nano ~/.ssh/authorized_keys

Public Key einfügen (und am besten kommentieren, für welchen Rechner)

Nun die SSH konfigurieren:

# nano /etc/ssh/sshd_config
Port 222
Protocol 2
# Logging
SyslogFacility AUTH
LogLevel INFO
# Authentication:
LoginGraceTime 120m
PermitRootLogin prohibit-password
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM no

X11Forwarding yes
PrintMotd no
TCPKeepAlive yes
ClientAliveInterval 120
ClientAliveCountMax 720

AllowUsers root
AllowUsers otheruser

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
Subsystem       sftp    /usr/lib/openssh/sftp-server

Dann den SSH Service neustarten und in einem neuen Console Fenster das Login per Key testen:

ssh -p 222 -i ~/.ssh/id_rsa root@94.16.XXX.XXX

Wenn das funktioniert, können wir die Konsole ein wenig komfortabler einrichten. Dazu die ~/.bashrc und die ~/.bash_aliases

# ~/.bashrc: executed by bash(1) for non-login shells.

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# You may uncomment the following lines if you want `ls' to be colorized:
export PS1="\[3[01;32m\]\u@\h\[3[01;34m\] \w $\[3[00m\] "

export HISTFILESIZE=20000
export HISTSIZE=10000
# Combine multiline commands into one in history
shopt -s cmdhist
# Ignore duplicates, ls without options and builtin commands
HISTCONTROL=ignoreboth
export HISTIGNORE="&:ls:[bf]g:exit"

export LS_OPTIONS='--color=auto -alh'

export LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33'

export TERM=xterm-color
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad


if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

shopt -s checkwinsize

shopt -s histappend
PROMPT_COMMAND='history -a'
eval "`dircolors`"

# Functions
extract () {
	if [ -f $1 ] ; then
		case $1 in
			*.tar.bz2)	tar xjf $1		;;
			*.tar.gz)	tar xzf $1		;;
			*.bz2)		bunzip2 $1		;;
			*.rar)		rar x $1		;;
			*.gz)		gunzip $1		;;
			*.tar)		tar xf $1		;;
			*.tbz2)		tar xjf $1		;;
			*.tgz)		tar xzf $1		;;
			*.zip)		unzip $1		;;
			*.Z)		uncompress $1	;;
			*)			echo "'$1' cannot be extracted via extract()" ;;
		esac
	else
		echo "'$1' is not a valid file"
	fi
}

# we need it later..
# echo 'ROOT Login:' `date` `who` | php /root/telegram.php
# .bash_aliases

alias ls='ls $LS_OPTIONS'
# saves backups of files; needs mkdir /root/.nanobackup
alias nano='nano --backup --backupdir=/root/.nanobackup'

Nun auf dem Client den SSH Zugang konfigurieren, so dass Port und Key automatisch ausgewählt werden:

$ nano ~/.ssh/config
ServerAliveInterval 120


Host myshortname realname.example.com
    HostName realname.example.com
    IdentityFile ~/.ssh/realname_rsa # private key for realname
    User remoteusername

Host myother realname2.example.org
    HostName realname2.example.org
    IdentityFile ~/.ssh/realname2_rsa  # different private key for realname2
    User remoteusername

Server absichern

# apt update && apt upgrade
# apt install rkhunter fail2ban
# rkhunter --propupd
# rkhunter --update
# rkhunter --check

Dann /etc/fail2ban/jail.local editieren:


[DEFAULT]
# 6 Stunden
bantime  = 21600

maxretry = 4

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.

# 24 Stunden
findtime  = 86400

[sshd]
enable = true

[sshd-ddos]
enable = true

[proftpd]
enable = true

[postfix]
enable = true

[postfix-rbl]
enable = true

[dovecot]
enable = true

[recidive]
enable = true
logpath  = /var/log/fail2ban.log
banaction = %(banaction_allports)s
bantime  = 604800  ; 1 week
findtime = 86400   ; 1 day

Dann installieren wir noch Lynis, ein Tool, welches diverse Schwachstellen in der Konfiguration des Servers prüft und Tipps gibt:

# wget -O - https://packages.cisofy.com/keys/cisofy-software-public.key | apt-key add -
# apt install apt-transport-https
# echo "deb https://packages.cisofy.com/community/lynis/deb/ stable main" | tee /etc/apt/sources.list.d/cisofy-lynis.list
# apt update
# apt install lynis
# lynis audit system

Weitere Tools installieren

# apt install htop iotop iftop screen haveged

Monitoring

Wir installieren munin-node für allgemeine Systemwerte und Statistiken

# apt install munin-node

Schreibe einen Kommentar