Setting SMTP on linux server
Follow through the steps to setup simple smtp on linxu server
Pre-requisite:
Steps to setup email are:
- Install ssmtp
sudo apt-get install ssmtp - Edit conf file ( /etc/ssmtp/ssmtp.conf and /etc/ssmtp/revaliases)
sudo vim /etc/ssmtp/ssmtp.conf - djust and add as necessary the following parameters:
- root=username@gmail.com
- mailhub=smtp.gmail.com:587
- hostname=username@gmail.com
- UseSTARTTLS=YES
- AuthUser=username
- AuthPass=password
- FromLineOverride=yes
To send hostname instead of root[root@hostname.FQDN] - save: :qw
- To make the default (root) “from” field be the server name, edit the/etc/ssmtp/revaliases file:sudo vim /etc/ssmtp/revaliases
- add into it the desired translation which in our Gmail examples case will be
root:machine-name@some-domain.com:smtp.gmail.com - Test the configuration
- $ echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv your-email@some-domain.com
and then press 'ctrl + d' to begin sending email
Your /etc/ssmtp/ssmtp.conf file will look like this for gmail server:
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
##root=postmaster
root=chantuBhantu@gmail.com
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
##mailhub=mail
mailhub=smtp.gmail.com:587
AuthUser=chantuBhantu@gmail.com
AuthPass=superSecret
UseTLS=YES
UseSTARTTLS=YES
# Where will the mail seem to come from?
#rewriteDomain=
rewriteDomain=gmail.com
# The full hostname
##hostname=vagrant-ubuntu-precise-64
hostname=chantuBhantu@gmail.com
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
and /etc/ssmtp/reveraliases like this:
# sSMTP aliases
#
# Format: local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
root:chantuBhantu@gmail.com:smtp.gmail.com:587
Security
- The default file permissions of /etc/ssmtp/ssmtp.conf allow all the users to read it. This isn’t good because it contains our email password in plain-text. Use this to change the read permissions:
- sudo chmod 640 /etc/ssmtp/ssmtp.conf
Now only root can send email - With 640 settings only root can send email. To enable other users to send email, set the file’s group to mail and add those users to the mail group:
- sudo chown root:mail /etc/ssmtp/ssmtp.conf
- sudo gpasswd -a USERNAME mail
- Test the configuration by sending
- echo "Test" | ssmtp -vs "Subject 123" root
- If you want to change the name of the sender when mailing on behalf of a system user, you can do this by setting the user’s name:
- sudo chfn -f root.homeserver root
- sudo chfn -f myuser.homeserver myuser
Troubleshooting
- If you got the following error in your /var/log/mail.err log file, then your password probably has a special character (such as ?) which breaks the shell when sending the email
- The following error in your /var/log/mail.err log file usualy indicates that your user isn’t in the mail group. Make sure by signing in again to the terminal and runninggroups to see which groups you are a member of.
/var/log/mail.err unable to locate smtp.gmail.com - root@vagrant-ubuntu-precise-64:/home/vagrant/chef-client/chef-repo# knife bootstrap 192.168.50.74 --sudo -r 'role[time-app-v2-1]'
- Bootstrapping Chef on 192.168.50.74
- ERROR: Net::SSH::HostKeyMismatch: fingerprint 4e:08:19:60:8b:ff:6a:70:a6:36:80:37:35:e2:ff:88 does not match for "192.168.50.74"
To fix this error, remove that fingerprint from your~/.ssh/known_hosts
file and run the command again.
For security reasons, SSH stores the fingerprints of systems in a local cache the first time you connect. If that fingerprint changes (like if you re-provision a server using the same FQDN), SSH will throw this error
References
- http://linux.die.net/man/8/ssmtp
- http://linux.die.net/man/5/ssmtp.conf
- http://www.havetheknowhow.com/Configure-the-server/Install-ssmtp.html
- http://amirshk.com/blog/2012/10/01/forward-roots-mail-to-a-gmail-account-using-ssmtp/
- http://tech.jonathangardner.net/wiki/Sendmail_%2B_Gmail
No comments:
Post a Comment