Linux邮件相关问题

 

关闭You have new mail in /var/spool/mail/root 提示

有的时候每敲一下回车,就出来You have new mail in /var/spool/mail/root的提示,究竟是为什么呢?

Linux 系统经常会自动发出一些邮件来提醒用户系统中出了哪些问题(收件箱位置:/var/mail/)。可是这些邮件都是发送给 root 用户的。出于系统安全考虑,通常不建议大家直接使用 root 帐户进行日常操作。

所以要想点办法来让系统把发给 root 用户的邮件也给自己指定的外部邮箱发一份,或者是直接关闭此项服务。

 

方法一:不用关闭sendmail服务的方法

echo "unset MAILCHECK" >> /etc/profile
source /etc/profile

 

方法二:关闭sendmail服务

1) service sendmail stop
     这种方法停止sendmail服务但在下次启动系统时sendmail服务仍然会被启动。
2) chkconfig 停止
     # chkconfig --list sendmail
 sendmail       0:off  1:off  2:on   3:on   4:on   5:on   6:off
    可以看到2,34,5都是on状态 
    # chkconfig --level 2 sendmail off 
    # chkconfig --level 3 sendmail off 
    # chkconfig --level 4 sendmail off 
    # chkconfig --level 5 sendmail off
    将2,345 level都关闭后,下次启动系统sendmail服务将不会自动启动。
3)ntsysv
   该命令将打开一个列表,选择sendmail然后取消表示选中的星号保存退出。下次启动系统时就不会启动sendmail

 

 

方法三: root邮件转发到自己的邮箱

在系统出现错误或有重要通知发送邮件给root的时候,让系统自动转送到我们通常使用的邮箱中,这样方便查阅相关报告和日志。
[root@sample ~]# vi /etc/aliases   ← 编辑aliases,添加如下行到文尾
root: yourname@yourserver.com ← 加入自己的邮箱地址
[root@sample ~]# newaliases ← 重建aliasesdb(也可以restart sendmail服务来使其生效)
/etc/aliases: 79 aliases, longest 19 bytes, 825 bytes total
[root@sample ~]# echo test | mail root ← 发送测试邮件给root


如果成功的话,会在刚刚填入的 yourname@yourserver.com 的邮箱中收到测试的邮件。

 

 

 

 

Linux下使用mail发送邮件

邮件常常是Linux下监控报警手段之一。Linux下的mail命令可以方便,快速的完成发送邮件。下面以CentOS为例

1、安装

[root@localhost ~]# mail
-bash: mail: command not found
[root@localhost ~]# yum install mailx 
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.btte.net
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
base                                                                                                                                                                                                                  | 3.7 kB     00:00     
extras                                                                                                                                                                                                                | 3.4 kB     00:00     
extras/primary_db                                                                                                                                                                                                     |  29 kB     00:00     
updates                                                                                                                                                                                                               | 3.4 kB     00:00     
updates/primary_db                                                                                                                                                                                                    | 4.7 MB     00:04     
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mailx.x86_64 0:12.4-8.el6_6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================================================================
 Package                                                Arch                                                    Version                                                          Repository                                             Size
=============================================================================================================================================================================================================================================
Installing:
 mailx                                                  x86_64                                                  12.4-8.el6_6                                                     base                                                  235 k

Transaction Summary
=============================================================================================================================================================================================================================================
Install       1 Package(s)

Total download size: 235 k
Installed size: 452 k
Is this ok [y/N]: y
Downloading Packages:
mailx-12.4-8.el6_6.x86_64.rpm                                                                                                                                                                                         | 235 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : mailx-12.4-8.el6_6.x86_64                                                                                                                                                                                                 1/1 
  Verifying  : mailx-12.4-8.el6_6.x86_64                                                                                                                                                                                                 1/1 

Installed:
  mailx.x86_64 0:12.4-8.el6_6                                                                                                                                                                                                                

Complete!
[root@localhost ~]# mail
No mail for root
mail命令安装

 

2、配置
vi /etc/mail.rc   在文件尾加上如下配置  (注:因为163的设置相对简单些,以163邮箱为例,QQ邮箱等,其他邮箱因为安全等因素,需要设置的比较多,具体的可以搞下,本文不作重点。QQ邮箱也是可以的)

set from=xd502djj@163.com
set smtp=smtp.163.com
set smtp-auth-user=xd502djj@163.com
set smtp-auth-password=邮箱密码 或者 授权码
set smtp-auth=login


3、发送邮件测试

[root@vm8028 ~]# echo "Content" | mail -s "Title" dongjunjie@cmcm.com
[root@vm8028 ~]#  
[root@vm8028 ~]# 

 

4、其他

mail命令 可以使用查看具体更多的使用方法
 mail --h 
  举例:
a、无邮件正文  
      # mail -s "Title"  280094177@qq.com
b、有邮件正文
      #   mail -s "Tile"  280094177@qq.com </root/sms_out.txt 
 或者 cat /root/sms_out.txt  | mail -s "Tile"  280094177@qq.com
     或者 echo “”Content“  |   mail -s "Tile"  280094177@qq.com 
c 、带附件的邮件
   mail -s "Tile"  280094177@qq.com  -a  /root/sms_out.txt  

 

 

 

 

 

 

 

posted @ 2019-07-06 10:17  冥想心灵  阅读(246)  评论(0编辑  收藏  举报