Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/!bin/bash
- ##
- ## Install and configure postfix for sending mail
- ## 2016-05-01 - Mark Hillis (IBM)
- ##
- # Check redhat version
- if grep -q 7. /etc/redhat-release ;
- then
- echo -ne "Release supported: " && cat /etc/redhat-release
- else
- echo "RHEL Release not supported"
- exit 1
- fi
- # Check if being run as root user.
- if [ "$EUID" -ne 0 ]
- then echo "Please run this script as root user."
- exit 1
- fi
- echo "###############################################################"
- echo ""
- echo " This script will install and configure postfix on this server."
- echo ""
- echo " This script assumes run subscription was previously setup."
- echo ""
- echo "##############################################################"
- echo -n "Please press [enter] to continue or [crtl]^c to exit.."
- read
- # Install Postfix from RHEL repository
- yum install -y postfix 1>&2
- # Check for command return code
- if [ $? -eq 0 ]
- then
- echo "*** postfix install succeeded."
- else
- echo "*** postfix install failed."
- # exit 1
- fi
- # Allow smtp service through firewall
- firewall-cmd --permanent --add-service=smtp >&2
- # Check for command return code
- if [ $? -eq 0 ]
- then
- echo "*** Firewall rules updated sucessfully"
- else
- echo "*** Firewall rules failed."
- # exit 1
- fi
- # Reload firewall with new rules
- firewall-cmd --reload >&2
- # Check for command return code
- if [ $? -eq 0 ]
- then
- echo "*** Firewall reloaded sucessfully"
- else
- echo "*** Firewall reload failed."
- # exit 1
- fi
- # Add post fix to startup
- systemctl enable postfix 1>&2
- # Check for command return code
- if [ $? -eq 0 ]
- then
- echo "*** postfix enabled successfully"
- else
- echo "*** postfix unable to add to startup"
- # exit 1
- fi
- # Start postfix daemon
- systemctl restart postfix 1>&2
- # Check for command return code
- if [ $? -eq 0 ]
- then
- echo "*** postfix started successfully"
- else
- echo "*** postfix failed to start"
- # exit 1
- fi
- # Backup default config
- if [ -f /etc/postfix/main.cf ];
- then
- mv /etc/postfix/main.cf /etc/postfix/main.cf.old.$(date +"%Y%m%d%H%M") 1>&2
- fi
- # Check for command return code
- if [ $? -eq 0 ]
- then
- echo "*** Config backup successful"
- else
- echo "*** Unable to backup config files"
- # exit 1
- fi
- ## Create new postfix config file: /etc/postfix/main.cf
- echo "*** Staging settings for postfix config file.."
- # Config Postfix Seting myhostname
- myhostname="$HOSTNAME"
- read -e -i"$myhostname" -p "enter hostname: " myhostname
- myhostname="${input:-$myhostname}"
- #myhostname = servername
- #Config Postfix seting mydomain
- mydomain="ibm.com"
- read -e -i "$mydomain" -p "enter domain name: " mydomain
- mydomain="${input:-$mydomain}"
- #mydomain = example.com
- # Config Postfix setting myorigin
- myorigin="ibm.com"
- read -e -i "$myorigin" -p "enter origin domain: " myorigin
- myorigin="${input:-$myorigin}"
- #myorigin = $mydomain
- # Config Postfix setting inet_interfaces
- eth="all"
- read -e -i "$eth" -p "enter allowed interfaces: " eth
- eth="${input:-$eth}"
- #inet_interfaces = all
- # Config Postfix setting mydestinaion
- mydestination="$myhostname, localhost.$mydomain, localhost, $mydomain"
- # Config Postfix setting my networks
- mynetworks="10.0.0.1/24, 127.0.0.0/8"
- read -e -i "$mynetworks" -p "enter allowed Networks : " mynetworks
- mynetworks="${input:-$mynetworks}"
- #mynetworks = 192.168.1.0/24, 127.0.0.0/8
- echo " *** Please review settings.."
- echo ""
- echo "myhostname = $myhostname
- mydomain = $mydomain
- myorigin = $myorigin
- mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
- mynetworks = $mynetworks"
- echo ""
- read -p "*** Press [Enter] to continue.."
- # Write Postfix config file to /etc/postfix/main.cf
- echo "queue_directory = /var/spool/postfix
- command_directory = /usr/sbin
- daemon_directory = /usr/libexec/postfix
- data_directory = /var/lib/postfix
- mail_owner = postfix
- myhostname = $myhostname.$mydomain
- mydomain = $mydomain
- myorigin = $myorigin
- mynetworks = $mynetworks
- inet_interfaces = $eth
- inet_protocols = all
- mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
- unknown_local_recipient_reject_code = 550
- alias_maps = hash:/etc/aliases
- alias_database = hash:/etc/aliases
- debug_peer_level = 2
- debugger_command =
- PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
- ddd $daemon_directory/$process_name $process_id & sleep 5
- sendmail_path = /usr/sbin/sendmail.postfix
- newaliases_path = /usr/bin/newaliases.postfix
- mailq_path = /usr/bin/mailq.postfix
- setgid_group = postdrop
- html_directory = no
- sample_directory = /usr/share/doc/postfix-2.10.1/samples
- readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES
- " > /etc/postfix/main.cf
- # Check postfix configs
- postfix check 1>&2
- # Check for command return code
- if [ $? -eq 0 ]
- then
- echo "*** Postfix check passed"
- else
- echo "*** Postfix check failed"
- # exit 1
- fi
- # check postfix non-default config
- postconf -n 1>&2
- # Check for command return code
- if [ $? -eq 0 ]
- then
- echo "*** Postfix custom check passed."
- else
- echo "*** Postfix custom check failed."
- # exit 1
- fi
- # Allow postfix for SE linux
- setsebool -P allow_postfix_local_write_mail_spool on 1>&2
- # Check for command return code
- if [ $? -eq 0 ]
- then
- echo "*** Enable Postfix for SE Linux Succeeded."
- else
- echo "*** Enable Postfix for SE Linux failed."
- # exit 1
- fi
- # Restart postfix daemon
- systemctl restart postfix 1>&2
- # Check for command return code
- if [ $? -eq 0 ]
- then
- echo "*** postfix restarted successfully"
- else
- echo "*** postfix failed to restart"
- # exit 1
- fi
- echo "*** Post installation & configuration complete"
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement