Skip to content

Instantly share code, notes, and snippets.

@luginbash
Last active December 21, 2015 14:02
Show Gist options
  • Save luginbash/225ca272267764854253 to your computer and use it in GitHub Desktop.
Save luginbash/225ca272267764854253 to your computer and use it in GitHub Desktop.
System Initialization Script, Personalized
#!/bin/bash
# libSystem - this script does nothing all by itself.
function addUfw {
aptitude -y install ufw
ufw logging on
ufw default deny
ufw allow ssh
ufw enable
rm /etc/rsyslog.d/20-ufw.conf
echo ":msg,contains,\"[UFW \" /var/log/ufw.log" >> /etc/rsyslog.d/50-default.conf
echo "\& stop" >> /etc/rsyslog.d/50-default.conf
touch /tmp/restart-rsyslog restart
}
function addUserWithSudo {
# Installs sudo if needed and creates a user in the sudo group.
#
# $1 - Required - username
# $2 - Required - password
USERNAME="$1"
USERPASS="$2"
if [ ! -n "$USERNAME" ] || [ ! -n "$USERPASS" ]; then
echo "No new username and/or password entered"
return 1;
fi
aptitude -y install sudo
adduser $USERNAME --disabled-password --gecos ""
echo "$USERNAME:$USERPASS" | chpasswd
usermod -aG sudo $USERNAME
echo "%sudo ALL = NOPASSWD: ALL" >> /etc/sudoers
}
function addPubkey {
# Adds the users public key to authorized_keys for the specified user. Make sure you wrap your input variables in double quotes, or the key may not load properly.
#
#
# $1 - Required - username
# $2 - Required - public key
USERNAME="$1"
USERPUBKEY="$2"
if [ ! -n "$USERNAME" ] || [ ! -n "$USERPUBKEY" ]; then
echo "Must provide a username and the location of a pubkey"
return 1;
fi
if [ "$USERNAME" == "root" ]; then
mkdir /root/.ssh
echo "$USERPUBKEY" >> /root/.ssh/authorized_keys
return 1;
fi
mkdir -p /home/$USERNAME/.ssh
echo "$USERPUBKEY" >> /home/$USERNAME/.ssh/authorized_keys
chown -R "$USERNAME":"$USERNAME" /home/$USERNAME/.ssh
chmod 700 /home/$USERNAME/.ssh
chmod 600 /home/$USERNAME/.ssh/*
if [ "$USERNAME" == "qzhou" ]; then
wget https://gist.githubusercontent.com/methou/13492f4e8ee99a407ea7/raw/240e970020c82669a6ced240018b0e169d7669c9/authorized_keys
mkdir -p /home/$USERNAME/.ssh
cp authorized_keys /home/$USERNAME/.ssh/authorized_keys
rm -f authorized_keys
chown -R "$USERNAME":"$USERNAME" /home/$USERNAME/.ssh
chmod 700 /home/$USERNAME/.ssh
chmod 600 /home/$USERNAME/.ssh/*
fi
}
function disableRoot {
# Disables root SSH access.
sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
touch /tmp/restart-ssh
}
function addOptional {
# Installs the REAL vim, wget, less, and enables color root prompt and the "ll" list long alias
aptitude -y install wget vim less git
sed -i -e 's/^#PS1=/PS1=/' /root/.bashrc # enable the colorful root bash prompt
sed -i -e "s/^#alias ll='ls -l'/alias ll='ls -al'/" /root/.bashrc # enable ll list long alias <3
}
function restartServices {
# restarts services that have a file in /tmp/needs-restart/
for service in $(ls /tmp/restart-* | cut -d- -f2-10); do
/etc/init.d/$service restart
rm -f /tmp/restart-$service
done
}
function system_primary_ip {
# returns the primary IP assigned to eth0
echo $(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }')
}
function get_rdns {
# calls host on an IP address and returns its reverse dns
if [ ! -e /usr/bin/host ]; then
aptitude -y install dnsutils > /dev/null
fi
echo $(host $1 | awk '/pointer/ {print $5}' | sed 's/\.$//')
}
function get_rdns_primary_ip {
# returns the reverse dns of the primary IP assigned to this system
echo $(get_rdns $(system_primary_ip))
}
function system_set_hostname {
# $1 - The hostname to define
HOSTNAME="$1"
if [ ! -n "$HOSTNAME" ]; then
echo "Hostname undefined"
return 1;
fi
echo "$HOSTNAME" > /etc/hostname
hostname -F /etc/hostname
}
function system_add_host_entry {
# $1 - The IP address to set a hosts entry for
# $2 - The FQDN to set to the IP
IPADDR="$1"
FQDN="$2"
if [ -z "$IPADDR" -o -z "$FQDN" ]; then
echo "IP address and/or FQDN Undefined"
return 1;
fi
echo $IPADDR $FQDN >> /etc/hosts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment