Last active
December 10, 2017 10:23
-
-
Save ggicci/bf1742fcce1b91268404bb487d68f701 to your computer and use it in GitHub Desktop.
Add a new sudoer user with password.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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