« Info:: apsi.net: GUIDE GESTION MAIL » : différence entre les versions

De wiki_pcwiki2
Aller à la navigationAller à la recherche
Page créée avec « KEYWORDS: MAIL, SERVER, APSI.NET, OVH1, MAIL.APSI.NET, OVH2, BACKUP.APSI.NET '''Le 02/01/2026:''' »
 
Aucun résumé des modifications
Ligne 2 : Ligne 2 :


  '''Le 02/01/2026:'''
  '''Le 02/01/2026:'''
<span id="guide-de-gestion-mail---ovh1-ovh2"></span>
= Guide de Gestion Mail - ovh1 &amp; ovh2 =
<span id="configuration-actuelle-apsi.net-voilou.nl"></span>
== Configuration actuelle : apsi.net + voilou.nl ==
-----
<span id="différenciation-des-boîtes-mail"></span>
== 1. DIFFÉRENCIATION DES BOÎTES MAIL ==
<span id="configuration-actuelle"></span>
=== Configuration Actuelle ===
'''Domaine apsi.net :''' - <code>pchevalier@apsi.net</code> → <code>/home/pchevalier/Maildir/</code> - <code>contact@apsi.net</code> → <code>/home/contact/Maildir/</code>
'''Domaine voilou.nl (séparé) :''' - <code>pchevalier@voilou.nl</code> → <code>/home/pchevalier-voilou/Maildir/</code> - <code>contact@voilou.nl</code> → <code>/home/contact-voilou/Maildir/</code>
<span id="fichier-de-configuration-etcpostfixvirtual"></span>
=== Fichier de configuration : <code>/etc/postfix/virtual</code> ===
<pre>pchevalier@apsi.net    pchevalier
contact@apsi.net      contact
pchevalier@voilou.nl  pchevalier-voilou
contact@voilou.nl      contact-voilou</pre>
<span id="modifier-les-alias-virtuels"></span>
=== Modifier les alias virtuels ===
<ol style="list-style-type: decimal;">
<li><p>'''Éditer le fichier :'''</p>
<syntaxhighlight lang="bash">nano /etc/postfix/virtual</syntaxhighlight></li>
<li><p>'''Ajouter une entrée :'''</p>
<pre>nouvelle-adresse@apsi.net    utilisateur-unix</pre></li>
<li><p>'''Compiler et recharger :'''</p>
<syntaxhighlight lang="bash">postmap /etc/postfix/virtual
systemctl reload postfix</syntaxhighlight></li>
<li><p>'''Tester le mapping :'''</p>
<syntaxhighlight lang="bash">postmap -q "nouvelle-adresse@apsi.net" hash:/etc/postfix/virtual</syntaxhighlight></li></ol>
-----
<span id="créer-des-alias"></span>
== 2. CRÉER DES ALIAS ==
<span id="alias-simples-redirection"></span>
=== Alias Simples (redirection) ===
'''Fichier :''' <code>/etc/postfix/virtual</code>
<pre># Exemples d'alias
postmaster@apsi.net    contact
abuse@apsi.net          contact
admin@apsi.net          pchevalier
support@voilou.nl      contact-voilou
# Alias vers plusieurs destinataires
info@apsi.net          pchevalier,contact</pre>
Après modification :
<syntaxhighlight lang="bash">postmap /etc/postfix/virtual
systemctl reload postfix</syntaxhighlight>
<span id="alias-système-domaine-local"></span>
=== Alias Système (domaine local) ===
'''Fichier :''' <code>/etc/aliases</code>
<pre>postmaster:    root
root:          pchevalier</pre>
Après modification :
<syntaxhighlight lang="bash">newaliases
systemctl reload postfix</syntaxhighlight>
-----
<span id="commandes-essentielles---ovh1-serveur-principal"></span>
== 3. COMMANDES ESSENTIELLES - OVH1 (Serveur Principal) ==
<span id="gestion-des-emails"></span>
=== Gestion des Emails ===
<span id="voir-la-queue-des-emails"></span>
==== Voir la queue des emails ====
<syntaxhighlight lang="bash">mailq
# ou
postqueue -p</syntaxhighlight>
<span id="supprimer-un-email-de-la-queue"></span>
==== Supprimer un email de la queue ====
<syntaxhighlight lang="bash">postsuper -d QUEUE_ID
# Exemple: postsuper -d 3A5F17FA00AF</syntaxhighlight>
<span id="supprimer-tous-les-emails-de-la-queue"></span>
==== Supprimer tous les emails de la queue ====
<syntaxhighlight lang="bash">postsuper -d ALL</syntaxhighlight>
<span id="vider-la-queue-forcer-lenvoi"></span>
==== Vider la queue (forcer l’envoi) ====
<syntaxhighlight lang="bash">postqueue -f</syntaxhighlight>
<span id="voir-les-détails-dun-email-en-queue"></span>
==== Voir les détails d’un email en queue ====
<syntaxhighlight lang="bash">postcat -vq QUEUE_ID</syntaxhighlight>
<span id="lire-un-email-dans-le-maildir"></span>
==== Lire un email dans le Maildir ====
<syntaxhighlight lang="bash"># Dernier email reçu
cat $(ls -t /home/pchevalier/Maildir/new/ | head -1 | xargs -I {} echo /home/pchevalier/Maildir/new/{})
# Lister les emails
ls -lth /home/pchevalier/Maildir/new/
ls -lth /home/pchevalier/Maildir/cur/</syntaxhighlight>
<span id="logs-et-diagnostic"></span>
=== Logs et Diagnostic ===
<span id="suivre-les-logs-en-temps-réel"></span>
==== Suivre les logs en temps réel ====
<syntaxhighlight lang="bash">tail -f /var/log/mail.log
# Avec filtres
tail -f /var/log/mail.log | grep -E "DKIM|error|reject"</syntaxhighlight>
<span id="rechercher-dans-les-logs"></span>
==== Rechercher dans les logs ====
<syntaxhighlight lang="bash"># Emails pour un destinataire
grep "to=<contact@voilou.nl>" /var/log/mail.log
# Emails depuis une IP
grep "client=mail.example.com\[1.2.3.4\]" /var/log/mail.log
# Erreurs récentes
grep -i "error\|warning\|reject" /var/log/mail.log | tail -50</syntaxhighlight>
<span id="vérifier-les-connexions-actives"></span>
==== Vérifier les connexions actives ====
<syntaxhighlight lang="bash"># Connexions SMTP
netstat -tnp | grep :25
# Connexions IMAP
netstat -tnp | grep :143</syntaxhighlight>
<span id="tests-et-vérification"></span>
=== Tests et Vérification ===
<span id="tester-smtp-localement"></span>
==== Tester SMTP localement ====
<syntaxhighlight lang="bash">echo "Test email" | sendmail -v destinataire@example.com</syntaxhighlight>
<span id="tester-smtp-avec-authentification"></span>
==== Tester SMTP avec authentification ====
<syntaxhighlight lang="bash"># Générer auth string
echo -ne '\000username\000password' | base64
# Tester manuellement
telnet localhost 587
EHLO mail.apsi.net
STARTTLS
AUTH PLAIN [base64_string]</syntaxhighlight>
<span id="tester-imap"></span>
==== Tester IMAP ====
<syntaxhighlight lang="bash">telnet localhost 143
a1 LOGIN utilisateur@apsi.net motdepasse
a2 LIST "" "*"
a3 SELECT INBOX
a4 LOGOUT</syntaxhighlight>
<span id="tester-dkim"></span>
==== Tester DKIM ====
<syntaxhighlight lang="bash"># Vérifier les clés
opendkim-testkey -d apsi.net -s mail -vvv
opendkim-testkey -d voilou.nl -s mail -vvv
# Vérifier signature dans un email
grep "DKIM-Signature" /home/pchevalier/Maildir/cur/*</syntaxhighlight>
<span id="services"></span>
=== Services ===
<span id="redémarrer-les-services"></span>
==== Redémarrer les services ====
<syntaxhighlight lang="bash">systemctl restart postfix
systemctl restart dovecot
systemctl restart opendkim
systemctl restart apache2</syntaxhighlight>
<span id="vérifier-le-statut"></span>
==== Vérifier le statut ====
<syntaxhighlight lang="bash">systemctl status postfix
systemctl status dovecot
systemctl status opendkim</syntaxhighlight>
<span id="recharger-la-configuration-sans-interruption"></span>
==== Recharger la configuration (sans interruption) ====
<syntaxhighlight lang="bash">systemctl reload postfix
systemctl reload dovecot</syntaxhighlight>
<span id="configuration-postfix"></span>
=== Configuration Postfix ===
<span id="voir-la-configuration-active"></span>
==== Voir la configuration active ====
<syntaxhighlight lang="bash">postconf | grep virtual
postconf | grep mydestination
postconf -n  # Voir uniquement les valeurs modifiées</syntaxhighlight>
<span id="tester-la-configuration"></span>
==== Tester la configuration ====
<syntaxhighlight lang="bash">postfix check</syntaxhighlight>
<span id="recharger-après-modification"></span>
==== Recharger après modification ====
<syntaxhighlight lang="bash">postfix reload</syntaxhighlight>
-----
<span id="commandes-essentielles---ovh2-serveur-relay"></span>
== 4. COMMANDES ESSENTIELLES - OVH2 (Serveur Relay) ==
<span id="gestion-de-la-queue-relay"></span>
=== Gestion de la Queue Relay ===
<span id="voir-la-queue"></span>
==== Voir la queue ====
<syntaxhighlight lang="bash">ssh root@ovh2.pcstreet.org "mailq"</syntaxhighlight>
<span id="forcer-lenvoi-des-emails-en-queue"></span>
==== Forcer l’envoi des emails en queue ====
<syntaxhighlight lang="bash">ssh root@ovh2.pcstreet.org "postqueue -f"</syntaxhighlight>
<span id="supprimer-un-email"></span>
==== Supprimer un email ====
<syntaxhighlight lang="bash">ssh root@ovh2.pcstreet.org "postsuper -d QUEUE_ID"</syntaxhighlight>
<span id="supprimer-tous-les-emails"></span>
==== Supprimer tous les emails ====
<syntaxhighlight lang="bash">ssh root@ovh2.pcstreet.org "postsuper -d ALL"</syntaxhighlight>
<span id="logs-relay"></span>
=== Logs Relay ===
<span id="voir-les-logs-récents"></span>
==== Voir les logs récents ====
<syntaxhighlight lang="bash">ssh root@ovh2.pcstreet.org "tail -50 /var/log/mail.log"</syntaxhighlight>
<span id="suivre-les-logs-en-temps-réel-1"></span>
==== Suivre les logs en temps réel ====
<syntaxhighlight lang="bash">ssh root@ovh2.pcstreet.org "tail -f /var/log/mail.log"</syntaxhighlight>
<span id="vérifier-les-tentatives-de-relay"></span>
==== Vérifier les tentatives de relay ====
<syntaxhighlight lang="bash">ssh root@ovh2.pcstreet.org "grep 'relay=' /var/log/mail.log | tail -20"</syntaxhighlight>
<span id="tests-relay"></span>
=== Tests Relay ===
<span id="envoyer-un-test-depuis-ovh2"></span>
==== Envoyer un test depuis ovh2 ====
<syntaxhighlight lang="bash">ssh root@ovh2.pcstreet.org 'echo "Test relay" | sendmail pchevalier@apsi.net'</syntaxhighlight>
<span id="vérifier-connectivité-vers-ovh1"></span>
==== Vérifier connectivité vers ovh1 ====
<syntaxhighlight lang="bash">ssh root@ovh2.pcstreet.org "telnet mail.apsi.net 25"</syntaxhighlight>
-----
<span id="gestion-des-utilisateurs"></span>
== 5. GESTION DES UTILISATEURS ==
<span id="créer-un-nouvel-utilisateur-mail"></span>
=== Créer un nouvel utilisateur mail ===
<syntaxhighlight lang="bash"># Créer l'utilisateur Unix
useradd -m -s /bin/bash nouveau-user
echo "nouveau-user:motdepasse" | chpasswd
# Ajouter dans /etc/postfix/virtual
echo "nouveau@apsi.net    nouveau-user" >> /etc/postfix/virtual
# Compiler et recharger
postmap /etc/postfix/virtual
systemctl reload postfix
# Créer le Maildir (automatique à la réception du 1er email)
# Ou manuellement :
su - nouveau-user
maildirmake ~/Maildir</syntaxhighlight>
<span id="changer-un-mot-de-passe-utilisateur"></span>
=== Changer un mot de passe utilisateur ===
<syntaxhighlight lang="bash"># Pour l'accès IMAP/SMTP (= mot de passe Unix)
passwd pchevalier
# Ou en une commande
echo "pchevalier:nouveau_mot_de_passe" | chpasswd</syntaxhighlight>
<span id="supprimer-un-utilisateur-mail"></span>
=== Supprimer un utilisateur mail ===
<syntaxhighlight lang="bash"># Supprimer de /etc/postfix/virtual
nano /etc/postfix/virtual  # Supprimer la ligne
postmap /etc/postfix/virtual
systemctl reload postfix
# Supprimer l'utilisateur Unix (optionnel)
userdel -r ancien-user</syntaxhighlight>
-----
<span id="statistiques-et-monitoring"></span>
== 6. STATISTIQUES ET MONITORING ==
<span id="statistiques-denvoiréception"></span>
=== Statistiques d’envoi/réception ===
<syntaxhighlight lang="bash"># Résumé des logs du jour
pflogsumm /var/log/mail.log | less
# Installer si nécessaire:
apt install pflogsumm</syntaxhighlight>
<span id="top-expéditeursdestinataires"></span>
=== Top expéditeurs/destinataires ===
<syntaxhighlight lang="bash"># Top 10 expéditeurs
grep "from=<" /var/log/mail.log | sed 's/.*from=<\([^>]*\)>.*/\1/' | sort | uniq -c | sort -rn | head -10
# Top 10 destinataires
grep "to=<" /var/log/mail.log | sed 's/.*to=<\([^>]*\)>.*/\1/' | sort | uniq -c | sort -rn | head -10</syntaxhighlight>
<span id="taille-des-boîtes-mail"></span>
=== Taille des boîtes mail ===
<syntaxhighlight lang="bash">du -sh /home/*/Maildir/</syntaxhighlight>
-----
<span id="sécurité---fail2ban"></span>
== 7. SÉCURITÉ - FAIL2BAN ==
<span id="voir-les-bannissements-actifs"></span>
=== Voir les bannissements actifs ===
<syntaxhighlight lang="bash">fail2ban-client status postfix-sasl
fail2ban-client status dovecot
fail2ban-client status sshd</syntaxhighlight>
<span id="débannir-une-ip"></span>
=== Débannir une IP ===
<syntaxhighlight lang="bash">fail2ban-client set postfix-sasl unbanip 1.2.3.4
fail2ban-client set dovecot unbanip 1.2.3.4</syntaxhighlight>
<span id="bannir-manuellement-une-ip"></span>
=== Bannir manuellement une IP ===
<syntaxhighlight lang="bash">fail2ban-client set postfix-sasl banip 1.2.3.4</syntaxhighlight>
<span id="voir-les-logs-fail2ban"></span>
=== Voir les logs fail2ban ===
<syntaxhighlight lang="bash">tail -f /var/log/fail2ban.log</syntaxhighlight>
-----
<span id="certificats-ssltls"></span>
== 8. CERTIFICATS SSL/TLS ==
<span id="vérifier-expiration-certificat"></span>
=== Vérifier expiration certificat ===
<syntaxhighlight lang="bash">openssl x509 -in /etc/letsencrypt/live/mail.apsi.net/fullchain.pem -noout -dates</syntaxhighlight>
<span id="renouveler-manuellement"></span>
=== Renouveler manuellement ===
<syntaxhighlight lang="bash">systemctl stop apache2
certbot renew
systemctl start apache2
systemctl reload postfix dovecot apache2</syntaxhighlight>
<span id="tester-certificat-smtp"></span>
=== Tester certificat SMTP ===
<syntaxhighlight lang="bash">echo "Q" | openssl s_client -connect mail.apsi.net:587 -starttls smtp</syntaxhighlight>
<span id="tester-certificat-imap"></span>
=== Tester certificat IMAP ===
<syntaxhighlight lang="bash">echo "Q" | openssl s_client -connect mail.apsi.net:993</syntaxhighlight>
-----
<span id="dépannage-courant"></span>
== 9. DÉPANNAGE COURANT ==
<span id="email-non-reçu"></span>
=== Email non reçu ===
<ol style="list-style-type: decimal;">
<li><p>'''Vérifier les logs :'''</p>
<syntaxhighlight lang="bash">grep "to=<destinataire@domain.com>" /var/log/mail.log | tail -20</syntaxhighlight></li>
<li><p>'''Vérifier la queue :'''</p>
<syntaxhighlight lang="bash">mailq</syntaxhighlight></li>
<li><p>'''Vérifier le Maildir :'''</p>
<syntaxhighlight lang="bash">ls -lth /home/utilisateur/Maildir/new/</syntaxhighlight></li></ol>
<span id="email-bloqué-en-queue"></span>
=== Email bloqué en queue ===
<ol style="list-style-type: decimal;">
<li><p>'''Identifier le problème :'''</p>
<syntaxhighlight lang="bash">postcat -vq QUEUE_ID</syntaxhighlight></li>
<li><p>'''Forcer l’envoi :'''</p>
<syntaxhighlight lang="bash">postqueue -f</syntaxhighlight></li>
<li><p>'''Supprimer si nécessaire :'''</p>
<syntaxhighlight lang="bash">postsuper -d QUEUE_ID</syntaxhighlight></li></ol>
<span id="erreur-dauthentification-imapsmtp"></span>
=== Erreur d’authentification IMAP/SMTP ===
<ol style="list-style-type: decimal;">
<li><p>'''Vérifier le mot de passe :'''</p>
<syntaxhighlight lang="bash">passwd utilisateur</syntaxhighlight></li>
<li><p>'''Tester manuellement :'''</p>
<syntaxhighlight lang="bash">doveadm auth test utilisateur@apsi.net motdepasse</syntaxhighlight></li>
<li><p>'''Vérifier les logs :'''</p>
<syntaxhighlight lang="bash">grep "auth failed" /var/log/mail.log | tail -20</syntaxhighlight></li></ol>
<span id="spam-email-non-désiré"></span>
=== Spam / Email non désiré ===
<ol style="list-style-type: decimal;">
<li><p>'''Identifier l’expéditeur :'''</p>
<syntaxhighlight lang="bash">grep "from=<spammer@" /var/log/mail.log</syntaxhighlight></li>
<li><p>'''Bannir l’IP si nécessaire :'''</p>
<syntaxhighlight lang="bash">fail2ban-client set postfix banip 1.2.3.4</syntaxhighlight></li></ol>
-----
<span id="vérifications-dns"></span>
== 10. VÉRIFICATIONS DNS ==
<span id="vérifier-les-enregistrements"></span>
=== Vérifier les enregistrements ===
<syntaxhighlight lang="bash"># MX
dig +short MX apsi.net
dig +short MX voilou.nl
# SPF
dig +short TXT apsi.net
dig +short TXT voilou.nl
# DKIM
dig +short TXT mail._domainkey.apsi.net
dig +short TXT mail._domainkey.voilou.nl
# DMARC
dig +short TXT _dmarc.apsi.net
dig +short TXT _dmarc.voilou.nl</syntaxhighlight>
<span id="tests-externes"></span>
=== Tests externes ===
'''Envoyer un email de test à :''' - check-auth@verifier.port25.com (top) - postmaster@dkimvalidator.com
'''Vérifier en ligne :''' - https://mxtoolbox.com/SuperTool.aspx - https://www.mail-tester.com/ (payant au dela de 3 tests par jour)
-----
<span id="backup-et-restauration"></span>
== 11. BACKUP ET RESTAURATION ==
<span id="backup-des-emails"></span>
=== Backup des emails ===
<syntaxhighlight lang="bash"># Backup complet d'un utilisateur
tar -czf /backup/pchevalier-mail-$(date +%Y%m%d).tar.gz /home/pchevalier/Maildir/
# Backup de tous les utilisateurs
for user in pchevalier contact pchevalier-voilou contact-voilou; do
    tar -czf /backup/${user}-mail-$(date +%Y%m%d).tar.gz /home/${user}/Maildir/
done</syntaxhighlight>
<span id="restauration"></span>
=== Restauration ===
<syntaxhighlight lang="bash"># Restaurer pour un utilisateur
tar -xzf /backup/pchevalier-mail-20260101.tar.gz -C /
# Corriger les permissions
chown -R pchevalier:pchevalier /home/pchevalier/Maildir/</syntaxhighlight>
-----
<span id="raccourcis-utiles"></span>
== 12. RACCOURCIS UTILES ==
<span id="alias-à-ajouter-dans-.bashrc"></span>
=== Alias à ajouter dans ~/.bashrc ===
<syntaxhighlight lang="bash"># Gestion mail
alias maillog='tail -f /var/log/mail.log'
alias mailq='postqueue -p'
alias mailflush='postqueue -f'
alias mailcheck='postfix check'
# Fail2ban
alias f2bstatus='fail2ban-client status'
alias f2bpostfix='fail2ban-client status postfix-sasl'
alias f2bdovecot='fail2ban-client status dovecot'
# Logs
alias logsmail='tail -100 /var/log/mail.log'
alias logsfail2ban='tail -100 /var/log/fail2ban.log'</syntaxhighlight>
-----
<span id="résumé-des-fichiers-importants"></span>
== RÉSUMÉ DES FICHIERS IMPORTANTS ==
<span id="configuration"></span>
=== Configuration ===
* <code>/etc/postfix/main.cf</code> - Configuration principale Postfix
* <code>/etc/postfix/virtual</code> - Alias virtuels (compilation: postmap)
* <code>/etc/aliases</code> - Alias système (compilation: newaliases)
* <code>/etc/dovecot/dovecot.conf</code> - Configuration Dovecot
* <code>/etc/opendkim.conf</code> - Configuration DKIM
<span id="logs"></span>
=== Logs ===
* <code>/var/log/mail.log</code> - Logs Postfix/Dovecot
* <code>/var/log/fail2ban.log</code> - Logs Fail2ban
<span id="données"></span>
=== Données ===
* <code>/home/*/Maildir/</code> - Boîtes mail des utilisateurs
* <code>/var/spool/postfix/</code> - Queue Postfix
<span id="certificats"></span>
=== Certificats ===
* <code>/etc/letsencrypt/live/mail.apsi.net/</code> - Certificats SSL/TLS

Version du 2 janvier 2026 à 10:07

KEYWORDS: MAIL, SERVER, APSI.NET, OVH1, MAIL.APSI.NET, OVH2, BACKUP.APSI.NET

Le 02/01/2026:

Guide de Gestion Mail - ovh1 & ovh2

Configuration actuelle : apsi.net + voilou.nl


1. DIFFÉRENCIATION DES BOÎTES MAIL

Configuration Actuelle

Domaine apsi.net : - pchevalier@apsi.net → /home/pchevalier/Maildir/ - contact@apsi.net → /home/contact/Maildir/

Domaine voilou.nl (séparé) : - pchevalier@voilou.nl → /home/pchevalier-voilou/Maildir/ - contact@voilou.nl → /home/contact-voilou/Maildir/

Fichier de configuration : /etc/postfix/virtual

pchevalier@apsi.net    pchevalier
contact@apsi.net       contact
pchevalier@voilou.nl   pchevalier-voilou
contact@voilou.nl      contact-voilou

Modifier les alias virtuels

  1. Éditer le fichier :

    nano /etc/postfix/virtual
    
  2. Ajouter une entrée :

    nouvelle-adresse@apsi.net    utilisateur-unix
  3. Compiler et recharger :

    postmap /etc/postfix/virtual
    systemctl reload postfix
    
  4. Tester le mapping :

    postmap -q "nouvelle-adresse@apsi.net" hash:/etc/postfix/virtual
    



2. CRÉER DES ALIAS

Alias Simples (redirection)

Fichier : /etc/postfix/virtual

# Exemples d'alias
postmaster@apsi.net     contact
abuse@apsi.net          contact
admin@apsi.net          pchevalier
support@voilou.nl       contact-voilou

# Alias vers plusieurs destinataires
info@apsi.net           pchevalier,contact

Après modification :

postmap /etc/postfix/virtual
systemctl reload postfix

Alias Système (domaine local)

Fichier : /etc/aliases

postmaster:    root
root:          pchevalier

Après modification :

newaliases
systemctl reload postfix

3. COMMANDES ESSENTIELLES - OVH1 (Serveur Principal)

Gestion des Emails

Voir la queue des emails

mailq
# ou
postqueue -p

Supprimer un email de la queue

postsuper -d QUEUE_ID
# Exemple: postsuper -d 3A5F17FA00AF

Supprimer tous les emails de la queue

postsuper -d ALL

Vider la queue (forcer l’envoi)

postqueue -f

Voir les détails d’un email en queue

postcat -vq QUEUE_ID

Lire un email dans le Maildir

# Dernier email reçu
cat $(ls -t /home/pchevalier/Maildir/new/ | head -1 | xargs -I {} echo /home/pchevalier/Maildir/new/{})

# Lister les emails
ls -lth /home/pchevalier/Maildir/new/
ls -lth /home/pchevalier/Maildir/cur/

Logs et Diagnostic

Suivre les logs en temps réel

tail -f /var/log/mail.log

# Avec filtres
tail -f /var/log/mail.log | grep -E "DKIM|error|reject"

Rechercher dans les logs

# Emails pour un destinataire
grep "to=<contact@voilou.nl>" /var/log/mail.log

# Emails depuis une IP
grep "client=mail.example.com\[1.2.3.4\]" /var/log/mail.log

# Erreurs récentes
grep -i "error\|warning\|reject" /var/log/mail.log | tail -50

Vérifier les connexions actives

# Connexions SMTP
netstat -tnp | grep :25

# Connexions IMAP
netstat -tnp | grep :143

Tests et Vérification

Tester SMTP localement

echo "Test email" | sendmail -v destinataire@example.com

Tester SMTP avec authentification

# Générer auth string
echo -ne '\000username\000password' | base64

# Tester manuellement
telnet localhost 587
EHLO mail.apsi.net
STARTTLS
AUTH PLAIN [base64_string]

Tester IMAP

telnet localhost 143
a1 LOGIN utilisateur@apsi.net motdepasse
a2 LIST "" "*"
a3 SELECT INBOX
a4 LOGOUT

Tester DKIM

# Vérifier les clés
opendkim-testkey -d apsi.net -s mail -vvv
opendkim-testkey -d voilou.nl -s mail -vvv

# Vérifier signature dans un email
grep "DKIM-Signature" /home/pchevalier/Maildir/cur/*

Services

Redémarrer les services

systemctl restart postfix
systemctl restart dovecot
systemctl restart opendkim
systemctl restart apache2

Vérifier le statut

systemctl status postfix
systemctl status dovecot
systemctl status opendkim

Recharger la configuration (sans interruption)

systemctl reload postfix
systemctl reload dovecot

Configuration Postfix

Voir la configuration active

postconf | grep virtual
postconf | grep mydestination
postconf -n  # Voir uniquement les valeurs modifiées

Tester la configuration

postfix check

Recharger après modification

postfix reload

4. COMMANDES ESSENTIELLES - OVH2 (Serveur Relay)

Gestion de la Queue Relay

Voir la queue

ssh root@ovh2.pcstreet.org "mailq"

Forcer l’envoi des emails en queue

ssh root@ovh2.pcstreet.org "postqueue -f"

Supprimer un email

ssh root@ovh2.pcstreet.org "postsuper -d QUEUE_ID"

Supprimer tous les emails

ssh root@ovh2.pcstreet.org "postsuper -d ALL"

Logs Relay

Voir les logs récents

ssh root@ovh2.pcstreet.org "tail -50 /var/log/mail.log"

Suivre les logs en temps réel

ssh root@ovh2.pcstreet.org "tail -f /var/log/mail.log"

Vérifier les tentatives de relay

ssh root@ovh2.pcstreet.org "grep 'relay=' /var/log/mail.log | tail -20"

Tests Relay

Envoyer un test depuis ovh2

ssh root@ovh2.pcstreet.org 'echo "Test relay" | sendmail pchevalier@apsi.net'

Vérifier connectivité vers ovh1

ssh root@ovh2.pcstreet.org "telnet mail.apsi.net 25"

5. GESTION DES UTILISATEURS

Créer un nouvel utilisateur mail

# Créer l'utilisateur Unix
useradd -m -s /bin/bash nouveau-user
echo "nouveau-user:motdepasse" | chpasswd

# Ajouter dans /etc/postfix/virtual
echo "nouveau@apsi.net    nouveau-user" >> /etc/postfix/virtual

# Compiler et recharger
postmap /etc/postfix/virtual
systemctl reload postfix

# Créer le Maildir (automatique à la réception du 1er email)
# Ou manuellement :
su - nouveau-user
maildirmake ~/Maildir

Changer un mot de passe utilisateur

# Pour l'accès IMAP/SMTP (= mot de passe Unix)
passwd pchevalier

# Ou en une commande
echo "pchevalier:nouveau_mot_de_passe" | chpasswd

Supprimer un utilisateur mail

# Supprimer de /etc/postfix/virtual
nano /etc/postfix/virtual  # Supprimer la ligne
postmap /etc/postfix/virtual
systemctl reload postfix

# Supprimer l'utilisateur Unix (optionnel)
userdel -r ancien-user

6. STATISTIQUES ET MONITORING

Statistiques d’envoi/réception

# Résumé des logs du jour
pflogsumm /var/log/mail.log | less

# Installer si nécessaire:
apt install pflogsumm

Top expéditeurs/destinataires

# Top 10 expéditeurs
grep "from=<" /var/log/mail.log | sed 's/.*from=<\([^>]*\)>.*/\1/' | sort | uniq -c | sort -rn | head -10

# Top 10 destinataires
grep "to=<" /var/log/mail.log | sed 's/.*to=<\([^>]*\)>.*/\1/' | sort | uniq -c | sort -rn | head -10

Taille des boîtes mail

du -sh /home/*/Maildir/

7. SÉCURITÉ - FAIL2BAN

Voir les bannissements actifs

fail2ban-client status postfix-sasl
fail2ban-client status dovecot
fail2ban-client status sshd

Débannir une IP

fail2ban-client set postfix-sasl unbanip 1.2.3.4
fail2ban-client set dovecot unbanip 1.2.3.4

Bannir manuellement une IP

fail2ban-client set postfix-sasl banip 1.2.3.4

Voir les logs fail2ban

tail -f /var/log/fail2ban.log

8. CERTIFICATS SSL/TLS

Vérifier expiration certificat

openssl x509 -in /etc/letsencrypt/live/mail.apsi.net/fullchain.pem -noout -dates

Renouveler manuellement

systemctl stop apache2
certbot renew
systemctl start apache2
systemctl reload postfix dovecot apache2

Tester certificat SMTP

echo "Q" | openssl s_client -connect mail.apsi.net:587 -starttls smtp

Tester certificat IMAP

echo "Q" | openssl s_client -connect mail.apsi.net:993

9. DÉPANNAGE COURANT

Email non reçu

  1. Vérifier les logs :

    grep "to=<destinataire@domain.com>" /var/log/mail.log | tail -20
    
  2. Vérifier la queue :

    mailq
    
  3. Vérifier le Maildir :

    ls -lth /home/utilisateur/Maildir/new/
    

Email bloqué en queue

  1. Identifier le problème :

    postcat -vq QUEUE_ID
    
  2. Forcer l’envoi :

    postqueue -f
    
  3. Supprimer si nécessaire :

    postsuper -d QUEUE_ID
    

Erreur d’authentification IMAP/SMTP

  1. Vérifier le mot de passe :

    passwd utilisateur
    
  2. Tester manuellement :

    doveadm auth test utilisateur@apsi.net motdepasse
    
  3. Vérifier les logs :

    grep "auth failed" /var/log/mail.log | tail -20
    

Spam / Email non désiré

  1. Identifier l’expéditeur :

    grep "from=<spammer@" /var/log/mail.log
    
  2. Bannir l’IP si nécessaire :

    fail2ban-client set postfix banip 1.2.3.4
    



10. VÉRIFICATIONS DNS

Vérifier les enregistrements

# MX
dig +short MX apsi.net
dig +short MX voilou.nl

# SPF
dig +short TXT apsi.net
dig +short TXT voilou.nl

# DKIM
dig +short TXT mail._domainkey.apsi.net
dig +short TXT mail._domainkey.voilou.nl

# DMARC
dig +short TXT _dmarc.apsi.net
dig +short TXT _dmarc.voilou.nl

Tests externes

Envoyer un email de test à : - check-auth@verifier.port25.com (top) - postmaster@dkimvalidator.com

Vérifier en ligne : - https://mxtoolbox.com/SuperTool.aspx - https://www.mail-tester.com/ (payant au dela de 3 tests par jour)



11. BACKUP ET RESTAURATION

Backup des emails

# Backup complet d'un utilisateur
tar -czf /backup/pchevalier-mail-$(date +%Y%m%d).tar.gz /home/pchevalier/Maildir/

# Backup de tous les utilisateurs
for user in pchevalier contact pchevalier-voilou contact-voilou; do
    tar -czf /backup/${user}-mail-$(date +%Y%m%d).tar.gz /home/${user}/Maildir/
done

Restauration

# Restaurer pour un utilisateur
tar -xzf /backup/pchevalier-mail-20260101.tar.gz -C /

# Corriger les permissions
chown -R pchevalier:pchevalier /home/pchevalier/Maildir/

12. RACCOURCIS UTILES

Alias à ajouter dans ~/.bashrc

# Gestion mail
alias maillog='tail -f /var/log/mail.log'
alias mailq='postqueue -p'
alias mailflush='postqueue -f'
alias mailcheck='postfix check'

# Fail2ban
alias f2bstatus='fail2ban-client status'
alias f2bpostfix='fail2ban-client status postfix-sasl'
alias f2bdovecot='fail2ban-client status dovecot'

# Logs
alias logsmail='tail -100 /var/log/mail.log'
alias logsfail2ban='tail -100 /var/log/fail2ban.log'

RÉSUMÉ DES FICHIERS IMPORTANTS

Configuration

  • /etc/postfix/main.cf - Configuration principale Postfix
  • /etc/postfix/virtual - Alias virtuels (compilation: postmap)
  • /etc/aliases - Alias système (compilation: newaliases)
  • /etc/dovecot/dovecot.conf - Configuration Dovecot
  • /etc/opendkim.conf - Configuration DKIM

Logs

  • /var/log/mail.log - Logs Postfix/Dovecot
  • /var/log/fail2ban.log - Logs Fail2ban

Données

  • /home/*/Maildir/ - Boîtes mail des utilisateurs
  • /var/spool/postfix/ - Queue Postfix

Certificats

  • /etc/letsencrypt/live/mail.apsi.net/ - Certificats SSL/TLS