Ubuntu 20.04 başlangıç konfigurasyonu

Kendi projelerimde yönettiğim uzak sunucularda dağıtım olarak Ubuntu 20.04 kullanıyorum genellikle. Yeni bir sunucu/VM kaldırdığımda hep tekrarladığım belli başlı işlemler olıyor. Bunları hızlandıran bir shell scripti kullanıyorum. Onu paylaşayım.

#!/usr/bin/env bash

set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT

script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
DOCKER_COMPOSE_VERSION=1.27.4

usage() {
  cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-d] -u user

This initializtion scrip creates a user with the given user name and installs docker and docker-compose if docker param passed.

Available options:

-h, --help      Print this help and exit
-v, --verbose   Print script debug info
-d, --docker    Enable docker & docker-compose installation
-u, --user      Name of the user to be created
EOF
  exit
}

cleanup() {
  trap - SIGINT SIGTERM ERR EXIT
  # script cleanup here
}

setup_colors() {
  if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
    NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
  else
    NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
  fi
}

msg() {
  echo >&2 -e "${1-}"
}

die() {
  local msg=$1
  local code=${2-1} # default exit status 1
  msg "$msg"
  exit "$code"
}

parse_params() {
  # default values of variables set from params
  docker=0

  while :; do
    case "${1-}" in
    -h | --help) usage ;;
    -v | --verbose) set -x ;;
    --no-color) NO_COLOR=1 ;;
    -d | --docker) docker=1 ;; # example flag
    -u | --user)  # example named parameter
      user="${2-}"
      shift
      ;;
    -?*) die "Unknown option: $1" ;;
    *) break ;;
    esac
    shift
  done

  args=("$@")

  # check required params and arguments
  [[ -z "${user-}" ]] && die "Missing required parameter: user"

  return 0
}

init(){
    msg "${GREEN}[+] APT Updates and upgrades: ${user}${NOFORMAT}"
    # update and upgrade
    apt update
    apt upgrade -y
}

create_user() {
    msg "${GREEN}[+] Creating new user: ${user}${NOFORMAT}"
    
    # create a new user and give sudo rights
    # generate a random password
    RANDOM_PASSWORD=$(date +%s | sha256sum | base64 | head -c 32 | cut -c 1-16)
    useradd -m --password $(perl -e 'print crypt($ARGV[0], "password")' ${RANDOM_PASSWORD}) --shell /bin/bash ${user}
    
    # add user to sudo group
    msg "${GREEN}[+] Adding ${user} to sudo group.${NOFORMAT}"
    usermod -aG sudo ${user}

    # sync .ssh folders with root user
    msg "${GREEN}[+] Copying root user's .ssh folder to new user's home${NOFORMAT}"
    rsync --archive --chown=${user}:${user} ~/.ssh /home/${user}
}

install_docker () {
    # install docker
    msg "${GREEN}[+] Installing docker${NOFORMAT}"
    apt install apt-transport-https ca-certificates curl software-properties-common -y
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
    apt update
    apt install docker-ce -y
    
    msg "${GREEN}[+] Adding ${user} to docker group.${NOFORMAT}"
    usermod -aG docker ${user}

    # install docker-compose
    msg "${GREEN}[+] Installing docker-compose${NOFORMAT}"
    curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
}

install_useful_packages() {
    # install useful packages
    msg "${GREEN}[+] Installing useful packages${NOFORMAT}"    
    apt install curl wget unzip htop net-tools git tree -y
}

parse_params "$@"
setup_colors



# Running script logic

# update and upgrade packages
init

# create sudo privileged user
create_user

# install docker and docker-compose if enabled
if [[ $docker -eq 1 ]]
then
    install_docker
else
    msg "${BLUE}[-] Skipping docker installation.${NOFORMAT}"
fi

install_useful_packages

msg "${GREEN}[+]Username: $user${NOFORMAT}"
msg "${RED}[!]Password: $RANDOM_PASSWORD${NOFORMAT}"
msg "${RED}[!] Keep it secret!${NOFORMAT}"

Gist linki:

Ubuntu 20.04 Server Initialization Script includes Docker & Docker Compose Installation
Ubuntu 20.04 Server Initialization Script includes Docker & Docker Compose Installation - init.sh
Show Comments