diff --git a/home/.chezmoiscripts/universal/run_onchange_after_05-postfix.sh.tmpl b/home/.chezmoiscripts/universal/run_onchange_after_05-postfix.sh.tmpl index e6dfe787..87a16987 100644 --- a/home/.chezmoiscripts/universal/run_onchange_after_05-postfix.sh.tmpl +++ b/home/.chezmoiscripts/universal/run_onchange_after_05-postfix.sh.tmpl @@ -101,8 +101,24 @@ if command -v postfix > /dev/null; then logg info 'Forward user e-mail to root@localhost' echo '{{ .user.username }}: root' | sudo tee -a /etc/aliases > /dev/null fi - logg info 'Running newaliases to regenerate the alias database' - sudo newaliases + + ### Ensure old /etc/aliases.db is removed + if [ -f /etc/aliases.db ]; then + logg info 'Ensuring /etc/aliases.db is removed' && sudo rm -f /etc/aliases.db + else + logg info '/etc/aliases.db was not found' + fi + + ### Re-generate the /etc/aliases.db file + if [ -f /etc/aliases ]; then + logg info 'Ensuring proper permissions on the /etc/aliases file' && chown $(stat -c "%U:%G" /etc/sudoers) /etc/aliases + logg info 'Generating Postfix aliases' && sudo postalias /etc/aliases + else + logg warn '/etc/aliases is missing which is required for Postfix' + fi + # The `sudo newaliases` mode is probably used to regenerate the /etc/aliases.db + # but since we are removing it to ensure proper permissions, this method is commented out. + # logg info 'Running newaliases to regenerate the alias database' && sudo newaliases else logg warn '/etc/aliases does not appear to exist' fi