返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
系统配置

Linux Configure System Email Forwarding to Designated Mailbox

Preface In Linux operations, sometimes every time you press Enter, the prompt "You have new mail in /var/spool/mail/root" appears. What exactly is the reason for this? The Linux system often automatically sends out some emails to alert users about issues in the system (inbox location: /var/mail/). However, these emails are all sent to the root user. Considering system security, it is generally not recommended for everyone to directly use the root account for daily operations. Therefore, we need to find a way to let the system...

曦远 (DealiAxy)约 8 分钟
目录6 个章节

Preface

In Linux operations, sometimes after pressing Enter, the message "You have new mail in /var/spool/mail/root" appears. Why does this happen? Linux systems often automatically send emails to notify users about issues within the system (mailbox location: /var/mail/). However, these emails are all sent to the root user. For system security reasons, it is generally not recommended to use the root account for daily operations. Therefore, you need to find a way to either forward the emails sent to the root user to a designated external mailbox or simply disable this service.

Disabling the sendmail Service

This method introduces a way to disable the sendmail service without actually shutting it down.

echo “unset MAILCHECK” >> /etc/profile
source /etc/profile

To disable the functionality of sendmail:

chmod 0 /usr/sbin/sendmail
mv /usr/sbin/sendmail /usr/sbin/sendmail.bak
ln -s /var/qmail/bin/sendmail /usr/sbin/sendmail

Clear the /var/spool/mail/root log

cat /dev/null > /var/spool/mail/root
cat /dev/null>;/var/spool/mail/root

Forward root Emails to Your Own Mailbox

Method 1

Modify this file

/etc/log.d/logwatch.conf
添加 MailTo = root,xxx@xxx.com

Method 2

Modify /etc/aliases
Add root: xxx@xxx.com

Note: It seems that if the recipient is set to be in the same domain as the host, the email cannot be sent successfully. For example, if the local machine's email is moper.me, sending to this address will fail, while sending to other email addresses will succeed.

Regarding "/etc/aliases":

When sendmail receives a message addressed to a specific user, it delivers the message to another user based on the contents of the /etc/aliases file. This feature allows the creation of a user that is only valid within the mail system. For instance, this function is used for mailing lists. In a mailing list, we might create an address like redlinux@link.ece.uci.edu, but there is no actual user named "redmail". In effect, the aliases file directs all mail sent to this user to the mailing list processing program, which is responsible for distributing the messages.

The /etc/aliases file is a text document, but sendmail requires a binary format file, /etc/aliases.db. The purpose of the newaliases command is to convert /etc/aliases into a database file that sendmail can understand:

[root@centos ~]# newaliases

Mail for users other than root can be forwarded by creating a .forward file in their /home/ directory:

//somebody
other1
other2

Setting the file permissions to 600 achieves the same effect. However, the .forward file can be maintained by the user themselves, whereas the aliases file can only be modified by the administrator.

To set it up, simply add the forwarding destination to the ~/.forward file.

References

http://moper.me/linux-you-have-new-mail-in.html

_EOF_

Linux Configure System Email Forwarding to Designated Mailbox

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

在Nginx上配置https加密服务器

前言 最近开发微信小程序需要使用Https,于是折腾了一番。 超简单步骤 1. 申请SSL证书 一般云服务器提供商就会提供这种服务,我在腾讯云上申请了,十分钟就完成了。 2. 上传证书 上传到一个文件夹里面 3. 配置nginx 网络上的资料都是直接修改 /etc/nginx/nginx.conf 这个文件。 不过我的服务器上这个文件里头没有 server 这个节点可以配置。 因此需要修改 /et

2025-10-27 · 系统配置 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…