tech:shell_script_-_allow_chown_for_non_root_users
Ceci est une ancienne révision du document !
Shell Script - Allow chown for non root users
Voir aussi :
- CAP_CHOWN
Pour changer le groupe propriétaire d'un fichier il faut soit être root ou alors il faut :
- Avoir les droits en lecture / écriture et
- Appartenir au groupe cible
L'autre solution, c'est de copier le fichier. Ce que fait le script ci-dessous.
- chuser.sh
#!/bin/bash # Creative Commons CC0 Public Domain Licence set -euo pipefail FICHIER=$1 TMPDIR="$(mktemp -d)" clean_on_exit() { rm -r "$TMPDIR" } trap clean_on_exit EXIT main() { local FICNAME FICNAME="$(basename "$FICHIER")" cp -d --preserve=all -r "$FICHIER" "$TMPDIR" rm -r "$FICHIER" mv "${TMPDIR}/${FICNAME}" "$FICHIER" } main
Exemple
roger$ echo PLOP > /home/share/plop.txt roger$ chmod a+rw /home/share/plop.txt roger$ ls -l /home/share/plop.txt -rw-rw-rw- 1 roger roger 5 Aug 12 10:03 /home/share/plop.txt
jean$ ./chuser.sh /home/share/plop.txt jean$ ls -l /home/share/plop.txt -rw-rw-rw- 1 jean jean 5 Aug 12 10:03 /home/share/plop.txt
Ce qui revient à faire
sudo chown $(whoami) /home/share/plop.txt
tech/shell_script_-_allow_chown_for_non_root_users.1742825205.txt.gz · Dernière modification : de 127.0.0.1
