Configuring Mail Services Using Postfix in Ubuntu Jaunty


Configuring Mail Services Using Postfix in Ubuntu Jaunty

This documentation is available in PDF format. It covers Configuration of the many server services in Ubuntu.

The Problem with Linux Servers

Install postfix as the mail server
root@ismail­laptop:~# apt­get install postfix
you will be prompted in a number of text­based screens to configure Postfix

To reconfigure the these features run
root@ismail­laptop:~# dpkg­reconfigure postfix
Note
After modifying main.cf, be sure to run ‘/etc/init.d/postfix reload’

You will need to further configure Postfix

Backup the main configuration file

root@ismail­laptop:~# cd /etc/postfix/
root@ismail­laptop:/etc/postfix# cp main.cf main.cf.bak
Try the telnet command to explore some of the settings. I tested using IP address, 172.16.0.2 which
is the IP for my localmachine, localhost, etc
root@ismail­laptop:~# telnet 172.16.0.2 25
Trying 172.16.0.2…
Connected to 172.16.0.2.
Escape character is ‘^]’.
220 ismail­laptop ESMTP Postfix (Ubuntu)
root@ismail­laptop:~# telnet 127.0.0.1 25
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.
220 ismail­laptop ESMTP Postfix (Ubuntu)
I changed the myhostname directive
myhostname = mars.example.com
You must restart postfix
# /etc/init.d/./postfix reload
root@ismail­laptop:/etc/postfix# telnet localhost 25
Trying ::1…

Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
220 mars.example.com ESMTP Postfix (Ubuntu)
From the above excerpt output, the hostname is changed. I will change it back to
ubuntu.example.com as it is a Ubuntu machine. This will also serve as my mail server in DNS
settings.

Important
It’s important that you changed your machine hostname so that it reflects the same hostname as in
the configuration file. I changed my hostname to ubunt.example.com by typing
# vi /etc/hostname
ubunt.example.com
You will then need to reboot the server or you can easily typed
# /etc/init.d/./hostname.sh
I could also telnet from mail client, Vector Linux host which I have also configured as my DNS
client
root:# telnet 172.16.0.2 25
Trying 172.16.0.2…
Connected to 172.16.0.2.
Escape character is ‘^]’.
220 mars.example.com ESMTP Postfix (Ubuntu)
I edited the forward and reverse zone files in DNS. I tested these settings
root@ismail­laptop:/etc/bind# host mail.example.com
mail.example.com is an alias for ubuntu.example.com.
ubuntu.example.com has address 172.16.0.2

root@ismail­laptop:/etc/bind# host ubuntu.example.com
ubuntu.example.com has address 172.16.0.2
root@ismail­laptop:/etc/bind# host 172.16.0.2
2.0.16.172.in­addr.arpa domain name pointer ubuntu.example.com.
2.0.16.172.in­addr.arpa domain name pointer ns1.example.com.
2.0.16.172.in­addr.arpa domain name pointer mail.example.com.
I can also ping from my DNS client, Vector Linux
root:# ping ubuntu.example.com
PING ubuntu.example.com (172.16.0.2) 56(84) bytes of data.
64 bytes from ubuntu.example.com (172.16.0.2): icmp_seq=1 ttl=64 time=0.376 ms
64 bytes from mail.example.com (172.16.0.2): icmp_seq=2 ttl=64 time=0.321 ms
­­­ ubuntu.example.com ping statistics ­­­
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.321/0.348/0.376/0.033 ms
root:# ping mail.example.com
PING ubuntu.example.com (172.16.0.2) 56(84) bytes of data.
64 bytes from ns1.example.com (172.16.0.2): icmp_seq=1 ttl=64 time=0.324 ms
64 bytes from ubuntu.example.com (172.16.0.2): icmp_seq=2 ttl=64 time=0.336 ms


Sending Test Mail

root@ismail­laptop:~# mailx

No mail for root
root@ismail­laptop:~# mail ­s “Test Mail”
To: root@example.com
Cc:
Hi this is a test mail
root@ismail­laptop:~# mail
“/var/mail/root”: 1 message 1 new
>N   1 root               Mon Mar  1 18:32  13/448   Test Mail
Press enter to read mail
Subject: Test Mail
To: <root@example.com>
Date: Mon,  1 Mar 2010 18:32:59 +0800 (SGT)
From: root@mail.example.com (root)
Hi this is a test mail
&
To quit press q and enter
& q
Held 1 message in /var/mail/root
Note that the mail is from root@mail.example.com

You can read old mails at /var/mail/root


Sending Mail from local user to local user on localhost

Now try to send mail from a user of the local host
root@ismail­laptop:~# su ­ ismail
To run a command as administrator (user “root”), use “sudo <command>”.
See “man sudo_root” for details.
ismail@ismail­laptop:~$ mail ­s “test mail from user ismail”
To: root@example.com
Cc:
Hi there!
ismail@ismail­laptop:~$
ismail@ismail­laptop:~$ mailx
No mail for ismail
ismail@ismail­laptop:~$ exit
logout
root@ismail­laptop:~# mailx
“/var/mail/root”: 2 messages 1 new
R   1 root               Mon Mar  1 18:32  16/493   Test Mail
>N   2 ismail             Mon Mar  1 18:52  13/461   test mail from user ismai
&
Subject: test mail from user ismail
To: <root@example.com>
Date: Mon,  1 Mar 2010 18:52:32 +0800 (SGT)

From: ismail@mail.example.com (ismail)
Hi there!
&


Just Experimenting..

.
I tried to send mail to a remote host but failed. This is from the /var/log/mail.info
Mar  1 18:56:21 ismail­laptop postfix/qmgr[9125]: 0C11328080: from=<root@mail.example.com>, size=390, nrcpt=1 (queue active)
Mar  1 18:56:21 ismail­laptop postfix/smtp[11221]: connect to mars.example.com[172.16.0.1]:25: Connection refused
Mar  1 18:56:21 ismail­laptop postfix/smtp[11221]: 0C11328080: to=<root@mars.example.com>, relay=none, delay=0.04,
delays=0.03/0.01/0/0, dsn=4.4.1, status=deferred (connect to mars.example.com[172.16.0.1]:25: Connection refused)
Since I have include the mail server, this is the forward and reverse zone files in my DNS


My DNS Forward Zone File

2.0.16.172.in­addr.arpa domain name pointer mail.example.com.
root@ismail­laptop:/etc/bind# more db.example.com
;
; BIND data file for local loopback interface
;
$TTL    604800
@       IN        SOA       ns1.example.com. root.localhost. (
2           ; Serial
604800           ; Refresh
86400           ; Retry
2419200           ; Expire
604800 )         ; Negative Cache TTL
;

IN NS   ns1.example.com.
IN MX   10 mail.example.com.
ns1    IN A    172.16.0.2
mars   IN A    172.16.0.1
ubuntu IN A    172.16.0.2
www    IN CNAME         mars
mail   IN CNAME         ubuntu


My DNS Reverse Zone file

root@ismail­laptop:/etc/bind# more db.16.172
;
; BIND reverse data file for local loopback interface
;
$TTL    604800
@       IN       SOA       ns1.example.com. root.localhost. (
1           ; Serial
604800           ; Refresh
86400           ; Retry
2419200           ; Expire
604800 )         ; Negative Cache TTL
;
@       IN       NS        ns1.example.com.
2.0     IN       PTR       ns1.example.com.
1.0     IN       PTR       mars.example.com.
1.0     IN       PTR       http://www.example.com.
2.0       IN      PTR      mail.example.com.
2.0 IN PTR       ubuntu.example.com.
This is what my /etc/postfix/main.cf configuration file looks like:
root@ismail­laptop:/etc/postfix# less main.cf | grep ­v “^#” |more
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no
smtpd_tls_cert_file=/etc/ssl/certs/ssl­cert­snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl­cert­snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
myhostname = ubuntu.example.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = example.com, ismail­laptop, localhost.localdomain, localhost
relayhost =
mynetworks = 172.16.0.0/16 127.0.0.0/8
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = ipv4
I changed the entries in bold because for the mydestination value it should read mail.example.com
instead of just example.com

As for the mynetworks, it should have comma in between entries.
You can use the postconf utility to edit the main.cf file:
# postconf ­e “mydestination = mail.example.com, ismail­laptop, localhost.localdomain, localhost”
# postconf ­e “mynetworks = 127.0.0.0/8, 172.16.0.0/16”
Make Postfix to receive mail from the Internet
Instruct Postfix to receive on all interfaces:
sudo postconf -e “inet_interfaces = all”
(optional) Make Postfix accept IPv4, IPv6 protocols
If you’re not using IPv6 yet, and you’re paranoid, use “ipv4” instead of “all”. Again, this is to suit
your own network sensibilities.
sudo postconf -e “inet_protocols = all”

Restart Postfix
# ./postfix restart
* Stopping Postfix Mail Transport Agent postfix                         [ OK ]
•    Starting Postfix Mail Transport Agent postfix                         [ OK ]

Send mail from root to user, ismail
# mail ­s “new server”
To: ismail@mail.example.com
Cc:
testing
Cttl+D will exit the mail
Check if user ismail receive the mail
# su ­ ismail
ismail@ismail­laptop:~$ mailx
“/var/mail/ismail”: 3 messages 1 new

R   1 root               Mon Mar  1 23:12  16/481   Scarlet
R   2 root@localhost     Mon Mar  1 23:33  17/479   My first mail
>N   3 root               Tue Mar  2 00:22  13/457   new server
&
Subject: new server
To: <ismail@mail.example.com>
Date: Tue,  2 Mar 2010 00:22:14 +0800 (SGT)
From: root@mail.example.com (root)
testing
&
Yes the newly received mail has the address ismail@mail.example.com
From the /var/log/mail.log
Mar  2 00:39:45 ismail­laptop postfix/pickup[11736]: 7376B28083: uid=0 from=<root>
Mar  2 00:39:45 ismail­laptop postfix/cleanup[13412]: 7376B28083: message­
id=<20100301163945.7376B28083@ubuntu.example.com>
Mar  2 00:39:45 ismail­laptop postfix/qmgr[11737]: 7376B28083: from=<root@mail.example.com>, size=359, nrcpt=1 (queue
active)
Mar  2 00:39:45 ismail­laptop postfix/local[13414]: 7376B28083: to=<ismail@mail.example.com>, relay=local, delay=0.06,
delays=0.03/0.01/0/0.02, dsn=2.0.0, status=sent (delivered to mailbox)
Mar  2 00:39:45 ismail­laptop postfix/qmgr[11737]: 7376B28083: removed


Mail Bounce

trong>
User ismail tried to send mail to root@example.com . It will failed as shown by the logs. The
recipient should be root@mail.example.com
root@ismail­laptop:/etc/bind# su ­ ismail
ismail@ismail­laptop:~$

ismail@ismail­laptop:~$ mail ­s “USA idol”
To: root@example.com
Cc:
Winner
From the /var/log/mail.log
Mar  2 00:34:29 ismail­laptop postfix/pickup[11736]: A144028083: uid=1001 from=<ismail>
Mar  2 00:34:29 ismail­laptop postfix/cleanup[13032]: A144028083: message­
id=<20100301163429.A144028083@ubuntu.example.com>
Mar  2 00:34:29 ismail­laptop postfix/qmgr[11737]: A144028083: from=<ismail@mail.example.com>, size=350, nrcpt=1 (queue
active)
Mar  2 00:34:29 ismail­laptop postfix/smtp[13034]: A144028083: to=<root@example.com>, relay=none, delay=0.06,
delays=0.05/0.01/0/0, dsn=5.4.6, status=bounced (mail for example.com loops back to myself)
Mar  2 00:34:29 ismail­laptop postfix/cleanup[13032]: AFE5628089: message­
id=<20100301163429.AFE5628089@ubuntu.example.com>
Mar  2 00:34:29 ismail­laptop postfix/qmgr[11737]: AFE5628089: from=<>, size=2096, nrcpt=1 (queue active)
Mar  2 00:34:29 ismail­laptop postfix/bounce[13035]: A144028083: sender non­delivery notification: AFE5628089
Mar  2 00:34:29 ismail­laptop postfix/qmgr[11737]: A144028083: removed
Mar  2 00:34:29 ismail­laptop postfix/local[13036]: AFE5628089: to=<ismail@mail.example.com>, relay=local, delay=0.05,
delays=0.02/0.02/0/0.01, dsn=2.0.0, status=sent (delivered to mailbox)
Mar  2 00:34:29 ismail­laptop postfix/qmgr[11737]: AFE5628089: removed
The mail is returned to the sender as shown below
>N   4 Mail Delivery Syst Tue Mar  2 00:34  65/2127  Undelivered Mail Returned
N   5 root               Tue Mar  2 00:39  13/463   Super Bargain
&
Date: Tue,  2 Mar 2010 00:34:29 +0800 (SGT)
From: MAILER­DAEMON@mail.example.com (Mail Delivery System)
Subject: Undelivered Mail Returned to Sender
To: ismail@mail.example.com
This is a MIME­encapsulated message.

­­A144028083.1267461269/ubuntu.example.com
Content­Description: Notification
Content­Type: text/plain; charset=us­ascii
This is the mail system at host ubuntu.example.com.
I’m sorry to have to inform you that your message could not
be delivered to one or more recipients. It’s attached below.
For further assistance, please send mail to postmaster.
If you do so, please include this problem report. You can
delete your own text from the attached returned message.
The mail system
<root@example.com>: mail for example.com loops back to myself
­­A144028083.1267461269/ubuntu.example.com
Content­Description: Delivery report
Content­Type: message/delivery­status
Reporting­MTA: dns; ubuntu.example.com
X­Postfix­Queue­ID: A144028083
X­Postfix­Sender: rfc822; ismail@mail.example.com
Arrival­Date: Tue,  2 Mar 2010 00:34:29 +0800 (SGT)
Final­Recipient: rfc822; root@example.com
Action: failed

Status: 5.4.6
Diagnostic­Code: X­Postfix; mail for example.com loops back to myself
­­A144028083.1267461269/ubuntu.example.com
Content­Description: Undelivered Message
Content­Type: message/rfc822
Received: by ubuntu.example.com (Postfix, from userid 1001)
id A144028083; Tue,  2 Mar 2010 00:34:29 +0800 (SGT)
Subject: USA idol
To: <root@example.com>
X­Mailer: mail (GNU Mailutils 1.2)
Message­Id: <20100301163429.A144028083@ubuntu.example.com>
Date: Tue,  2 Mar 2010 00:34:29 +0800 (SGT)
From: ismail@mail.example.com (ismail)
Winner
­­A144028083.1267461269/ubuntu.example.com­­
&


SOLVED:How to Send Mails to username@example.com

To be able to send mails to username@example.com instead of username@mail.example.com you
will need to check that the directive mydestination has the value example.com and not
mail.example.com. It’s simple as that. In my case the mydestination directive looks like this:
mydestination = example.com, ismail­laptop, localhost.localdomain, localhost
The Postfix main.cf configuration file has the following settings:
root@ismail­laptop:~# less /etc/postfix/main.cf |grep ­v “^#” | more
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no
smtpd_tls_cert_file=/etc/ssl/certs/ssl­cert­snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl­cert­snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
myhostname = ubuntu.example.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = example.com, ismail­laptop, localhost.localdomain, localhost
relayhost =
mynetworks = 127.0.0.0/8, 172.16.0.0/16
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = ipv4
The BIND forward zone file looks like this:
;
; BIND data file for local loopback interface
;
$TTL    604800
@       IN      SOA     ns1.example.com. root.localhost. (
3         ; Serial
604800         ; Refresh
86400         ; Retry
2419200         ; Expire
604800 )       ; Negative Cache TTL
;
IN      NS      ns1.example.com.
IN      MX      10 mail.example.com.
ns1     IN      A       172.16.0.2
mars    IN      A       172.16.0.1
ubuntu  IN      A       172.16.0.2
mail    IN      A       172.16.0.2
www     IN      CNAME   mars
In this example, user ismail will send mail to root@example.com and idris@example.com
# su ­ ismail
ismail@ismail­laptop:~$ mail ­s “Stock Prices”
To: root@example.com
Cc: idris@example.com
Stocks Prices will be out
Press Ctrl+D to send the mail
Switch user to the respective users to see if the mails are received.
ismail@ismail­laptop:~$ su ­ idris
Password:
idris@ismail­laptop:~$ mailx
“/var/mail/idris”: 3 messages 1 new
R   1 root               Tue Mar  2 00:54  16/515   Breaking News
>N   3 ismail             Wed Mar  3 00:29  14/491   Stock Prices
Subject: Stock Prices
To: <root@example.com>
Cc: <idris@example.com>
Date: Wed,  3 Mar 2010 00:29:13 +0800 (SGT)
From: ismail@mail.example.com (ismail)
Stocks Prices will be out
# su – root
#mailx
Subject: Stock Prices
To: <root@example.com>
Cc: <idris@example.com>
Date: Wed,  3 Mar 2010 00:29:13 +0800 (SGT)
From: ismail@mail.example.com (ismail)
Stocks Prices will be out
From the excerpts shown above, mails can be received for both root and user idris. So in this case
we have solve the issue of sending mail to username@example.com instead of typing
username@mail.example.com.


SOLVED: @example.com instead of @mail.example.com

Now if you look carefully at the mail the origin of the mail reads

From: ismail@mail.example.com (ismail)
We do not want to see @mail.example.com. What we want is to see @example.com. To solve this
you will need to edit the following file.
# vi /etc/mailname
example.com
It should read example.com instead of mail.example.com
How I knew that you need to change this file, is that when I look at the postfix configuration file, it
has the following parameter
myorigin = /etc/mailname


Just experimenting

I tried to send to root@ubuntu.example.com. because I thought ubuntu is a CNAME to mail as
configure in the forward zone DNS. I failed to send.
# nslookup mail.example.com
Server:                     127.0.0.1
Address:          127.0.0.1#53
mail.example.com            canonical name = ubuntu.example.com.
Name: ubuntu.example.com
Address: 172.16.0.2
Why it failed? Eventhough ubuntu is a CNAME to mail, but the ubuntu.example.com entries that I
saw in the logs as shown here, actually refers to the hostname in the main.cf configuration file.
From the Logs
Mar  2 00:54:05 ismail­laptop postfix/cleanup[14694]: C950D28083: message­
id=<20100301165405.C950D28083@ubuntu.example.com>
From the main.cf configuration file
myhostname = ubuntu.example.com
If you change the myhostname directive value to something else, let’s say whatever.example.com,

you will see it in the logs as shown below:
Mar  2 04:24:58 ismail­laptop postfix/cleanup[5633]: 70D8528083: message­
id=<20100301202458.70D8528083@
whatever.example.com    >
But later I remove the cname to the mail  and I could send mail to username@ubuntu.example.com
Sending another mail from root to another user
From user root to user idris
# mail ­s “Breaking News”
To: idris@mail.example.com
Cc:
Man escaped from zoo
Checking mail from user idris
root@ismail­laptop:~# su ­ idris
idris@ismail­laptop:~$ mailx
“/var/mail/idris”: 1 message 1 new
>N   1 root               Tue Mar  2 00:54  13/470   Breaking News
&
Subject: Breaking News
To: <idris@mail.example.com>
Date: Tue,  2 Mar 2010 00:54:05 +0800 (SGT)
From: root@mail.example.com (root)
Man escaped from zoo
&

From the /var/log/mail.log
Mar  2 00:54:05 ismail­laptop postfix/pickup[11736]: C950D28083: uid=0 from=<root>
Mar  2 00:54:05 ismail­laptop postfix/cleanup[14694]: C950D28083: message­
id=<20100301165405.C950D28083@ubuntu.example.com>
Mar  2 00:54:05 ismail­laptop postfix/qmgr[11737]: C950D28083: from=<root@mail.example.com>, size=368, nrcpt=1 (queue
active)
Mar  2 00:54:05 ismail­laptop postfix/local[14696]: C950D28083: to=<idris@mail.example.com>, relay=local, delay=0.06,
delays=0.03/0.01/0/0.02, dsn=2.0.0, status=sent (delivered to mailbox)
Mar  2 00:54:05 ismail­laptop postfix/qmgr[11737]: C950D28083: removed

This entry was posted in apache, bind, dhcp, DNS, dovecot, ftp, mail server, network, nfs, nis, postfix, telnet, ubuntu. Bookmark the permalink.

Leave a comment