Custom sudoers files for different platforms.
Currently, only a generic Linux version is provided (known to work on Debian/Ubuntu). A version for macOS is planned).
This repository contains sudoers files for different platforms. A sudoers file defines the behaviour of the sudo command.
Every system has a default sudoers file named /etc/sudoers. This file contains all the default sudoers settings and it has an include directive for all files in the /etc/sudoers.d directory. That means, all the sudoers files in the /etc/sudoers.d directory will be applied along with the main /etc/sudoers file.
The sudoers files in this repository are intended to be put in the /etc/sudoers.d directory. They take into account the default /etc/sudoers file on the corresponding system and are designed to be applied on top of these default settings.
Note that settings in
/etc/sudoers.dfiles override the settings in the/etc/sudoersfile.
The sudoers files in this repository largely implement the following common behaviour:
- Password-less
sudofor the default user- This means that this user can use
sudowithout entering a password
- This means that this user can use
- The
PATHvariable is passed to thesudoenvironment- This means that commands executed with
sudohave the samePATHas the default user
- This means that commands executed with
- The
HOMEvariable is passed to thesudoenvironment- This means that commands executed with
sudouse config files from the default user's home directory, such as~/.vimrc,~/.bashrc, etc.
- This means that commands executed with
- A few additional common variables are passed to the
sudoenvironment- Including:
EDITOR,http_proxy,https_proxy,no_proxy
- Including:
To install the sudoers file, run the following as the default user:
curl https://raw.githubusercontent.com/weibeld/sudoers/main/linux | DATE=$(date -Iseconds) envsubst | sudo tee /etc/sudoers.d/config >/dev/nullThe above saves the sudoers file for Linux as /etc/sudoers.d/config on the local machine.
Note the following about this command:
- The user for which password-less
sudois enabled is the user who executes the above command. - The
envsubstcommand is needed to replace the$USERplaceholder in the downloaded file with the value of theUSERenvironment variable on the local system.envsubstis installed by default on most systems, if it isn't, it can be installed through thegettextpackage.
- The
DATEvariable assignment is optional and causes the current date to be included as a comment in created sudoers file.
One of the most crucial settings in a sudoers file is env_keep += HOME which causes the HOME variable (as well as the ~ character) in the sudo environment to be set to the invoking user's HOME variable instead of the root user's HOME variable.
The consequence of this is that commands executed in the sudo environment will use configuration files from the invoking user's home directory instead of the root user's home directory. For example, sudo vim will use the invoking user's .vimrc file and .vim directory, with all the familiar configurations, rather than the root user's version of these files (which are likely even non-existent).
This has also effects when an interactive root shell is started with sudo -s (not sudo -i as explained below). In this case, the started shell uses the invoking user's .bashrc file, including all the customisations, shell alias, shell functions, environment variables, etc., rather than the root user's .bashrc file.
On macOS,
env_keep += HOMEis even included in the default/etc/sudoersfile.
There are various ways to use sudo, the most important ones are listed below.
- This runs a single command in a new environment that's defined by the settings int the
sudoersfile. - It does not source any
.bashrc,.bash_profileor any other config files. CMDmust be an executable and not a shell alias, a shell function, or a shell builtin (command not founderror otherwise).
There's a trick to create the illusion that
sudocan actually execute Bash aliases by defining the following:alias sudo='sudo 'The space after the alias value causes Bash to resolve the first word after the alias value as an alias as well (see Bash docs). This means that if
myaliasis an alias in the current user's environment, then the following invocation succeeds:sudo myaliasHowever, it's Bash doing the replacement before invoking
sudoandsudoactually never sees themyaliasalias, only the substituted value. As mentioned, thesudoenvironment itself doesn't have any access to shell aliases, shell functions, or shell builtins.
- This starts an interactive non-login shell and sources
$HOME/.bashrc. That means, the environment consists of thesudoenvironment as defined by the settings in thesudoersfile plus anything (including shell aliases and functions) defined in the$HOME/.bashrcfile. The interactive shell also has access to shell builtins like a normal shell.- Which
.bashrcfile is sourced depends on the value of theHOMEvariable which can be influenced by theenv_keepsetting. For example, settingenv_keep += HOMEsets theHOMEvariable to the invoking user's home directory (instead of the root user's home directory) which consequently causes the invoking user's.bashrcfile to be sourced.
- Which
- When the interactive shell starts, it stays in the same directory from which
sudo -swas invoked.
- This starts an interactive login shell and sources the entire chain of config files starting from
/etc/profiledown to the root user's.profilefile. - Unlike the other two options, it does overwrite some of the settings in the
sudoersfile:- The
HOMEenvironment variable is always set to the root user's home directory, no matter whether it's added toenv_keepin thesudoersfile. - The
env_resetsetting in thesudoersfile is always enforced to be true, no matter what its actual value is in thesudoersfile.
- The
- Due to the forced setting of
HOME, it's always the root user's shell config that is sourced, and since a login shell is started, the sourced config file is.profile(corresponding to the.bash_profilefile of non-root users). - When the interactive shell starts, the current working directory is changed to the user's home directory.
- For simple one-off commands, use
sudo CMD. - If a shell alias, shell function, or shell builtin needs to be executed, or if access to environment variables or other settings defined in a
.bashrcfile is needed, start an interactive shell withsudo -s. - There should never really be a requirement to use
sudo -i.