手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux下邮件服务Postfix的搭建

[日期:2012-07-09] 来源:Linux社区  作者:leejia [字体:大 中 小]
三、安装配置postfix
 
# groupadd -g 2525 postfix
# useradd -g postfix -u 2525 -s /sbin/nologin -M postfix
# groupadd -g 2526 postdrop
# useradd -g postdrop -u 2526 -s /sbin/nologin -M postdrop
 
# tar zxvf postfix-2.9.1.tar.gz
# cd postfix-2.9.1
# make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl  -DUSE_TLS ' 'AUXLIBS=-L/usr/lib/mysql -lmysqlclient -lz -lm -L/usr/lib/sasl2 -lsasl2  -lssl -lcrypto'
# make
# make install
 
按照以下的提示输入相关的路径([]号中的是缺省值,”]”后的是输入值,省略的表示采用默认值)
 
  install_root: [/]
  tempdir: [/root/postfix-2.9.1] /tmp/postfix
  config_directory: [/etc/postfix]
    command_directory: [/usr/sbin]
  daemon_directory: [/usr/libexec/postfix]
    data_directory: [/var/lib/postfix]
    html_directory: [no]/var/www/html/postfix
    mail_owner: [postfix]
    mailq_path: [/usr/bin/mailq]
    manpage_directory: [/usr/local/man]
    newaliases_path: [/usr/bin/newaliases]
  queue_directory: [/var/spool/postfix]
    readme_directory: [no]
  sendmail_path: [/usr/sbin/sendmail]
    setgid_group: [postdrop]
 
生成别名二进制文件:
#  newaliases
 
2.进行一些基本配置,测试启动postfix并进行发信
# vim /etc/postfix/main.cf
修改以下几项为您需要的配置
myhostname = mail.linuxidc.com
mydomain = linuxidc.com
myorigin = linuxidc.com
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 172.16.0.0/16, 127.0.0.0/8, 192.168.0.0/24
 
说明:
myorigin 参数用来指明发件人所在的域名,即做发件地址伪装;
mydestination 参数指定postfix接收邮件时收件人的域名,即您的postfix系统要接收到哪个域名的邮件;
myhostname 参数指定运行postfix邮件系统的主机的主机名,默认情况下,其值被设定为本地机器名;
mydomain 参数指定您的域名,默认情况下,postfix将myhostname的第一部分删除而作为mydomain的值;
mynetworks 参数指定你所在的网络的网络地址,postfix系统根据其值来区别用户是远程的还是本地的,如果是本地网络用户则允许其访问;
inet_interfaces 参数指定postfix系统监听的网络接口;
 
注意:
1、在postfix的配置文件中,参数行和注释行是不能处在同一行中的;
2、任何一个参数的值都不需要加引号,否则,引号将会被当作参数值的一部分来使用;
3、每修改参数及其值后执行 postfix reload 即可令其生效;但若修改了inet_interfaces,则需重新启动postfix;
4、如果一个参数的值有多个,可以将它们放在不同的行中,只需要在其后的每个行前多置一个空格即可;postfix会把第一个字符为空格或tab的文本行视为上一行的延续;
 
 
四、为postfix提供SysV服务脚本/etc/rc.d/init.d/postfix,内容如下(#END 之前):
#!/bin/bash
#
# postfix      Postfix Mail Transfer Agent
#
# chkconfig: 2345 80 30
# description: Postfix is a Mail Transport Agent, which is the program \
#              that moves mail from one machine to another.
# processname: master
# pidfile: /var/spool/postfix/pid/master.pid
# config: /etc/postfix/main.cf
# config: /etc/postfix/master.cf
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ $NETWORKING = "no" ] && exit 3
 
[ -x /usr/sbin/postfix ] || exit 4
[ -d /etc/postfix ] || exit 5
[ -d /var/spool/postfix ] || exit 6
 
RETVAL=0
prog="postfix"
 
start() {
 # Start daemons.
 echo -n $"Starting postfix: "
        /usr/bin/newaliases >/dev/null 2>&1
 /usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure $"$prog start"
 RETVAL=$?
 [ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix
        echo
 return $RETVAL
}
 
stop() {
  # Stop daemons.
 echo -n $"Shutting down postfix: "
 /usr/sbin/postfix stop 2>/dev/null 1>&2 && success || failure $"$prog stop"
 RETVAL=$?
 [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/postfix
 echo
 return $RETVAL
}
 
reload() {
 echo -n $"Reloading postfix: "
 /usr/sbin/postfix reload 2>/dev/null 1>&2 && success || failure $"$prog reload"
 RETVAL=$?
 echo
 return $RETVAL
}
 
abort() {
 /usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure $"$prog abort"
 return $?
}
 
flush() {
 /usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure $"$prog flush"
 return $?
}
 
check() {
 /usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure $"$prog check"
 return $?
}
 
restart() {
 stop
 start
}
 
# See how we were called.
case "$1" in
  start)
 start
 ;;
  stop)
 stop
 ;;
  restart)
 stop
 start
 ;;
  reload)
 reload
 ;;
  abort)
 abort
 ;;
  flush)
 flush
 ;;
  check)
 check
 ;;
  status)
   status master
 ;;
  condrestart)
 [ -f /var/lock/subsys/postfix ] && restart || :
 ;;
  *)
 echo $"Usage: $0 {start|stop|restart|reload|abort|flush|check|status|condrestart}"
 exit 1
esac
 
exit $?
 
# END
 
为此脚本赋予执行权限:
# chmod +x /etc/rc.d/init.d/postfix
 
将postfix服务添加至服务列表:
# chkconfig --add postfix
 
设置其开机自动启动:
# chkconfig postfix on
 
使用此脚本重新启动服务,以测试其能否正常执行:
# service postfix restart
 
此时可使用本地用户测试邮件收发了。
# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 mail.linuxidc.com ESMTP Postfix
ehlo mail.linuxidc.com
250-mail.linuxidc.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from:root@linuxidc.com
250 2.1.0 Ok
rcpt to:lee@linuxidc.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
subject:Mail test!
Mail test!!!
.
250 2.0.0 Ok: queued as AB94A1A561
quit
221 2.0.0 Bye
切换到RedHat用户进行收信:
# su - lee
[lee@mail ~]$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/lee": 1 message 1 new
>N  1 root@linuxidc.com       Sun Mar 18 12:08  14/476   "mail test"
&
 
退出用户lee:
[lee@mail ~]$ exit
 
五、为postfix服务开启用户别名支持:
主要作用:使一个用户的邮件让另一个用户代理接收
 
1、在配置文件开启基于hash的别名文件支持
#vim /etc/postfix/main.cf
找到如下指令,而后启用它(即移除前面的#号):
#alias_maps = hash:/etc/aliases
 
2、在/etc/aliases文件中定义新的别名项,其格式通常为以冒号隔开的两个字段,前一个字段为初始目标邮件地址,后一个字段为实际发往的地址,如把发往用户lee的邮件都让root用户接受,则:
#vim /etc/aliases
在文件末尾添加:
lee: root
 
3、将/etc/aliases转换为hash格式:
# postalias  /etc/aliases
# ls /etc |grep aliases
 
4、让postfix重新启动,即可进行测试;
#service postfix restart
会出现aliases.db文件:
aliases
aliases.dblinux
相关资讯       Postfix 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款