tech:notes_sudo_et_sudoers
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| tech:notes_sudo_et_sudoers [2026/04/10 09:14] – Jean-Baptiste | tech:notes_sudo_et_sudoers [2026/06/29 14:23] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | Voir : | ||
| + | * [[Pb sudo - Lenteur sudo]] | ||
| + | * `man sudo_root` | ||
| + | |||
| + | Voir aussi : | ||
| + | * OpenDoas | ||
| + | * runuser / setpriv (util-linux) | ||
| + | * [sudoedit](http:// | ||
| + | * sudoreplay | ||
| + | * [Linux capabilities : se passer des commandes su et sudo](https:// | ||
| + | * https:// | ||
| + | * sux (su X11) | ||
| + | * sudoers [[audit|Audit - list all users allow root privileges]] | ||
| + | * [[Notes sécurité PAM|PAM]] | ||
| + | * [[Ansible sudo su become_method]] | ||
| + | * [[Notes userhelper - usermode]] | ||
| + | * [gosu, setpriv, su-exec](https:// | ||
| + | * userhelper | ||
| + | * `systemd-run -t bash` ou `systemd-run --shell` | ||
| + | * Pourquoi ne pas utiliser sudo dans un contener : https:// | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | |||
| + | # Notes sudo et sudoers | ||
| + | |||
| + | **sudo** does `fork`+`exec` instead of just `exec` | ||
| + | |||
| + | ~~~bash | ||
| + | visudo | ||
| + | ~~~ | ||
| + | ~~~ | ||
| + | jean ALL=(test) NOPASSWD: ALL | ||
| + | ~~~ | ||
| + | |||
| + | Utilisation | ||
| + | ~~~bash | ||
| + | sudo -u test -s /bin/bash | ||
| + | echo 'ls / | ||
| + | ~~~ | ||
| + | |||
| + | Test sudoers | ||
| + | ~~~bash | ||
| + | sudo -l | ||
| + | sudo -U username -l | ||
| + | sudo -U username -ll | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | env_keep : Check environment variables sudo preserved : | ||
| + | ~~~bash | ||
| + | sudo sudo -V | ||
| + | ~~~ | ||
| + | |||
| + | Restricting a sudo command to specific arguments | ||
| + | ~~~bash | ||
| + | user ALL=(root) NOPASSWD: / | ||
| + | ~~~ | ||
| + | |||
| + | Prohibiting Command Arguments with sudo | ||
| + | Follow the program name with the single argument “” in / | ||
| + | ~~~bash | ||
| + | smith ALL = (root) / | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Exemple de Sudoers | ||
| + | |||
| + | Accès root sans mdp pour un utilisateur | ||
| + | |||
| + | ~~~bash | ||
| + | # export EDITOR=vim | ||
| + | visudo -f / | ||
| + | ~~~ | ||
| + | |||
| + | `/ | ||
| + | ~~~ | ||
| + | jean ALL=(ALL) | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | # | ||
| + | # Disable "ssh hostname sudo < | ||
| + | # You have to run "ssh -t hostname sudo < | ||
| + | # | ||
| + | Defaults | ||
| + | Host_Alias LOCAL_SERVER=servername | ||
| + | Cmnd_Alias CHK_MSG=/ | ||
| + | Defaults: | ||
| + | nagios LOCAL_SERVER=(ALL) NOPASSWD: CHK_MSG | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | operator ALL=(root) sudoedit / | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | user1 ALL = NOPASSWD: /bin/ln -s / | ||
| + | user1 ALL = NOPASSWD: / | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | Faire des groupes | ||
| + | ~~~bash | ||
| + | sudo visudo -f / | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | Cmnd_Alias | ||
| + | Cmnd_Alias | ||
| + | Cmnd_Alias | ||
| + | %netadmin ALL=NETALL | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Demander le mot de passe du compte root au lieu de l' | ||
| + | |||
| + | ~~~ | ||
| + | Defaults rootpwc | ||
| + | ~~~ | ||
| + | |||
| + | ### Demander le mot de passe x fois | ||
| + | |||
| + | ~~~bash | ||
| + | Defaults passwd_tries=4 | ||
| + | ~~~ | ||
| + | |||
| + | ### Timeout | ||
| + | |||
| + | ~~~ | ||
| + | Defaults timestamp_timeout=x | ||
| + | |||
| + | Defaults: | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ### Logs | ||
| + | |||
| + | ~~~ | ||
| + | Defaults logfile=/ | ||
| + | ~~~ | ||
| + | |||
| + | ### Mails | ||
| + | |||
| + | ~~~ | ||
| + | # | ||
| + | Defaults | ||
| + | Defaults | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### PATH | ||
| + | |||
| + | `/ | ||
| + | ~~~ | ||
| + | Defaults | ||
| + | ~~~ | ||
| + | |||
| + | ## Exemples sudoers | ||
| + | |||
| + | ### Exemple sudoedit | ||
| + | |||
| + | `/ | ||
| + | ~~~ | ||
| + | exploit | ||
| + | exploit | ||
| + | exploit | ||
| + | exploit | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | export EDITOR=vim | ||
| + | sudoedit / | ||
| + | sudo -e / | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Alias | ||
| + | |||
| + | ~~~ | ||
| + | Cmnd_Alias ADMIN=/ | ||
| + | jean ALL= NOPASSWD: ADMIN | ||
| + | ~~~ | ||
| + | |||
| + | Voir [Sudo: You're Doing it Wrong](https:// | ||
| + | |||
| + | ~~~ | ||
| + | Defaults insults | ||
| + | |||
| + | # Users Hosts = (Runas) Cmds | ||
| + | # %Group Hosts = (Runas) Cmds | ||
| + | |||
| + | %wheel ALL=(ALL) ALL | ||
| + | |||
| + | Defaults env_keep+=" | ||
| + | |||
| + | mwlucas dns1=ALL | ||
| + | mwlucas, | ||
| + | / | ||
| + | |||
| + | |||
| + | mwlucas db1 = (oracle) ALL | ||
| + | mwlucas dns[1-4]=ALL | ||
| + | mwlucas ALL = / | ||
| + | |||
| + | mwlucas ALL=/ | ||
| + | |||
| + | # "" | ||
| + | mwlucas ALL=/ | ||
| + | |||
| + | Cmnd_Alias BACKUP = / | ||
| + | mwlucas ALL=BACKUP | ||
| + | |||
| + | User_Alias ADMIN_USERS = sysops, | ||
| + | |||
| + | User_Alias TAPEMONKEYS_USERS = mwlucas, jeanmm | ||
| + | Host_Alias WWW = web1, | ||
| + | TAPEMONKEYS_USERS WWW=BACKUP | ||
| + | |||
| + | Runas_Alias DB_RUNAS = oracle, pqsql, mysql | ||
| + | fred DB_HOSTS = (DB_RUNAS) ALL | ||
| + | |||
| + | DBA_USERS DB_HOSTS = (DB_RUNAS) ALL | ||
| + | |||
| + | mwlucas ALL = NOEXEC: ALL | ||
| + | |||
| + | |||
| + | Defaults!ALL NOEXEC | ||
| + | Cmnd_Alias MAYEXEC = / | ||
| + | mwlucas ALL = ALL, EXEC: MAYEXEC | ||
| + | |||
| + | mwlucas ALL = sudoedit / | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | identifiant ALL = (ALL) / | ||
| + | ~~~ | ||
| + | Toutes les commandes situées à la droite du mot-clé NOPASSWD: peuvent être exécutées par l' | ||
| + | |||
| + | ~~~ | ||
| + | User_Alias USER_T_PLOP_ALL=user1 | ||
| + | USER_T_PLOP_ALL= (jean) EXEC: NOPASSWD: ALL | ||
| + | |||
| + | # | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Checksum | ||
| + | |||
| + | Using openssl, to generate the checksum: | ||
| + | ~~~bash | ||
| + | openssl dgst -sha224 / | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | SHA224(/ | ||
| + | ~~~ | ||
| + | |||
| + | Then in your sudoers file (on the same line): | ||
| + | ~~~ | ||
| + | | ||
| + | sha224: | ||
| + | / | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Exemple sudo | ||
| + | |||
| + | Get shell | ||
| + | ~~~bash | ||
| + | sudo -u jean -i | ||
| + | sudo -u jean -s | ||
| + | sudo -u jean -s /bin/bash | ||
| + | sudo su - jean | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ## PAM | ||
| + | |||
| + | Source : https:// | ||
| + | |||
| + | Permette aux membres du groupe postgres d' | ||
| + | `/ | ||
| + | ~~~ | ||
| + | auth | ||
| + | auth | ||
| + | ~~~ | ||
| + | |||
| + | In the above configuration, | ||
| + | |||
| + | Équivalent à | ||
| + | ~~~ | ||
| + | %postgres ALL=NOPASSWD: | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ## Autres | ||
| + | |||
| + | ### Ausible - become | ||
| + | |||
| + | Voir : | ||
| + | * [[ansible_sudo_su_become_method|Ansible sudo su become_method become_flags]] | ||
| + | |||
| + | ~~~ | ||
| + | $ ansible-doc -t become ansible.builtin.sudo | ||
| + | ... | ||
| + | become_flags | ||
| + | default: -H -S -n | ||
| + | ... | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## gosu, setpriv, su-exec, setuser (Python) | ||
| + | |||
| + | Voir : | ||
| + | * [gosu, setpriv, su-exec](https:// | ||
| + | * [setuser Python Script](https:// | ||
| + | |||
| + | Dans un container doit être appelé exec `exec`. Exemple : | ||
| + | |||
| + | ~~~bash | ||
| + | exec gosu myAppUser / | ||
| + | ~~~ | ||
| + | |||
| + | Exemples : | ||
| + | * https:// | ||
| + | |||
| + | ~~~bash | ||
| + | gosu user-spec command [args] | ||
| + | gosu tianon bash | ||
| + | gosu nobody:root bash -c ' | ||
| + | gosu 1000:1 id | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | su-exec apache:1000 / | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
