You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
6.1 KiB

---
Alias: ["Postfix"]
Tag: ["Server", "Email"]
Date: 2021-09-10
DocType: "Personal"
Hierarchy: "NonRoot"
TimeStamp:
location: [51.514678599999996, -0.18378583926867909]
CollapseMetaTable: Yes
---
3 years ago
Parent:: [[Selfhosting]], [[Server Alias]], [[Server Cloud]], [[Server Tools]], [[Server VPN]]
3 years ago
---
^Top
 
```button
name Save
type command
action Save current file
id Save
```
^button-TNSave
 
# Configuring Postfix
 
```ad-abstract
title: Summary
collapse: open
This note goes through the installation of Postfix, an email-handling solution for servers.
```
 
```toc
style: number
```
 
---
 
### Installation
3 years ago
[[#^Top|TOP]]
 
Installation is simple and opens a dialogue box. Steps are described below:
1. **Install Postfix**
3 years ago
```ad-command
~~~bash
3 years ago
sudo apt-get install -y postfix postfix-pgsql -y
~~~
```
2. **Configuration screen**
Choose 'Internet Site'
3. **System mail name**
Define a name and make a note of it for further use.
 
---
 
### Generic configuration
3 years ago
[[#^Top|TOP]]
 
```ad-note
title: Documentation
[Basic config](https://github.com/simple-login/app)
```
The basic configuration is run in detail below.
 
#### Main.cf
3 years ago
[[#^Top|TOP]]
3 years ago
```ad-path
~~~
/etc/postfix/main.cf
3 years ago
~~~
```
Edit the file whilst editing the following values:
1. myhostname (current server)
2. mydomain (associated domain)
3. my origin (associated domain)
3 years ago
```ad-code
title: main.cf
~~~
###### POSTFIX config file, adapted for SimpleLogin
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
3 years ago
###### appending .domain is the MUA's job.
append_dot_mydomain = no
3 years ago
###### 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
3 years ago
###### TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtp_tls_security_level = may
smtpd_tls_security_level = may
3 years ago
###### See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
###### information on enabling SSL in the smtp client.
alias_maps = hash:/etc/aliases
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 240.0.0.0/24
3 years ago
###### Set your domain here
mydestination =
myhostname = app.mydomain.com
mydomain = mydomain.com
myorigin = mydomain.com
3 years ago
relay_domains = pgsql:/etc/postfix/pgsql-relay-domains.cf
transport_maps = pgsql:/etc/postfix/pgsql-transport-maps.cf
3 years ago
###### HELO restrictions
smtpd_delay_reject = yes
smtpd_helo_required = yes
smtpd_helo_restrictions =
3 years ago
permit_mynetworks,
reject_non_fqdn_helo_hostname,
reject_invalid_helo_hostname,
permit
###### Sender restrictions:
smtpd_sender_restrictions =
3 years ago
permit_mynetworks,
reject_non_fqdn_sender,
reject_unknown_sender_domain,
permit
3 years ago
###### Recipient restrictions:
smtpd_recipient_restrictions =
reject_unauth_pipelining,
reject_non_fqdn_recipient,
reject_unknown_recipient_domain,
permit_mynetworks,
reject_unauth_destination,
reject_rbl_client zen.spamhaus.org,
reject_rbl_client bl.spamcop.net,
permit
~~~
```
 
#### relay domains
3 years ago
[[#^Top|TOP]]
3 years ago
```ad-path
~~~
/etc/postfix/pgsql-relay-domains.cf
3 years ago
~~~
```
Below is the default config for the file (replace mydomain.com with relevant value):
3 years ago
```ad-code
title: master.cf
~~~
###### postgres config
hosts = localhost
user = myuser
password = mypassword
dbname = simplelogin
3 years ago
query = SELECT domain FROM custom_domain WHERE domain='%s' AND verified=true
UNION SELECT '%s' WHERE '%s' = 'mydomain.com' LIMIT 1;
~~~
```
 
#### transport maps
3 years ago
[[#^Top|TOP]]
3 years ago
```ad-path
~~~
/etc/postfix/pgsql-transport-maps.cf
3 years ago
~~~
```
Below is the standard declaration of the file (replace mydomain.com with the relevant value):
3 years ago
```ad-code
title: pgsql-transport-maps.cf
~~~
###### postgres config
hosts = localhost
user = myuser
password = mypassword
dbname = simplelogin
3 years ago
###### forward to smtp:127.0.0.1:20381 for custom domain AND email domain
query = SELECT 'smtp:127.0.0.1:20381' FROM custom_domain WHERE domain = '%s' AND verified=true
3 years ago
UNION SELECT 'smtp:127.0.0.1:20381' WHERE '%s' = 'mydomain.com' LIMIT 1;
~~~
```
 
#### Generic commands
3 years ago
[[#^Top|TOP]]
##### Restart the service
3 years ago
```ad-command
~~~bash
3 years ago
sudo systemctl restart postfix
~~~
```
 
---
 
### Allow external connections
3 years ago
[[#^Top|TOP]]
 
```ad-note
title: Documentation
[Fwd to external smtp](https://github.com/simple-login/app/discussions/612)
```
 
#### Incoming
3 years ago
[[#^Top|TOP]]
##### Ports
3 years ago
Ports (25, 465, 587) need to be opened by the firewall:
3 years ago
```ad-command
~~~bash
3 years ago
sudo ufw allow (port)/tcp
~~~
```
3 years ago
3 years ago
 
##### Networks
3 years ago
[[#^Top|TOP]]
3 years ago
Definition of permitted networks to restrict access (in [[Configuring Postfix#Main cf|main.cf]]).
3 years ago
 
3 years ago
##### Submission/SMTPD
3 years ago
3 years ago
```ad-path
~~~
3 years ago
/etc/postfix/master.cf
3 years ago
~~~
3 years ago
```
Options are coded in the file by default. Uncheck necessary directives.
3 years ago
 
##### SSL cert
3 years ago
[[#^Top|TOP]]
3 years ago
Postfix SSL certs need to match that of the server (in [[Configuring Postfix#Main cf|main.cf]]).
3 years ago
 
##### SASL
3 years ago
[[#^Top|TOP]]
3 years ago
[Tutorial](https://github.com/webmin/webmin/issues/58)
 
##### Add SASL users
3 years ago
```ad-command
title: add a user
~~~bash
3 years ago
sudo saslpasswd2 -c -u (mydomain.tld) (username)
~~~
```
3 years ago
```ad-command
title: check number of users
~~~bash
3 years ago
sudo sasldblistusers2
~~~
```
 
#### Outgoing
Allow for connecting to an external SMTP - not tested.
3 years ago
 
---
 
### Monitor Postfix
3 years ago
[[#^Top|TOP]]
3 years ago
 
#### Monitor outgoing emails
Unfortunately there is no built-in functionality to monitor specifically outgoing emails. The following command can however be used and grans relevant info from `mail.log`:
3 years ago
```ad-command
~~~bash
3 years ago
sudo grep "status=sent" /var/log/mail.log | egrep -ve 'postfix/(cleanup|pickup|master|qmgr|smtpd|local|pipe)'
~~~
```
3 years ago
 
#### Monitor SMTP connections
3 years ago
```ad-command
~~~bash
3 years ago
sudo tail -f /var/log/mail.log
~~~
```
3 years ago
3 years ago
[[#^Top|TOP]]