« Réinitialiser une machine Linux » : différence entre les versions

De Adadov.net wiki
Aucun résumé des modifications
Ligne 40 : Ligne 40 :


Le fichier personnalisé est {{Path|/etc/systemd/system/systemd-firstboot.service}}<syntaxhighlight lang="ini">
Le fichier personnalisé est {{Path|/etc/systemd/system/systemd-firstboot.service}}<syntaxhighlight lang="ini">
Contenu du fichier /etc/systemd/system/systemd-firstboot.service
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
 
[Unit]
Description=First Boot Wizard
Documentation=man:systemd-firstboot(1)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-remount-fs.service
Before=systemd-sysusers.service sysinit.target shutdown.target
ConditionPathIsReadWrite=/etc
ConditionFirstBoot=yes
 
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/systemd-firstboot --setup-machine-id --prompt-hostname
StandardOutput=tty
StandardInput=tty
StandardError=tty
 
[Install]
WantedBy=sysinit.target
 
</syntaxhighlight>
</syntaxhighlight>
{{LxTerm|text=/usr/bin/systemctl enable systemd-firstboot}}
{{LxTerm|text=/usr/bin/systemctl enable systemd-firstboot}}

Version du 12 novembre 2018 à 18:02

Ecrit Par : Adadov

Remettre à zéro une station de travail Linux demande de faire attention à quelques détails.

La remise à zéro d'une machine est indispensable afin d'avoir un parc propre.

Les doublons de clés SSH ou d'UUID pourraient vous empêcher d'identifier correctement une machine.

Les hostname en doubles risquent de faire hurler vos serveurs dans de nombreux cas ...

Détails

Afin de ne pas copier des données inutilement ou laisser des informations qui pourraient compromettre la sécurité. Il faut supprimer toutes les infos qui ne sont pas utiles à la nouvelle machine.

Supprimer les fichiers temporaires

[root@linux] # /usr/bin/rm -rf /var/tmp/*dblclick to copy
[root@linux] # /usr/bin/rm -rf /tmp/*

Vider les fichiers de logs

[root@linux] # /usr/bin/cat /dev/null > /var/log/audit/audit.logdblclick to copy
[root@linux] # /usr/bin/cat /dev/null > /var/log/wtmp
[root@linux] # /usr/bin/cat /dev/null > /var/log/lastlog
[root@linux] # /usr/bin/cat /dev/null > /var/log/grubby

Suppression des caches

[root@linux] # /usr/bin/dnf clean alldblclick to copy

Suppression des clés SSH

Pour éviter que des machines puissent se faire passer les unes pour les autres, il faut réinitialiser les identifiants SSH.

Il suffit simplement de supprimer les fichiers contenant les clés pour qu'ils soient générés au prochain démarrage.

[root@linux] # /usr/bin/rm -f /etc/ssh/*key*dblclick to copy
[root@linux] # /usr/bin/rm -f ~root/.ssh/known_hosts

RAZ UUID et Hostname

La remise à zéro de ces paramètres demande quelques modifications pour qu'elle soit optimale.

On va créer un fichier de service systemd personnalisé pour le service firstboot afin qu'il génère automatiquement un nouvel UUID pour la machine et qu'il nous donne un prompt au prochain démarrage pour saisir le nom de la machine.

[root@linux] # /usr/bin/rm -f /etc/machine-id /etc/hostnamedblclick to copy

Le fichier personnalisé est /etc/systemd/system/systemd-firstboot.service

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=First Boot Wizard
Documentation=man:systemd-firstboot(1)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-remount-fs.service
Before=systemd-sysusers.service sysinit.target shutdown.target
ConditionPathIsReadWrite=/etc
ConditionFirstBoot=yes

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/systemd-firstboot --setup-machine-id --prompt-hostname
StandardOutput=tty
StandardInput=tty
StandardError=tty

[Install]
WantedBy=sysinit.target
[root@linux] # /usr/bin/systemctl enable systemd-firstbootdblclick to copy

Utilisation de sys-unconfig

Cette partie doit se faire en dernière, le script fera le nettoyage final puis demandera que le firstboot se lance au prochain démarrage.

Puis il éteindra la machine.

[root@linux] # /usr/sbin/sys-unconfigdblclick to copy

Script

#!/usr/bin/env bash

/usr/bin/rm -rf /var/tmp/*
/usr/bin/rm -rf /tmp/*

/usr/bin/cat /dev/null > /var/log/audit/audit.log
/usr/bin/cat /dev/null > /var/log/wtmp
/usr/bin/cat /dev/null > /var/log/lastlog
/usr/bin/cat /dev/null > /var/log/grubby

/usr/bin/dnf clean all

/usr/bin/rm -f /etc/ssh/*key*
/usr/bin/rm -f /etc/machine-id
/usr/bin/rm -f /etc/hostname

/usr/bin/rm -f ~root/.ssh/known_hosts

/usr/bin/systemctl daemon-reload
/usr/bin/systemctl enable systemd-firstboot

/usr/sbin/sys-unconfig