Advertisement
Guest User

RHEL7 Postfix config script

a guest
May 31st, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.72 KB | None | 0 0
  1. #/!bin/bash
  2. ##
  3. ## Install and configure postfix for sending mail
  4. ## 2016-05-01 - Mark Hillis (IBM)
  5. ##
  6.  
  7. # Check redhat version
  8. if grep -q 7. /etc/redhat-release ;
  9. then  
  10.   echo -ne "Release supported: " && cat /etc/redhat-release
  11. else
  12.   echo "RHEL Release not supported"
  13.   exit 1
  14. fi
  15.  
  16. # Check if being run as root user.
  17. if [ "$EUID" -ne 0 ]
  18.   then echo "Please run this script as root user."
  19.   exit 1
  20. fi
  21.  
  22. echo "###############################################################"
  23. echo ""
  24. echo " This script will install and configure postfix on this server."
  25. echo ""
  26. echo " This script assumes run subscription was previously setup."
  27. echo ""
  28. echo "##############################################################"
  29. echo -n "Please press [enter] to continue or [crtl]^c to exit.."
  30. read
  31.  
  32. # Install Postfix from RHEL repository
  33. yum install -y postfix 1>&2
  34.  
  35.   # Check for command return code
  36.   if [ $? -eq 0 ]
  37.   then
  38.     echo "*** postfix install succeeded."
  39.   else
  40.     echo "*** postfix install failed."
  41.   # exit 1
  42.   fi
  43.  
  44. # Allow smtp service through firewall
  45. firewall-cmd --permanent --add-service=smtp >&2
  46.  
  47.   # Check for command return code
  48.   if [ $? -eq 0 ]
  49.   then
  50.     echo "*** Firewall rules updated sucessfully"
  51.   else
  52.     echo "*** Firewall rules failed."
  53.   # exit 1
  54.   fi
  55.  
  56. # Reload firewall with new rules
  57. firewall-cmd --reload >&2
  58.  
  59.   # Check for command return code
  60.   if [ $? -eq 0 ]
  61.   then
  62.     echo "*** Firewall reloaded sucessfully"
  63.   else
  64.     echo "*** Firewall reload failed."
  65.   # exit 1
  66.   fi
  67.  
  68. # Add post fix to startup
  69. systemctl enable postfix 1>&2
  70.  
  71.   # Check for command return code
  72.   if [ $? -eq 0 ]
  73.   then
  74.     echo "*** postfix enabled successfully"
  75.   else
  76.     echo "*** postfix unable to add to startup"
  77.   # exit 1
  78.   fi
  79.  
  80. # Start postfix daemon
  81. systemctl restart postfix 1>&2
  82.  
  83. # Check for command return code
  84.   if [ $? -eq 0 ]
  85.   then
  86.     echo "*** postfix started successfully"
  87.   else
  88.     echo "*** postfix failed to start"
  89.   # exit 1
  90.   fi
  91.  
  92. # Backup default config
  93.   if [ -f /etc/postfix/main.cf ];
  94.   then
  95.       mv /etc/postfix/main.cf /etc/postfix/main.cf.old.$(date +"%Y%m%d%H%M") 1>&2
  96.   fi
  97.  
  98. # Check for command return code
  99.   if [ $? -eq 0 ]
  100.   then
  101.     echo "*** Config backup successful"
  102.   else
  103.     echo "*** Unable to backup config files"
  104.   # exit 1
  105.   fi
  106.  
  107. ## Create new postfix config file: /etc/postfix/main.cf
  108. echo "*** Staging settings for postfix config file.."
  109.  
  110. # Config Postfix Seting myhostname
  111. myhostname="$HOSTNAME"
  112. read -e -i"$myhostname" -p "enter hostname: " myhostname
  113. myhostname="${input:-$myhostname}"
  114. #myhostname = servername
  115.  
  116. #Config Postfix seting mydomain
  117. mydomain="ibm.com"
  118. read -e -i "$mydomain" -p "enter domain name: " mydomain
  119. mydomain="${input:-$mydomain}"
  120. #mydomain = example.com
  121.  
  122. # Config Postfix setting myorigin
  123. myorigin="ibm.com"
  124. read -e -i "$myorigin" -p "enter origin domain: " myorigin
  125. myorigin="${input:-$myorigin}"
  126. #myorigin = $mydomain
  127.  
  128. # Config Postfix setting inet_interfaces
  129. eth="all"
  130. read -e -i "$eth" -p "enter allowed interfaces: " eth
  131. eth="${input:-$eth}"
  132. #inet_interfaces = all
  133.  
  134. # Config Postfix setting mydestinaion
  135. mydestination="$myhostname, localhost.$mydomain, localhost, $mydomain"
  136.  
  137. # Config Postfix setting my networks
  138. mynetworks="10.0.0.1/24, 127.0.0.0/8"
  139. read -e -i "$mynetworks" -p "enter allowed Networks : " mynetworks
  140. mynetworks="${input:-$mynetworks}"
  141. #mynetworks = 192.168.1.0/24, 127.0.0.0/8
  142.  
  143. echo " *** Please review settings.."
  144. echo ""
  145. echo "myhostname = $myhostname                 
  146. mydomain = $mydomain
  147. myorigin = $myorigin
  148. mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
  149. mynetworks = $mynetworks"
  150. echo ""
  151. read -p "*** Press [Enter] to continue.."
  152.  
  153. # Write Postfix config file to /etc/postfix/main.cf
  154. echo "queue_directory = /var/spool/postfix
  155. command_directory = /usr/sbin
  156. daemon_directory = /usr/libexec/postfix
  157. data_directory = /var/lib/postfix
  158. mail_owner = postfix
  159. myhostname = $myhostname.$mydomain
  160. mydomain = $mydomain
  161. myorigin = $myorigin
  162. mynetworks = $mynetworks
  163. inet_interfaces = $eth
  164. inet_protocols = all
  165. mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
  166. unknown_local_recipient_reject_code = 550
  167. alias_maps = hash:/etc/aliases
  168. alias_database = hash:/etc/aliases
  169. debug_peer_level = 2
  170. debugger_command =
  171.      PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
  172.      ddd $daemon_directory/$process_name $process_id & sleep 5
  173. sendmail_path = /usr/sbin/sendmail.postfix
  174. newaliases_path = /usr/bin/newaliases.postfix
  175. mailq_path = /usr/bin/mailq.postfix
  176. setgid_group = postdrop
  177. html_directory = no
  178. sample_directory = /usr/share/doc/postfix-2.10.1/samples
  179. readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES
  180. " > /etc/postfix/main.cf
  181.  
  182. # Check postfix configs
  183. postfix check 1>&2
  184.  
  185. # Check for command return code
  186.   if [ $? -eq 0 ]
  187.   then
  188.     echo "*** Postfix check passed"
  189.   else
  190.     echo "*** Postfix check failed"
  191.   # exit 1
  192.   fi
  193.  
  194. # check postfix non-default config
  195. postconf -n 1>&2
  196.  
  197.   # Check for command return code
  198.   if [ $? -eq 0 ]
  199.   then
  200.     echo "*** Postfix custom check passed."
  201.   else
  202.     echo "*** Postfix custom check failed."
  203.   # exit 1
  204.   fi
  205.  
  206. # Allow postfix for SE linux
  207. setsebool -P allow_postfix_local_write_mail_spool on 1>&2
  208.  
  209.   # Check for command return code
  210.   if [ $? -eq 0 ]
  211.   then
  212.     echo "*** Enable Postfix for SE Linux Succeeded."
  213.   else
  214.     echo "*** Enable Postfix for SE Linux failed."
  215.   # exit 1
  216.   fi
  217.  
  218. # Restart postfix daemon
  219. systemctl restart postfix 1>&2
  220.  
  221. # Check for command return code
  222.   if [ $? -eq 0 ]
  223.   then
  224.     echo "*** postfix restarted successfully"
  225.   else
  226.     echo "*** postfix failed to restart"
  227.   # exit 1
  228.   fi
  229.  
  230. echo "*** Post installation & configuration complete"
  231. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement