Skip to content

Instantly share code, notes, and snippets.

@ggicci
Last active December 10, 2017 10:23
Show Gist options
  • Save ggicci/bf1742fcce1b91268404bb487d68f701 to your computer and use it in GitHub Desktop.
Save ggicci/bf1742fcce1b91268404bb487d68f701 to your computer and use it in GitHub Desktop.
Add a new sudoer user with password.
#!/usr/bin/env bash
set -o errexit
set -o pipefail
add_user() {
local username="$1"
local password="$2"
if id -u "${username}" &>/dev/null; then
return
fi
useradd -m -U -G wheel -s /bin/bash "${username}"
echo "${username}:${password}" | chpasswd
}
add_sudoers() {
local username="$1"
chmod u+w /etc/sudoers
if [[ "$(grep -P "${username}\tALL=\(ALL\)\s+?ALL" "/etc/sudoers" 2>/dev/null)" != "" ]]; then
return
fi
echo -e "${username}\tALL=(ALL)\tALL" >> "/etc/sudoers"
chmod u-w /etc/sudoers
}
main() {
local username="$1"
local password="$2"
add_user "${username}" "${password}"
add_sudoers "${username}"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment