Last active
May 3, 2018 20:47
-
-
Save kmohrf/2d93d0ce2976ae7226369823c0b116c9 to your computer and use it in GitHub Desktop.
user aware root-shell
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
#!/bin/sh | |
set -eu | |
# we only want to run this script as root | |
if [ ! "$(id -u)" = 0 ]; then | |
echo "This script must be run as root" 1>&2 | |
exit 1 | |
fi | |
# use environment variable for user | |
# or fallback to logname | |
USER="${SOURCE_USER:-$(logname)}" | |
# get user home directory and preferred shell | |
HOME="$(getent passwd "$USER" | cut -d: -f6)" | |
SHELL="$(getent passwd "$USER" | cut -d: -f7)" | |
# in case the users preferred shell defaulted to this | |
# script we fallback to bash | |
if [ "$SHELL" = "$0" ]; then | |
SHELL=/bin/bash | |
fi | |
# if the user has a custom gitconfig we use it to define the | |
# git author and email | |
if [ -n "$HOME" ] && [ -e "$HOME/.gitconfig" ]; then | |
GIT_AUTHOR_NAME="$(git config -f "$HOME/.gitconfig" user.name)" | |
GIT_AUTHOR_EMAIL="$(git config -f "$HOME/.gitconfig" user.email)" | |
# in case no custom gitconfig has been provided we fallback to | |
# the username and a dynamically created mail address | |
else | |
GIT_AUTHOR_NAME="$USER" | |
GIT_AUTHOR_EMAIL="$USER@$(hostname -a)" | |
fi | |
# export the git authorship information into the environment | |
export GIT_AUTHOR_NAME | |
export GIT_AUTHOR_EMAIL | |
export USER=root | |
export HOME=/root | |
# finally execute the preferred shell with the original call args of this script | |
exec "$SHELL" "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment