Skip to content

Instantly share code, notes, and snippets.

@klaemo
Last active March 8, 2016 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save klaemo/8543810 to your computer and use it in GitHub Desktop.
Save klaemo/8543810 to your computer and use it in GitHub Desktop.
initial ubuntu server setup
printf '\e[1;34m%b\e[m' "\nUpdating the system...\n"
apt-get -y -qq update
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
NEW_USER=${1:-admin}
SSH_PORT=${2}
if [ ! -d /home/${NEW_USER} ]; then
printf '\e[1;34m%b\e[m' "\nCreating user '$NEW_USER'...\n"
adduser --gecos "" $NEW_USER
mkdir /home/$NEW_USER/.ssh
chmod 700 /home/$NEW_USER/.ssh
printf '\e[1;34m%b\e[m' "\nAdding ssh keys to $NEW_USER...\n"
cp /root/.ssh/authorized_keys /home/$NEW_USER/.ssh/
chmod 400 /home/$NEW_USER/.ssh/authorized_keys
chown $NEW_USER:$NEW_USER /home/$NEW_USER -R
printf '\e[1;34m%b\e[m' "\nGranting sudo rights to $NEW_USER\n"
adduser $NEW_USER sudo
# make ssh a little more secure
printf '\e[1;34m%b\e[m' "\nDisabling ssh root login...\n"
sed -e 's/^PermitRootLogin .*$/PermitRootLogin no/' -i /etc/ssh/sshd_config
sed -e 's/#\{0,1\}PasswordAuthentication .*$/PasswordAuthentication no/' -i /etc/ssh/sshd_config
if [ -n "${SSH_PORT}" ]; then
printf '\e[1;34m%b\e[m' "\nChanging SSH port to ${SSH_PORT}...\n"
sed -e "s/^Port .*$/Port ${SSH_PORT}/" -i /etc/ssh/sshd_config
fi
reload ssh
fi
printf '\e[1;34m%b\e[m' "\nSetting up basic firewall...\n"
apt-get -y -qq install ufw
if [ -n "${SSH_PORT}" ]; then
ufw delete allow ssh
ufw allow ${SSH_PORT}/tcp
else
ufw allow ssh
fi
echo "y" | ufw enable
ufw status
printf '\e[1;34m%b\e[m' "\nDone!\n"
@klaemo
Copy link
Author

klaemo commented Jan 27, 2014

./initial.sh $user $ssh_port

you can now (optionally) set a different port for sshd to listen on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment