广告

本站里的文章大部分经过自行整理与测试

2017年1月2日星期一

ubuntu - postfix - Host not found 解决方法

Jan  2 17:55:33 ubuntu1610 postfix/error[1525]: 
C5206120FB0: to=<user@gmail.com>, 
relay=none, delay=0.01, delays=0.01/0/0/0, dsn=4.4.3, 
status=deferred (delivery temporarily suspended: Host or domain name not found. Name service error for name=smtp.gmail.com type=A: Host not found, try again)

1. 关闭 dnsmasq 插件, 重启

$ su
$ gedit /etc/NetworkManager/NetworkManager.conf

#dns=dnsmasq

$ systemctl restart network-manager

2. 检查

# /etc/resolv.conf 是 symlink, 连接到 /run/resolvconf/resolv.conf
$ cat /etc/resolv.conf
$ cat /var/spool/postfix/etc/resolv.conf

3. 修改后, 重启

# 在 /run/resolvconf/resolv.conf 文件头上加上 nameserver
$ gedit /etc/resolvconf/resolv.conf.d/head

# 这是 router 的 IP
nameserver 192.168.0.1

$ systemctl restart postfix

4. 再检查 (以下2个文件应该同步了)

$ cat /etc/resolv.conf
$ cat /var/spool/postfix/etc/resolv.conf

5. 再 mail 看看, 然后检查 mail.log

$ echo "This is a test" | mail -s "Subject" recipient@yahoo.com
$ tail -f /var/log/mail.log

Linux - postfix - 用 GMAIL SMTP 送信

1. 安装

$ su

Ubuntu
$ apt install postfix mailutils mpack mutt

Fedora
$ dnf install postfix mailx mutt

CentOS
$ yum install postfix mailx mutt cyrus-sasl cyrus-sasl-plain

OpenSUSE
$ zypper install postfix mailx mutt cyrus-sasl

* mpack / mailx / mutt 可选择只安装一个

* Fedora / CentOS 建议先解决 SELINUX 问题
http://jasonmun.blogspot.my/2017/01/centos-fedora-postfix-selinux.html

2. 设置

$ gedit /etc/postfix/sasl_passwd

[smtp.gmail.com]:587 username@gmail.com:password

$ chmod 600 /etc/postfix/sasl_passwd

$ gedit /etc/postfix/main.cf

relayhost = [smtp.gmail.com]:587

smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

smtp_use_tls = yes

Ubuntu
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Fedora / CentOS
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt

OpenSUSE
smtp_tls_CAfile = /etc/ssl/ca-bundle.pem

$ gedit /etc/postfix/master.cf

tlsmgr    unix  -       -       n       1000?   1       tlsmgr

3. 设置 - GOOGLE

i) Settings - Forwarding and POP/IMAP - IMAP Access - Enable IMAP
https://mail.google.com/mail/u/0/#settings/fwdandpop

ii) 修改步骤 2 的 password
Sign-in & security - Signing in to Google - 2-Step Verification - ON
Sign-in & security - Signing in to Google - App passwords
这个用 App Password

Sign-in & security - Signing in to Google - 2-Step Verification - Off
Sign-in & security - Connected apps & sites - Allow less secure apps: ON
这个用 Gmail Password

https://myaccount.google.com/security?utm_source=OGB#signin

4. 重启

$ postmap /etc/postfix/sasl_passwd
$ systemctl restart postfix

5. 在防火壁增加 PORT 25

Ubuntu
$ ufw allow Postfix

CentOS / Fedora
$ firewall-cmd --add-service=smtp --permanent
或
$ firewall-cmd --add-port=25/tcp --permanent
$ firewall-cmd --reload

OpenSUSE
YaST - Firewall - Allowed Services - Service to Allow : SMTP with Postfix - Add

6. 测试

$ echo "This is a test" | mail -s "Subject" recipient@yahoo.com
$ echo "This is a test" | mail -s "Subject" recipient@yahoo.com -A /home/user/content.txt

# OpenSUSE 用 mailx 要将 -a 改为 -A

$ echo "This is a test" | mailx -s "Subject" recipient@yahoo.com -a /home/user/content.txt
$ echo "This is a test" | mutt -s "Subject" recipient@yahoo.com -a /home/user/content.txt

$ mpack -s "Test File" /home/user/content.txt recipient@yahoo.com


7. 其它

Ubuntu - postfix - Host not found 解决方法

Linux - ssmtp - 用 GMAIL SMTP 送信

1. 安装

$ su

Ubuntu
$ apt install ssmtp mailutils mpack mutt

Fedora
$ dnf install ssmtp mailx mutt

CentOS
$ yum install http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ yum install ssmtp mailx mutt

* mpack / mailx / mutt 可选择只安装一个

2. 设置 - SSMTP

$ gedit /etc/ssmtp/ssmtp.conf

root=user@gmail.com
mailhub=smtp.gmail.com:587
hostname=ubuntu1610
FromLineOverride=YES
AuthUser=user@gmail.com
AuthPass=password
UseSTARTTLS=YES
UseTLS=YES

CentOS
$ alternatives --config mta

选 /usr/sbin/sendmail.ssmtp

3. 设置 - GOOGLE

i) Settings - Forwarding and POP/IMAP - IMAP Access - Enable IMAP
https://mail.google.com/mail/u/0/#settings/fwdandpop

ii) 修改 AuthPass=password
Sign-in & security - Signing in to Google - 2-Step Verification - ON
Sign-in & security - Signing in to Google - App passwords
修改以上 AuthPass=App passwords

Sign-in & security - Signing in to Google - 2-Step Verification - Off
Sign-in & security - Connected apps & sites - Allow less secure apps: ON
修改以上 AuthPass=Gmail passwords

https://myaccount.google.com/security?utm_source=OGB#signin

4. 测试

$ echo "This is a test" | ssmtp recipient@yahoo.com

$ ssmtp recipient@yahoo.com < /home/user/content.txt

$ echo "This is a test" | mail -s "Subject" recipient@yahoo.com
$ echo "This is a test" | mail -s "Subject" recipient@yahoo.com -A /home/user/content.txt

$ echo "This is a test" | mailx -s "Subject" recipient@yahoo.com -a /home/user/content.txt
$ echo "This is a test" | mutt -s "Subject" recipient@yahoo.com -a /home/user/content.txt

$ mpack -s "Test File" /home/user/content.txt recipient@yahoo.com

5. 增强安全设置

$ groupadd ssmtp
$ chown :ssmtp /etc/ssmtp/ssmtp.conf
$ chown :ssmtp /usr/sbin/ssmtp
$ chmod 640 /etc/ssmtp/ssmtp.conf
$ chmod g+s /usr/sbin/ssmtp