Welcome to kriss.run!


Back

lxc-setup.sh

#!/usr/bin/env bash

__lxc_setup() {

    local user="${1:-kriss}"

    if [ "$(id -u)" -ne 0 ]; then
        echo "This script must be run as root" >&2
        exit 1
    fi

    if id "$user" &>/dev/null; then
        echo "User $user already exists."
    else
        echo "Adding user $user..."
        adduser "$user"
    fi

    if groups "$user" | grep &>/dev/null "\bsudo\b"; then
        echo "User $user is already a member of the sudo group."
    else
        echo "Adding user $user to the sudo group..."
        usermod -aG sudo "$user"
    fi

    if [ -f "/etc/sudoers.d/$user" ]; then
        echo "File /etc/sudoers.d/$user already exists."
    else
        echo "Creating /etc/sudoers.d/$user file..."
        echo "$user ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/$user
    fi


}

__lxc_setup "$@"