Skip to content

Instantly share code, notes, and snippets.

@cabloo
Last active November 12, 2017 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cabloo/223528dc0c4d01caac95f7d872e38864 to your computer and use it in GitHub Desktop.
Save cabloo/223528dc0c4d01caac95f7d872e38864 to your computer and use it in GitHub Desktop.
Deploy a server with root account access via private key + sudo user only.

Run this on your local machine, it will run the scripts below for you:

wget "https://gist.githubusercontent.com/cabloo/223528dc0c4d01caac95f7d872e38864/raw/deploy-client.sh"
bash deploy-client.sh

You will need to follow the prompts and then enter the root password of the remote server twice.

#!/usr/bin/env bash
USER=$1
PASSWORD=$2
# Make sure the hostname is in /etc/hosts (for sudo).
if [ "$(grep 127.0.0.1 /etc/hosts | grep $(hostname))" = "" ]; then
echo "127.0.0.1 $(hostname)" >> /etc/hosts
fi
apt-get update --fix-missing
apt-get install -y sudo
USER_HOME=/home/${USER}
useradd ${USER}
chsh -s /bin/bash ${USER}
adduser ${USER} sudo
mkdir -p ${USER_HOME}/.ssh
mv ${HOME}/authorized_keys ${USER_HOME}/.ssh
chmod 0700 ${USER_HOME}/.ssh
chmod 0600 ${USER_HOME}/.ssh/authorized_keys
chown -R ${USER} ${USER_HOME}
echo "${USER}:${PASSWORD}" | chpasswd
# Disable password login.
sed -i 's/#*PasswordAuthentication [^\n]*/PasswordAuthentication no/g' /etc/ssh/sshd_config
service sshd restart
echo "${USER} is now a sudoer. The account password is: ${PASSWORD}"
USER=root
FILES=https://gist.githubusercontent.com/cabloo/223528dc0c4d01caac95f7d872e38864/raw
while [ "${HOST}" = "" ]; do
read -p "What is the IP or hostname of the Server? " HOST
done
while [ "${ACCOUNT_USER}" = "" ]; do
read -p "What user account should be created for login? [e.g. deploy] " ACCOUNT_USER
done
while [ "${ACCOUNT_PASS}" = "" ]; do
read -p "What password should be set for ${ACCOUNT_USER}? " ACCOUNT_PASS
done
ssh-keygen -f "${HOME}/.ssh/known_hosts" -R ${HOST}
ssh-keyscan ${HOST} >> "${HOME}/.ssh/known_hosts"
scp "${HOME}/.ssh/id_rsa.pub" ${USER}@${HOST}:authorized_keys
ssh ${USER}@${HOST} \
-- \
"apt-get update --fix-missing;apt-get install -y curl ntp; curl -L ${FILES}/deb-deploy-host.sh | bash -s -- ${ACCOUNT_USER} ${ACCOUNT_PASS}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment