Last active
January 19, 2019 21:43
-
-
Save sampowers/5414218 to your computer and use it in GitHub Desktop.
Debian/Ubuntu host initialization script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
### | |
# Script to set root password and networking config on first boot. | |
# | |
# Make your modifications and turn this on by running: | |
# /etc/init.d/firstrun enable | |
### | |
## | |
# Configure this stuff with your own defaults, these get used in prompts | |
## | |
DEF_DOM="oddbox.org" | |
DEF_IP="10.42.0." | |
DEF_NM="255.255.0.0" | |
DEF_GW="10.42.0.1" | |
DEF_DNS1="198.237.137.20" | |
DEF_DNS2="198.237.137.21" | |
DEF_ADMIN="root@$DEF_DOM" | |
DEF_RELAY="mail.$DEF_DOM" | |
if [ "$1" == "enable" ]; then | |
update-rc.d firstrun start 38 S . | |
echo "Enabled firstrun script." | |
fi | |
if [ "$1" != "start" ] ; then | |
exit | |
fi | |
sleep 3 ; clear | |
echo | |
echo "Doing initial local config of passwords and networking." | |
echo "To re-use, read and edit /etc/init.d/firstrun, then run" | |
echo "/etc/init.d/firstrun enable" | |
echo | |
# only prompt in interactive mode | |
if ! grep -q "noninteractive" /proc/cmdline ; then | |
stty sane | |
echo "Please specify a root password:" | |
while ! passwd ; do : ; done | |
if [ -f /usr/bin/vncpasswd ]; then | |
echo "Please specify a VNC password:" | |
while ! vncpasswd /etc/vncpass ; do : ; done | |
fi | |
# hostname: | |
read -p "Hostname (w/o domain): " hn | |
echo "$hn" >/etc/hostname | |
hostname $hn | |
dpkg -s nullmailer &>/dev/null | |
if [ "$?" == "0" ]; then | |
read -ei "$DEF_ADMIN" -p "Admin Email Address: " adminaddr | |
read -ei "$DEF_RELAY" -p "Mail Server: " relayhost | |
fi | |
read -e -p "Press enter for DHCP or N for static IP configuration [Y/n] " usedhcp | |
case "$usedhcp" in | |
[Nn]) | |
read -ei "$DEF_DOM" -p "Domain name: " dn | |
read -ei "$DEF_IP" -p "Primary IP address: " ipaddr | |
read -ei "$DEF_NM" -p "Subnet Mask: " nm | |
read -ei "$DEF_GW" -p "Gateway: " gw | |
read -ei "$DEF_DNS1" -p "First DNS server: " dns1 | |
read -ei "$DEF_DNS2" -p "Second DNS server: " dns2 | |
echo "Adding /etc/hosts entry for $ipaddr $hn.$dn" | |
echo "$ipaddr $hn.$dn $hn" >> /etc/hosts | |
echo "Configuring /etc/resolv.conf with DNS servers $dns1 and $dns2 and search of $dn" | |
cat /dev/null > /etc/resolv.conf | |
echo "search $dn" >> /etc/resolv.conf | |
echo "nameserver $dns1" >> /etc/resolv.conf | |
echo "nameserver $dns2" >> /etc/resolv.conf | |
echo "Reconfiguring interface eth0 via ifupdown..." | |
of="/etc/network/interfaces" | |
cat /dev/null > $of | |
echo "auto lo" >> $of | |
echo "iface lo inet loopback" >> $of | |
echo "" >> $of | |
echo "auto eth0" >> $of | |
echo "iface eth0 inet static" >> $of | |
echo " address $ipaddr" >> $of | |
echo " netmask $nm" >> $of | |
echo " gateway $gw" >> $of | |
;; | |
[Yy]*) | |
echo "Setting eth0 as DHCP in /etc/network/interfaces..." | |
rm -f /var/lib/dhcp3/*leases | |
of="/etc/network/interfaces" | |
cat /dev/null > $of | |
echo "auto lo" >> $of | |
echo "iface lo inet loopback" >> $of | |
echo "" >> $of | |
echo "auto eth0" >> $of | |
echo "iface eth0 inet dhcp" >> $of | |
;; | |
esac | |
fi | |
hn=`hostname` | |
dn=$DEF_DOM | |
echo "Re-generating unique host keys for your SSH server..." | |
find /etc/ssh -name "ssh_host_*_key*" -exec rm -f {} \; | |
DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical dpkg-reconfigure openssh-server | |
dpkg -s puppet &>/dev/null && ( | |
echo "Re-generating unique keys for puppet configuration management..." | |
find /var/lib/puppet/ssl -type f -exec rm {} \; | |
DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical dpkg-reconfigure puppet | |
echo "Puppet setup complete. Contact puppetmaster admin to approve keys." | |
) | |
dpkg -s postfix &>/dev/null && ( | |
echo "Reconfiguring postfix mailer" | |
rm -f /etc/mailname /etc/postfix/main.cf | |
echo "postfix postfix/mailname string $hn.$dn" | debconf-set-selections | |
DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical dpkg-reconfigure postfix | |
) | |
dpkg -s nullmailer &>/dev/null && ( | |
echo "Reconfiguring nullmailer..." | |
rm -f /etc/mailname /etc/nullmailer/* | |
echo "nullmailer shared/mailname string $hn.$dn" | debconf-set-selections | |
echo "nullmailer nullmailer/adminaddr string $adminaddr" | debconf-set-selections | |
echo "nullmailer nullmailer/relayhost string $relayhost" | debconf-set-selections | |
DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical dpkg-reconfigure nullmailer | |
) | |
update-rc.d -f firstrun remove &>/dev/null | |
echo "done with first-run config." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my version of a script first written by @magurski