Skip to content

Instantly share code, notes, and snippets.

@murar8
Last active August 21, 2020 00:21
Show Gist options
  • Select an option

  • Save murar8/9b7bd9dea377a9d183f76d5502663a15 to your computer and use it in GitHub Desktop.

Select an option

Save murar8/9b7bd9dea377a9d183f76d5502663a15 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Name: bootstrap-debian.sh
# Author: Lorenzo Murarotto <lnzmrr@gmail.com>
# Usage: ./bootstrap-debian.sh <USERNAME> <USER_UID> <USER_GID>
echo "Starting dependency installation procedure."
set -e
if [ ! -f /etc/debian_version ]; then
echo "Error: this script can only run in debian based distributions."
exit 1
fi
if [ "$(id -u)" -ne 0 ]; then
echo 'Error: this script must be run a root.'
exit 1
fi
USERNAME=${1:-"$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)"}
USERNAME=${USERNAME:-"ubuntu"}
USER_UID=${2:-1000}
USER_GID=${3:-1000}
DEBIAN_FRONTEND=noninteractive
# Suppress an apt-key warning about standard out not being a terminal. Use in this script is safe.
APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=true
echo "Installing dependencies."
apt-get update -y
# Upgrade packages
apt-get upgrade -y
# Install apt-utils to avoid debconf warning
apt-get install -y --no-install-recommends apt-utils 2> >( grep -v 'debconf: delaying package configuration, since apt-utils is not installed' >&2 )
# Install basic utilities
apt-get install -y --no-install-recommends \
locales \
sudo \
curl \
xz-utils \
ca-certificates \
zsh \
git
# Generate locale for correct unicode character handling.
# see https://stackoverflow.com/questions/28405902/how-to-set-the-locale-inside-a-debian-ubuntu-docker-container
echo "Generating locales."
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
# Handling of RLIMIT_CORE appears to be buggy in Linux containers. This workaround disables setrlimit warning.
# see https://github.com/sudo-project/sudo/issues/42
echo "Disabling setrlimit warning."
echo "Set disable_coredump false" >> /etc/sudo.conf
# Create a non-root user with sudo permissions.
# see https://code.visualstudio.com/docs/remote/containers-advanced#_adding-a-nonroot-user-to-your-dev-container
echo "Creating a non-root user with username=$USERNAME uid=$USER_UID gid=$USER_GID."
groupadd --gid $USER_GID $USERNAME
useradd -s $(which zsh) --uid $USER_UID --gid $USER_GID -m $USERNAME
echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME
chmod 0440 /etc/sudoers.d/$USERNAME
# Clean up
echo "Cleaning up apt."
apt-get autoremove -y
apt-get clean -y
rm -rf /var/lib/apt/lists/*
DEBIAN_FRONTEND=dialog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment