====== Installation et configuration d'un serveur Debian ====== ===== Préparation du disque d'installation ===== Télécharger l'image du disque d'installation à [[https://www.debian.org/distrib/|cette adresse]]. J'ai choisi l'image d'installation de taille réduite (netinst) qui __nécessite une connexion à Internet__. Créer une clé USB d'installation avec [[https://unetbootin.github.io/|UNetbootin]]. Dans sa documentation, Debian préconise Win32DiskImager et décourage l'usage d'UNetbootin, pouvant engendrer des problèmes. Dans mon cas, l'installation était cependant corrompue avec Win32DiskImager mais pas avec UNetbootin. ===== Installation du système ===== Booter sur la clé USB au démarrage (F12 dans le cas présent). Suivre les instructions données dans [[https://www.howtoforge.com/tutorial/debian-minimal-server/|ce tutoriel]]. Personnalisations : * Ne pas forcer l'installation UEFI. * Schéma de partitionnement du disque : ''separate /home, /var, and /tmp partitions'' et garder les valeurs proposées. ===== Installer sudo ===== apt install sudo usermod -aG sudo toto Il est ensuite nécessaire de se reconnecter pour que le changement soit pris en compte et que toto ait accès à la commande sudo. ===== Configurer une IP statique ===== Éditer le fichier /etc/network/interfaces en assignant une IP statique à l'interface réseau souhaitée : sudo nano /etc/network/interfaces # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eno1 iface eno1 inet static address 192.168.1.10 netmask 255.255.255.0 gateway 192.168.1.1 Redémarrer le service réseau : sudo service networking restart ou /etc/init.d/networking restart Si l'interface n'est pas définie en ''auto'', alors il sera nécessaire de la remonter manuellement avec la commande ''ifup'' ou en redémarrant la machine. ==== Sources ==== * https://www.howtoforge.com/debian-static-ip-address * https://linuxconfig.org/how-to-setup-a-static-ip-address-on-debian-linux * https://wiki.debian.org/fr/NetworkConfiguration ===== Configurer SSH ===== Éditer le fichier ''/etc/ssh/sshd_config'' : sudo nano /etc/ssh/sshd_config Interdire la connexion de l'utilisateur ''root'' et n'autoriser que l'utilisateur ''toto'' à se connecter : PermitRootLogin no AllowUsers toto Redémarrer le service SSH : sudo service ssh restart Il peut être utile de consulter les logs de temps en temps (en particulier les accès ayant échoué) : sudo cat /var/log/auth.log | grep Invalid ==== Sources ==== * https://debian-facile.org/doc:reseau:ssh:serveur * https://guide.ubuntu-fr.org/server/openssh-server.html * https://help.ubuntu.com/community/SSH/OpenSSH/Configuring ===== Configurer le pare-feu ===== Afficher les règles de filtrage en IPv4 : sudo iptables -L Garder les règles après un reboot : sudo apt-get install iptables-persistent Répondre oui pour la sauvegarde des règles actuelles. Il y a à présent deux fichiers de configuration à éditer pour configurer le pare-feu : * ''/etc/iptables/rules.v4'' pour IPv4 * ''/etc/iptables/rules.v6'' pour IPv6 On édite la configuration pour IPv4 : sudo nano /etc/iptables/rules.v4 Contenu par défaut : # Generated by iptables-save v1.6.0 on Sun May 5 09:35:53 2019 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] COMMIT # Completed on Sun May 5 09:35:53 2019 Tout est autorisé par défaut. J'ai suivi les indications données dans le [[https://www.geek17.com/fr/content/debian-9-stretch-securiser-votre-serveur-avec-le-firewall-iptables-32|tuto]], donc : * Le trafic transfert (FORWARD) est bloqué * Le trafic sortant est non filtré * Le trafic entrant est bloqué, sauf ce qui nous intéresse # Generated by iptables-save v1.6.0 on Sun May 5 09:35:53 2019 *filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] # Allow internal traffic on the loopback device -A INPUT -i lo -j ACCEPT # Continue connections that are already established or related to an established connection -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT # Drop non-conforming packets, such as malformed headers, etc. -A INPUT -m conntrack --ctstate INVALID -j DROP # Accept SSH -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT # Chain for preventing ping flooding - up to 6 pings per second from a single # source, again with log limiting. Also prevents us from ICMP REPLY flooding # some victim when replying to ICMP ECHO from a spoofed source. -N ICMPFLOOD -A ICMPFLOOD -m recent --name ICMP --set --rsource -A ICMPFLOOD -m recent --name ICMP --update --seconds 1 --hitcount 6 --rsource --rttl -m limit --limit 1/sec --limit-burst 1 -j LOG --log-prefix "iptables[ICMP-flood]: " -A ICMPFLOOD -m recent --name ICMP --update --seconds 1 --hitcount 6 --rsource --rttl -j DROP -A ICMPFLOOD -j ACCEPT # Permit useful IMCP packet types. # Note: RFC 792 states that all hosts MUST respond to ICMP ECHO requests. # Blocking these can make diagnosing of even simple faults much more tricky. # Real security lies in locking down and hardening all services, not by hiding. -A INPUT -p icmp --icmp-type 0 -m conntrack --ctstate NEW -j ACCEPT -A INPUT -p icmp --icmp-type 3 -m conntrack --ctstate NEW -j ACCEPT -A INPUT -p icmp --icmp-type 8 -m conntrack --ctstate NEW -j ICMPFLOOD -A INPUT -p icmp --icmp-type 11 -m conntrack --ctstate NEW -j ACCEPT # Drop all incoming malformed NULL packets -A INPUT -p tcp --tcp-flags ALL NONE -j DROP # Drop syn-flood attack packets -A INPUT -p tcp ! --syn -m conntrack --ctstate NEW -j DROP # Drop incoming malformed XMAS packets -A INPUT -p tcp --tcp-flags ALL ALL -j DROP COMMIT # Completed on Sun May 5 09:35:53 2019 Redémarrer le service pour recharger et appliquer les règles : sudo service netfilter-persistent restart ==== Sources ==== * https://www.geek17.com/fr/content/debian-9-stretch-securiser-votre-serveur-avec-le-firewall-iptables-32 * https://debian-facile.org/doc:reseau:iptables-pare-feu-pour-un-client * https://debian-facile.org/doc:reseau:iptables-pare-feu-pour-une-passerelle * https://wiki.debian.org/DebianFirewall * https://wiki.debian.org/iptables * https://wiki.debian-fr.xyz/Netfilter/Iptables_introduction * https://doc.ubuntu-fr.org/iptables ===== Installer et configurer fail2ban ===== sudo apt install fail2ban Le fichier ''/etc/fail2ban/jail.conf'' contient l'ensemble des plugins qu'il est possible d'activer pour protéger les services de la machine. Cependant, il ne **pas** modifier ce fichier directement, car il pourrait être remplacé lors d'une mise à jour du système ! Celui-ci doit servir de référence et de documentation. À la place, il faut modifier le fichier ''/etc/fail2ban/jail.d/defaults-debian.conf'' (ou créer un autre fichier dans ce répertoire) : sudo nano /etc/fail2ban/jail.d/defaults-debian.conf [sshd] enabled = true On constate donc que le service SSH est déjà protégé par défaut. Redéfinir globalement le temps du bannissement (10 minutes par défaut), le temps depuis lequel une anomalie est recherchée dans les logs (10 minutes par défaut) et les adresses IP à ignorer (ici, tout le réseau local) : [sshd] enabled = true [DEFAULT] # "bantime" is the number of seconds that a host is banned. bantime = 3600 # A host is banned if it has generated "maxretry" during the last "findtime" # seconds. findtime = 3600 # "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not # ban a host which matches an address in this list. Several addresses can be # defined using space (and/or comma) separator. ignoreip = 127.0.0.1/8 192.168.0.1/24 Redémarrer le service : sudo service fail2ban restart ==== Commandes utiles ==== Consulter les logs : sudo nano /var/log/fail2ban.log ou sudo tail -f /var/log/fail2ban.log Voir le status du jail ''sshd'' (avec la liste des adresses IP bannies) : sudo fail2ban-client status sshd Débannir une adresse IP du jail ''sshd'' (//pas testé//) : sudo fail2ban-client set sshd unbanip 1.2.3.4 ==== Sources ==== * https://www.geek17.com/fr/content/debian-9-stretch-securiser-votre-serveur-avec-fail2ban-31 * https://sebsauvage.net/wiki/doku.php?id=mint_customization_17_3#autres * https://doc.ubuntu-fr.org/fail2ban ===== Installer et configurer ddclient ===== ''ddclient'' est un client dynamic DNS. Installer ''ddclient'' : sudo apt install ddclient Éditer le fichier de configuration : sudo nano /etc/ddclient.conf # Configuration file for ddclient generated by debconf # # /etc/ddclient.conf syslog=yes #verbose=yes daemon=21600 ssl=yes protocol=dyndns2 use=web server=www.ovh.com login=mondomaine.net-login password='password' dyn.mondomaine.net Le paquet ''libio-socket-ssl-perl'' doit être installé pour que la connexion SSL puisse s'établir ! Redémarrer le service : sudo service ddclient restart Vérifier que le service soit démarré : sudo /etc/init.d/ddclient status Vérifier que le processus est en cours : sudo ps aux | grep ddclient Vérifier les logs : sudo cat /var/log/daemon.log | grep ddclient ==== Sources ==== * https://www.lacaveatonton.ovh/ddclient-le-client-dynhost-sous-linux/ * https://help.ubuntu.com/community/DynamicDNS * https://doc.ubuntu-fr.org/ddclient * https://perhonen.fr/blog/2016/03/dynhost-dyndns-de-chez-ovh-2446 * https://www.monlinux.net/2014/08/dns-dynamique-client-no-ip-dyndns-debian/ ===== Services web (Apache, MariaDB, PHP) ===== ==== MariaDB ==== Installer MariaDB : sudo apt install mariadb-server mariadb-client Lancer l'assistant de sécurisation : sudo mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] y ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB! MariaDB est dès à présent sécurisé. === Créer un utilisateur avec tous les privilèges === sudo mysql GRANT ALL ON *.* TO 'nom_utilisateur'@'localhost' IDENTIFIED BY 'mot_de_passe' WITH GRANT OPTION; FLUSH PRIVILEGES; QUIT; ==== Apache ==== Installer Apache : sudo apt install apache2 Ajouter une règle dans le pare-feu (voir [[installation#configurer_le_pare-feu|cette section]]) : # Accept HTTP -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT Chemins à connaître : * Racine des documents : ''/var/www'' * Fichier de configuration : ''/etc/apache2/apache2.conf'' * Modules : ''/etc/apache2/mods-enabled'' * Virtual hosts : ''/etc/apache2/sites-enabled'' * Global configuration fragments : ''/etc/apache2/conf-enabled'' === Déplacer le répertoire www === Copier le répertoire ''/var/www/'' et son contenu dans le nouvel emplacement (ici, ''/home/www/'') : sudo cp -r /var/www/ /home/www/ Il est également possible de copier le répertoire (et les droits associés) avec [[https://www.digitalocean.com/community/tutorials/how-to-move-an-apache-web-root-to-a-new-location-on-debian-8|rsync]]. Remplacer toutes les occurrences de ''/var/www/'' par le nouveau chemin dans les fichiers suivants : * ''/etc/apache2/apache2.conf'' * ''/etc/apache2/sites-enabled/000-default.conf'' * ''/etc/apache2/sites-available/default-ssl.conf'' Redémarrer Apache : sudo systemctl restart apache2 ==== PHP ==== Installer PHP et plusieurs modules courants : sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-intl php-json php-mbstring php-xml php-zip php-sqlite3 php-imagick php-mcrypt php-memcache Redémarrer Apache : sudo systemctl restart apache2 Tester : sudo nano /home/www/html/info.php ==== phpMyAdmin ==== Installer phpMyAdmin : sudo apt install phpmyadmin FIXME Configurer Tester le fonctionnement de phpMyAdmin en se rendant sur http://192.168.1.10/phpmyadmin S'il n'est pas accessible, il faut modifier la configuration du serveur Apache. Éditer ''/etc/apache2/apache2.conf'' : sudo nano /etc/apache2/apache2.conf Ajouter la ligne suivante à la fin du fichier : Include /etc/phpmyadmin/apache.conf Redémarrer le serveur Apache : sudo systemctl restart apache2 ==== HTTPS ==== Activer le module SSL : sudo a2enmod ssl Recharger la configuration d'Apache : sudo systemctl reload apache2 Finalement, suivre les instructions données [[https://doc.ubuntu-fr.org/apache2#mise_en_place_de_https_avec_certbot|ici]]. Ne pas oublier d'ouvrir le port 443 dans le pare-feu ! ==== Sources ==== * https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-lamp-on-debian-stretch/ * https://doc.ubuntu-fr.org/lamp * https://doc.ubuntu-fr.org/tutoriel/lamp_repertoires_de_travail * https://doc.ubuntu-fr.org/apache2 * https://doc.ubuntu-fr.org/php * https://doc.ubuntu-fr.org/mariadb * https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-debian * https://www.linode.com/docs/web-servers/lamp/lamp-on-debian-8-jessie/ ===== Divers liens utiles ===== * https://www.howtoforge.com/ * https://wiki.debian.org/