16

I have an Ubuntu server running postfix. It is not the mail server for my domain.

Whenever a cron job runs for root, the output mail is not delivered locally, instead it gets sent to root@mydomain.com via the main mail server. This is not what I want.

I want mail for root to either be delivered locally or forwarded to anothermail@anotherdomain.com.

I've tried modifying both ~root/.forward and /etc/aliases (and running newaliases), but nothing helps (I guess these files are only checked when postfix tries to deliver mail locally).

What can I do?

This is /etc/postfix/main.cf:

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 = linux1.mydomain.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = linux1.mydomain.com, localhost.linux1.mydomain.com, localhost
relayhost = my.isps.relayhost.com
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only

Edit:

When sending mail to root, this goes into /var/log/mail.log:

Mar  7 09:39:17 linux1 postfix/pickup[31381]: F3B9C98025E: uid=1000 from=<ct>
Mar  7 09:39:18 linux1 postfix/cleanup[31556]: F3B9C98025E: message-id=<20130307083917.F3B9C98025E@linux1.mydomain.com>
Mar  7 09:39:18 linux1 postfix/qmgr[28525]: F3B9C98025E: from=<ct@mydomain.com>, size=283, nrcpt=1 (queue active)
Mar  7 09:39:18 linux1 postfix/smtp[31558]: F3B9C98025E: to=<root@mydomain.com>, orig_to=<root>, relay=my.isps.relayhost.com[<IP address omitted>]:25, delay=0.72, delays=0.19/0.02/0.27/0.25, dsn=2.0.0, status=sent (250 Ok: queued as A97F5D8126)
Mar  7 09:39:18 linux1 postfix/qmgr[28525]: F3B9C98025E: removed

The name "ct" is my user name. I generated the above text through this command:

echo test | mail -s test root

The content of /etc/mailname is:

mydomain.com

The contents of /etc/aliases is:

root: anothermail@anotherdomain.com
postmaster:    root

where anothermail@anotherdomain.com is where I would like root's mail to be forwarded to.

The content of /etc/hosts actually surprises me a little:

127.0.0.1 localhost
127.0.1.1 linux1.mylinux.mydomain.com linux1

where "mylinux" is the hostname of a host operating system under which linux1 runs as a virtual machine. I'm not sure how "mylinux" got in there. (But could this really be the reason for my problem?)

oz1cz
  • 1,575
  • 3
  • 11
  • 10
  • Could you provide us your postfix logs when you send a mail to root ? – Dom Mar 07 '13 at 07:56
  • I have done so in an edit to the original post. – oz1cz Mar 07 '13 at 08:44
  • Could you give us your /etc/mailname and /etc/aliases files ? Check if /etc/hosts contains the linux1.mydomain.com assigned to 127.0.1.1 – Dom Mar 07 '13 at 13:41
  • I have added the information to the original post. – oz1cz Mar 07 '13 at 15:05
  • According to your logs, the mail has been correctly sent out to your isp smtp relay. I'd ask them (isp) to check my.isps.relayhost.com[] logs for any clue (antispam, error, ...) about the problem. –  Mar 07 '13 at 15:16
  • Actually the logs show the email being delivered to the wrong address. He wants it delivered to anothermail@anotherdomain.com, not root@mydomain.com. – gparent Mar 07 '13 at 15:18
  • This is for Dom: I turns out that the problem arises because of the difference between the hostname in `/etc/mailname` and the contents of `/etc/hosts`. I fixed that, and now the mail works nicely. Thank you, Dom. If you could write this as a proper answer, I will acknowledge it as a solution. – oz1cz Mar 07 '13 at 15:48

5 Answers5

15

As ususal, check your logs.

In your case, the postfix daemon thinks the mail is not for it and sends it without using /etc/aliases

First check your /etc/hosts file : it should have your machine name corresponding to 127.0.1.1, like this:

127.0.1.1  linux1.mydomain.com  linux1

Check your /etc/mailname too, and it should be consistent.

Check your /etc/aliases to see if root (user) is sent to another user, and redo the newaliases command.

And it should work!

MeSo2
  • 254
  • 1
  • 3
  • 18
Dom
  • 6,743
  • 1
  • 20
  • 24
  • Should that be `127.0.0.1`? – Nate Feb 27 '15 at 22:30
  • You can have any address in 127.0.0.0/8, so 127.0.0.1 works well too – Dom Feb 28 '15 at 09:36
  • I had a similar problem while I only wanted to use a smarthost relay to send mails out: "myhostname" had to be "localhost". Anything else skipped the aliases file. – Alex Nov 04 '15 at 22:16
9

If mydestination is empty or does not contain $myhostname then the /etc/aliases will be ignored because postfix thinks the email is not a local delivery and will therefore not apply the local aliases. So, leave mydestination at the default (postconf -d mydestination or remove it from main.cf) and the logs should show the to=<...> as your aliased address.

ColinM
  • 701
  • 8
  • 19
4

In some circumstances, (i.e. where all mail gets relayed to an external system), it's easier to just set the MAILTO variable in root's crontab to a real email address. This should pretty much bypass traditional delivery to root and just make it go where you want.

# Root's crontab
MAILTO=someone@example.com
0 0 * * * /usr/bin/somescript
Dale C. Anderson
  • 587
  • 1
  • 5
  • 13
0

In case you happen to have set up destination domain filtering in /etc/postfix/transport then you need to add the subdomain of your machine to the allowed domains list. And in case you are using some VPS and the hosting provider has assigned some automatic internal domain name to your system, then you may need to add this too.

For example, if you had:

mydomain.com                :
anotherdomain.com           :
*                           discard:

Update it to:

uvn-12-34.am01.hostingvs.eu :
linux1.mylinux.mydomain.com :
mydomain.com                :
anotherdomain.com           :
*                           discard:

Without this update the line root: anothermail@anotherdomain.com in /etc/aliases or a similar one in .forward does not have the desired end effect since the message is discarded before being forwarded.

Roland Pihlakas
  • 271
  • 3
  • 7
0

There seems to be some issue with opening of /root/.forward by postfix daemon, at least on hardened system (I didn't dig into it). Despite:

# ls -ld / /root /root/.forward
dr-xr-xr-x. 18 root root  236 Dec  4 00:02 /
dr-xr-x--x. 13 root root 4096 Jan  8 17:45 /root
-rw-r--r--.  1 root root   30 Jan  8 14:51 /root/.forward

Child of postfix daemon is not able to access .forward:

7603  open("/etc/passwd", O_RDONLY|O_CLOEXEC) = 16
7603  fstat(16, {st_mode=S_IFREG|0644, st_size=1227, ...}) = 0
7603  mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f86854fb000
7603  read(16, "root:x:0:0:root:/root:/bin/bash\n"..., 4096) = 1227
7603  close(16)                         = 0
7603  munmap(0x7f86854fb000, 4096)      = 0
7603  geteuid()                         = 89
7603  getegid()                         = 89
7603  geteuid()                         = 89
7603  setresuid(-1, 0, -1)              = 0
7603  setresgid(-1, 0, -1)              = 0
7603  setgroups(1, [0])                 = 0
7603  lstat("/root/.forward", 0x7ffc03f84750) = -1 EACCES (Permission denied)

I didn't have time to dig into it... Appending entry to /etc/aliases followed by newaliases works fine.

r2oro
  • 1
  • 1