Shell script to setup a new VPS

The shell script below can be used to setup a new VPS running Ubuntu. It performs the following actions:

Create a new ssh username and password.

Disables root login.

Disables swap.

Installs Docker.

Creates a few alias commands for the user.

#!/bin/sh

###################################

####### EDIT THIS SECTION #########

USERNAME="<enter username>"

PASSWORD="<enter secure password or see below to auto-generate>"

###################################

PASSWD="head /dev/urandom | tr -dc A-Za-z0-9 | head -c 16 ; echo ''"

#Uncomment line below to set an auto-generated password. The password will be displayed at the end of this process.

PASSWORD=$(eval "$PASSWD")

echo "USERNAME: "$USERNAME

echo "PASSWORD: "$PASSWORD

adduser --disabled-password --gecos "" $USERNAME && echo "$USERNAME:$PASSWORD" | chpasswd && echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USERNAME && sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config && service sshd restart

swapoff -a

sed -i '/swapfile/d' /etc/fstab

apt update -y && apt upgrade -y && apt install -y vim sudo net-tools apt-transport-https ca-certificates software-properties-common docker.io && usermod -aG docker $USERNAME && systemctl start docker && systemctl enable docker

cat <<EOT >> /home/$USERNAME/.bashrc

export HISTSIZE=

export HISTFILESIZE=

bind '"\e[A":history-search-backward'

bind '"\e[B":history-search-forward'

alias di='docker images'

alias dc='docker ps -a'

alias dv='docker volume ls'

alias dl='docker container logs -f'

alias up='docker-compose up -d'

alias dn='docker-compose down'

alias lg='docker-compose logs -f'

alias gs='git status'

alias gp='git pull && gs'

alias glf='git log -p'

alias gl='git log --graph --pretty=oneline --abbrev-commit'

alias gd='git diff'

alias gcm='git checkout master && gs'

alias gcd='git checkout develop && gs'

alias gcp='git checkout production && gs'

alias gc.='git checkout . && gs'

alias vb='vim ~/.bashrc'

alias vh='sudo vim /etc/hosts'

alias sb='source ~/.bashrc'

alias f='free -m'

alias u='uptime'

alias n='sudo netstat -punta | grep LISTEN'

alias i='ifconfig'

alias j='jobs -l'

alias l='ls -falh'

alias d='df -h'

alias k='sudo kill -9'

alias s='du -sh * | sort -h'

alias t='ls -t -1'

alias h='history | grep'

alias p='ps -ef | grep'

alias kj='k $(j | awk {print $2})'

EOT

echo "USERNAME: "$USERNAME

echo "PASSWORD: "$PASSWORD

init 6

