· 8 years ago · Mar 01, 2018, 12:24 AM
1# Setup a local only, *forwarding* Mail Server (Linux, Unix, Mac)
2
3## 1 - Point *localhost.com* to your machine
4
5 Most of programs will not accept an email using just @localhost as domain.
6 So, edit `/etc/hosts` file to make the domain localhost.com point to your machine by adding this line to the file:
7
8 `127.0.0.1 localhost.com`
9
10To make the change, open the /etc/hosts file using nano or your favorite text editor.
11
12`sudo nano /etc/hosts`
13
14## 2 - Install Postfix
15
16 Fedora/CentOS/RHEL: `sudo yum install postfix`
17
18 Ubuntu: `sudo apt-get install postfix`
19
20 Open SuSE: `sudo zypper install postfix`
21
22 MacOSX: Postfix is already installed by default.
23
24 BSD:Â `sudo pkg install postfix`
25 Â
26
27> If postfix is not found by command pkg try installing from the ports collection:
28>
29> `
30 Â cd /usr/ports/mail/postfix;Â make install clean; ln -s /usr/local/etc/postfix /etc/postfix
31 `
32
33
34During postfix install process, the configure text dialog will display five options:
35
36 ```
37 General type of mail configuration:
38
39 No configuration
40 Internet Site
41 Internet with smarthost
42 Satellite system
43 Local only
44 ```
45
46- Select option "Internet"
47
48For the domain name, use the default suggested or the one you want to use and finish the install.
49
50## 3 - Allow only *local* mail and *forward* to relayhost
51
52In this step, you'll read how to configure Postfix to process requests to send emails only from the server on which it is running, that is, from localhost.
53
54For that to happen, Postfix needs to be configured to listen only on the loopback interface, the virtual network interface that the server uses to communicate internally. To make the change, open the main Postfix configuration file using nano or your favorite text editor.
55
56`sudo nano /etc/postfix/main.cf`
57With the file open, scroll down and search for the following lines:
58
59/etc/postfix/main.cf
60```
61. . .
62mailbox_size_limit = 0
63recipient_delimiter = +
64inet_interfaces = all
65. . .
66relayhost =
67. . .
68```
69
70- Change interface line from `inet_interfaces = all` to `inet_interfaces = loopback-only`.
71- Change relayhost line from `relayhost = ` to `relayhost = [YOUR-SMTP-SERVER-IP-HERE]:587`
72 Â where `[YOUR-SMTP-SERVER-IP-HERE]` has to be replaced by the IP adress of your relay mail server.
73 Â depending on relay mail server you may have to change used port from 587 to 25
74
75```
76. . .
77mailbox_size_limit = 0
78recipient_delimiter = +
79inet_interfaces = loopback-only
80. . .
81relayhost = [YOUR-SMTP-SERVER-IP-HERE]:587
82. . .
83```
84Another directive you'll need to modify is mydestination, which is used to specify the list of domains that are delivered via the local_transport mail delivery transport. By default, the values are similar to these:
85
86/etc/postfix/main.cf
87```
88. . .
89mydestination = $myhostname, example.com, localhost.com, , localhost
90. . .
91```
92- The recommended defaults for that scenario are given in the code block below, so modify yours to match:
93
94```
95. . .
96mydestination = $myhostname, localhost.$mydomain, $mydomain
97. . .
98```
99
100Save and close the file.
101
102## 4 - Activate new Config
103
104 - Reload postfix with systemd:
105 `sudo systemctl restart postfix`
106 - Alternativly use service restart command:
107 `sudo service postfix reload`
108
109## 5 - See Mail Server in Action
110
111You can verify what postfix is doing and that email is sent or not by watching the mail log file.
112Open a terminal and enter:
113
114`sudo tail -f /var/log/mail.log`
115OR
116`sudo journalctl -u postfix`
117
118Tail -f will display the last 10 log entrys and all new incomming log entrys are displayed until you hit `<CTRL> + C`
119
120## 6 -Testing the SMTP Server
121
122In this step, you'll test whether Postfix can send emails to an external email account using the mail command, which is part of the mailutils package that was installe.
123
124
125To send a test email, type:
126
127`echo "This is the body of the email" | mail -s "This is the subject line" your_email_address`
128
129In performing your own test(s), you may use the body and subject line text as-is, or change them to your liking. However, in place of your_email_address, use a valid email address. The domain part can be gmail.com, fastmail.com, yahoo.com, or any other email service provider that you use.
130
131Now check the email address where you sent the test message. You should see the message in your inbox. If not, check your spam folder.
132
133## 7 - Forwarding System Mail
134
135The last thing you may want to set up is forwarding, so you'll get emails sent to root on the system at your personal, external email address.
136
137To configure Postfix so that system-generated emails will be sent to your email address, you need to edit the /etc/aliases file.
138
139`sudo nano /etc/aliases`
140The full contents of the file on a default installation of Ubuntu 16.04 are as follows:
141
142/etc/aliases
143```
144# See man 5 aliases for format
145postmaster: root
146```
147With that setting, system generated emails are sent to the root user. What you want to do is edit it so that those emails are rerouted to your email address. To accomplish that, edit the file so that it reads:
148
149/etc/aliases
150```
151# See man 5 aliases for format
152postmaster: root
153root: your_email_address
154```
155Replace your_email_address with your personal email address. When finished, save and close the file. For the change to take effect, run the following command:
156`sudo newaliases`
157
158You may now test that it works by sending an email to the root account using:
159`echo "This is the body of the email" | mail -s "This is the subject line" root`
160
161You should receive the email at your email address. If not, check your spam folder.