Setup a Mail Server with Postfix and Fetchmail
Previous Update: Dec 13 2019
Latest Update: Dec 14 2020
OS: Kubuntu 18.04
Postfix is a mail server,or MTA (Mail Transfer Agent). It accepts messages and delivers them.(Postfix created by Wietse Venema)
Fetchmail is a remote-mail retrieval system, providing home users, who don't have corporate accounts, the ability to pull down mail from an ISP, or in this case Gmail, to our local Linux box.
01 - Configure the hostname:
hostname -f
And to change your hostname:
sudo hostnamectl set-hostname YOURHOSTNAME
02 - Install Postfix:
sudo apt-get update
sudo apt-get install postfix
General type of mail configuration:
- 'No configuration' -> means the installation process will not configure any parameters.
- 'Internet Site' -> means using Postfix for sending emails to other MTAs and receiving email from other MTAs.
- 'Internet with smarthost' -> means using postfix to receive email from other MTAs, but using another smart host to relay emails to the recipient.
- 'Satellite system' -> means using smart host for sending and receiving email.
- 'Local only' -> means emails are transmitted only between local user accounts.
System mail name:
Next, enter your domain name for the system mail name, i.e. the domain name after @ symbol. This domain name will be appended to addresses that doesn’t have a domain name specified. Once installed, Postfix will be automatically started and a /etc/postfix/main.cf file will be generated. Now we can check Postfix version with this command:
sudo postconf mail_version
Postfix ships with many binaries under the /usr/sbin/ directory, as can be seen with the following command.
dpkg -L postfix | grep /usr/sbin/
Output:
/usr/sbin/postalias /usr/sbin/postcat /usr/sbin/postconf /usr/sbin/postdrop /usr/sbin/postfix /usr/sbin/postfix-add-filter /usr/sbin/postfix-add-policy /usr/sbin/postkick /usr/sbin/postlock /usr/sbin/postlog /usr/sbin/postmap /usr/sbin/postmulti /usr/sbin/postqueue /usr/sbin/postsuper /usr/sbin/posttls-finger /usr/sbin/qmqp-sink /usr/sbin/qmqp-source /usr/sbin/qshape /usr/sbin/rmail /usr/sbin/sendmail /usr/sbin/smtp-sink /usr/sbin/smtp-source
Sample Settings:
General type of mail configuration: Internet with smarthost
System mail name: DOMAIN.TLD
Root and postmaster mail recipient: YOURUSERNAME
Other destinations to accept mail for: $myhostname, DOMAIN.TLD, mail.DOMAIN.TLD, localhost.DOMAIN.TLD, localhost
Force synchronous updates on mail queue?: no
Local networks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
Mailbox size limit: (in ubuntu)0
Local address extension character:+
Internet protocols to use: all
sudo dpkg-reconfigure postfix
03 - Open ports in firewall
sudo ufw allow 25/tcp
sudo ufw allow 587/tcp
sudo ufw allow 465/tcp
sudo ufw allow 143/tcp
sudo ufw allow 993/tcp
04 - Checking If Port 25 (outbound) is blocked
telnet localhost 25
Trying 74.125.68.26... Connected to localhost Escape character is '^]'. 220 MAIL.DOMAIN.TLD ESMTP Postfix (Ubuntu)
05 - Generating Certificates:
openssl genrsa -des3 -out MAIL.DOMAIN.TLD.key 2048
openssl req -new -key mail.domain.tld.key -out MAIL.DOMAIN.TLD.csr
openssl x509 -req -days 365 -in MAIL.DOMAIN.TLD.csr -signkey MAIL.DOMAIN.TLD.key -out MAIL.DOMAIN.TLD.crt
openssl rsa -in MAIL.DOMAIN.TLD.key -out MAIL.DOMAIN.TLD.key.nopass
mv MAIL.DOMAIN.TLD.key.nopass MAIL.DOMAIN.TLD.key
openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem -days 3650
chmod 600 mail.domain.tld.key
chmod 600 cakey.pem
mv mail.domain.tld.key /etc/ssl/private/
mv mail.domain.tld.crt /etc/ssl/certs/
mv cakey.pem /etc/ssl/private/
mv cacert.pem /etc/ssl/certs/
postconf -e 'smtpd_use_tls = yes'
postconf -e 'smtpd_tls_auth_only = no'
postconf -e 'smtpd_tls_key_file = /etc/ssl/private/MAIL.DOMAIN.TLD.key'
postconf -e 'smtpd_tls_cert_file = /etc/ssl/certs/mail.domain.tld.crt'
postconf -e 'smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem'
postconf -e 'tls_random_source = dev:/dev/urandom'
postconf -e 'myhostname = MAIL.DOMAIN.TLD'
06 - Postfix main.cf
# See /usr/share/postfix/main.cf.dist for a commented, more complete version # Debian specific: Specifying a file name will cause the first # line of that file to be used as the name. The Debian default # is /etc/mailname. #myorigin = /etc/mailname smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu) biff = no # appending .domain is the MUA's job. append_dot_mydomain = no # Uncomment the next line to generate "delayed mail" warnings #delay_warning_time = 4h readme_directory = no # See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on # fresh installs. compatibility_level = 2 # TLS parameters # For Server smtpd_use_tls=yes smtpd_tls_cert_file=/etc/ssl/certs/mail.reganto.ir.crt smtpd_tls_key_file=/etc/ssl/private/mail.reganto.ir.key
smtpd_tls_CAfile=/etc/ssl/certs/cacert.pem smtpd_tls_auth_only=no smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache # For Client smtp_use_tls=yes smtp_tls_cert_file=/etc/ssl/certs/mail.reganto.ir.crt
smtp_tls_key_file=/etc/ssl/private/mail.reganto.ir.key
smtp_tls_CAfile=/etc/ssl/certs/cacert.pem # smtp_tls_auth_only=no smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache tls_random_source = dev:/dev/urandom # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for # information on enabling SSL in the smtp client. smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination myhostname = mail.reganto.ir
alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases myorigin = /etc/mailname mydestination = $myhostname, mail.reganto.ir, reganto.ir, localhost.com, localhost
# relayhost = [smtp.gmail.com]:587 # fallback_relay = relayhost = mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all inet_protocols = all ## SASL Settings smtpd_sasl_auth_enable = no smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtpd_sasl_local_domain = $myhostname smtp_sasl_security_options = noanonymous #smtp_sasl_security_options = smtp_sasl_tls_security_options = noanonymous # smtpd_sasl_application_name = smtpd # Disable DNS Lookups disable_dns_lookups = yes # Generic Mechanism smtp_generic_maps = hash:/etc/postfix/generic # Transport Mechanism transport_maps = hash:/etc/postfix/transport # LOG ;) smtp_tls_loglevel = 8 virtual_alias_maps = hash:/etc/postfix/virtual # Report troubles to postmaster error_notice_recipient = postmaster notify_classes = resource, software # smtp_tls_per_site = hash:/etc/postfix/smtp_tls_per_site smtp_tls_policy_maps = hash:/etc/postfix/tls_policy smtpd_enforce_tls = no
07 - Install MUA(mail user agent) to Send and Read Email
sudo apt-get install mailutils
mail USERNAME
08 - Create a new account on Postfix
sudo adduser USERNAME
sudo postconf -e 'virtual_alias_maps= hash:/etc/postfix/virtual'
sudo vim /etc/postfix/virtual
contact@DOMAIN.TLD reganto dev@DOMAIN.TLD reganto
sudo postmap /etc/postfix/virtual
sudo service postfix restart
09 - Creating Email Alias
sudo vim /etc/aliases
# See man 5 aliases for format postmaster: root root: USERNAME
sudo newaliases
10 - sasl_passwd
# Contents of sasl_passwd [smtp.gmail.com]:587 rreganto@gmail.com:asdfqwerasacoiap
postmap /etc/postfix/sasl_passwd
postmap -q [smtp.gmail.com]:587 sasl_passwd
rreganto@gmail.com:asdfqwerasacoiap
chown root.postfix sasl_passwd*
chmod 0640 sasl_passwd*
* Add following lines to main.cf (sasl_passwd configs)
## SASL Settings smtpd_sasl_auth_enable = no smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtpd_sasl_local_domain = $myhostname smtp_sasl_security_options = noanonymous #smtp_sasl_security_options = smtp_sasl_tls_security_options = noanonymous # smtpd_sasl_application_name = smtpd
11 - generic (Postfix on hosts without a real Internet hostname)
smtp_generic_maps = hash:/etc/postfix/generic
sudo vim /etc/postfix/generic
@mail.reganto.ir rreganto@gmail.com
dev@HOSTNAME.LOCAL ACCOUNT1@ISP.TLD foo@HOSTNAME.LOCAL ACCOUNT2@ISP.TLD
postmap /etc/postfix/generic
12 - Transport
# Internal Delivery mail.reganto.ir : # External Delivery * smtp:[smtp.gmail.com]:587
postmap /etc/postfix/transport
13 - Fetchmail
sudo apt-get install fetchmail
14 - Google Gmail Certificates
openssl s_client -connect pop.gmail.com:995 -showcerts
15 - Certificate of the CA
Equifax Secure CA ================= MD5 Fingerprint: 67:CB:9D:C0:13:24:8A:82:9B:B2:17:1E:D1:1B:EC:D4 PEM Data: -----BEGIN CERTIFICATE----- MIIDIDCCAomgAwIBAgIENd70zzANBgkqhkiG9w0BAQUFADBOMQswCQYDVQQGEwJV UzEQMA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2Vy dGlmaWNhdGUgQXV0aG9yaXR5MB4XDTk4MDgyMjE2NDE1MVoXDTE4MDgyMjE2NDE1 MVowTjELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0VxdWlmYXgxLTArBgNVBAsTJEVx dWlmYXggU2VjdXJlIENlcnRpZmljYXRlIEF1dGhvcml0eTCBnzANBgkqhkiG9w0B AQEFAAOBjQAwgYkCgYEAwV2xWGcIYu6gmi0fCG2RFGiYCh7+2gRvE4RiIcPRfM6f BeC4AfBONOziipUEZKzxa1NfBbPLZ4C/QgKO/t0BCezhABRP/PvwDN1Dulsr4R+A cJkVV5MW8Q+XarfCaCMczE1ZMKxRHjuvK9buY0V7xdlfUNLjUA86iOe/FP3gx7kC AwEAAaOCAQkwggEFMHAGA1UdHwRpMGcwZaBjoGGkXzBdMQswCQYDVQQGEwJVUzEQ MA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2VydGlm aWNhdGUgQXV0aG9yaXR5MQ0wCwYDVQQDEwRDUkwxMBoGA1UdEAQTMBGBDzIwMTgw ODIyMTY0MTUxWjALBgNVHQ8EBAMCAQYwHwYDVR0jBBgwFoAUSOZo+SvSspXXR9gj IBBPM5iQn9QwHQYDVR0OBBYEFEjmaPkr0rKV10fYIyAQTzOYkJ/UMAwGA1UdEwQF MAMBAf8wGgYJKoZIhvZ9B0EABA0wCxsFVjMuMGMDAgbAMA0GCSqGSIb3DQEBBQUA A4GBAFjOKer89961zgK5F7WF0bnj4JXMJTENAKaSbn+2kmOeUJXRmm/kEd5jhW6Y 7qj/WsjTVbJmcVfewCHrPSqnI0kBBIZCe/zuf6IWUrVnZ9NA2zsmWLIodz2uFHdh 1voqZiegDfqnc1zqcPGUIWVEX/r87yloqaKHee9570+sB3c4 -----END CERTIFICATE-----
16 - Rehash or Creating Symlinks
c_rehash ~/certs/.certs
17 - Checking the Certificate
openssl s_client -connect pop.gmail.com:995 -CApath ~/certs/.certs/
18 - The Fetchmail config
poll mail.example.com protocol pop3: username "elessar" password "lkfhsifvmaksadel" there is "aragorn" here;
username "mithrandir" password "qwokmnspkiuamjrt" there is "gandalf" here;
# Check mail every 600 seconds set daemon 600 set syslog set postmaster reganto #set bouncemail # user 'rreganto@gmail.com' with password "uiemjapqkjfutyhr" is 'reganto' here options ssl sslcertck sslcertpath '~/certs/.certs' keep # poll pop.gmail.com with proto POP3 and options no dns user 'rreganto@gmail.com' there with password "uiemjapqkjfutyhr" is 'reganto' here options ssl sslcertck sslcertpath '/home/reganto/certs/.certs' # deliver email to postfix on localhost smtphost localhost # You would use this to by-pass Postfix # mda '/usr/bin/procmail -d %T'
19 - Fetchmail Commands
fetchmail -q # quits fetchmail daemon fetchmail -v # start fetchmail daemon in verbose mode fetchmail -c # checks for email only fetchmail -S localhost # delivers mail to your Postfix server
20 - resources
- postfix.org
- souptonuts.sourceforge.net/postfix_tutorial.html
- linuxbabe.com/mail-server/setup-basic-postfix-mail-sever-ubuntu
- fetchmail.info
- other