Skip to content

Instantly share code, notes, and snippets.

@ITJesse
Last active June 15, 2017 17:26
Show Gist options
  • Save ITJesse/184e6444bb09484dc1d19851b3edd511 to your computer and use it in GitHub Desktop.
Save ITJesse/184e6444bb09484dc1d19851b3edd511 to your computer and use it in GitHub Desktop.
#!/bin/bash
USERNAME=$1
PASSWORD=$2
KERNEL_VERSION=$3
if [[ ! -n $USERNAME ]]; then
echo "Usage: ubuntu-vps-init username password [kernal version]";
exit 2
fi
if [[ ! -n $PASSWORD ]]; then
echo "Usage: ubuntu-vps-init username password [kernal version]";
exit 2
fi
if [[ ! -n $KERNEL_VERSION ]]; then
KERNEL_VERSION="4.4.0-47"
fi
apt-get update && apt-get install -y git zsh vim screen tmux wget curl vim expect
apt-get install -y linux-image-${KERNEL_VERSION}-generic \
linux-headers-${KERNEL_VERSION} linux-image-extra-${KERNEL_VERSION}-generic
apt-get purge -y linux-headers-generic* linux-image-generic*
dpkg -l linux-* | awk '/^ii/{print $2}' | egrep [0-9] | sort -t- -k3,4 --version-sort -r | grep -v -e `echo $KERNEL_VERSION | cut -f1,2 -d"-"` | xargs sudo apt-get -y purge
# sed -i "s/GRUB_DEFAULT=.*/GRUB_DEFAULT=\"Advanced options for Ubuntu>Ubuntu, with Linux ${KERNEL_VERSION}-generic\"/" /etc/default/grub
# update-grub
useradd -m -d /home/$USERNAME $USERNAME -s /bin/zsh
expect <<- DONE
spawn passwd $USERNAME
expect "Enter new UNIX password:"
send -- "$PASSWORD\r"
expect "Retype new UNIX password:"
send -- "$PASSWORD\r"
send -- "\r"
expect eof
DONE
echo -e "\n$USERNAME ALL=NOPASSWD: ALL\n" >> /etc/sudoers
mkdir /home/$USERNAME/.ssh
cp /root/.ssh/authorized_keys /home/$USERNAME/.ssh
chown -R $USERNAME:$USERNAME /home/$USERNAME/.ssh
sed -i 's/PermitRootLogin without-password/PermitRootLogin no/' /etc/ssh/sshd_config
cd /home/$USERNAME
su -c "git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh && \
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc && \
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc" $USERNAME
reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment