Script for Granular sudo Permissions in PVE

Script for Granular sudo Permissions in PVE

Salut la team,

Depuis la version 12.3.1 vous pouvez configurer un compte user pour ajouter un hyperviseur Proxmox dans Veeam Backup & Replication. Le détail est expliqué dans la KB suivante : https://www.veeam.com/kb4701

Voici un script a executer sur l’hyperviseur PVE pour fluidifier tout cela. Il va créer un user « veeamdep », vous demander son password puis configurer les bons paramétrages.

 

#!/bin/bash

set -e

USERNAME= »veeamdep »
REALM= »pam »  # Change this to the desired auth realm, e.g., « pve »
SUDOERS_FILE= »/etc/sudoers.d/$USERNAME »
LOG_FILE= »/var/log/create_veeam_user.log »

# Logging function
log() {
echo « $(date +’%Y-%m-%d %H:%M:%S’) | $1 » | tee -a « $LOG_FILE »
}

# Function to securely read password
prompt_password() {
while true; do
read -s -p « Enter password for ‘$USERNAME’:  » PASSWORD
echo
read -s -p « Confirm password:  » PASSWORD_CONFIRM
echo
if [[ -z « $PASSWORD » ]]; then
echo « Password cannot be empty. Try again. »
elif [[ « $PASSWORD » != « $PASSWORD_CONFIRM » ]]; then
echo « Passwords do not match. Try again. »
else
break
fi
done
}

log « ——————————————— »
log « Starting user creation script for ‘$USERNAME' »

# Ensure sudo and visudo are installed
if ! command -v visudo &>/dev/null; then
log « ‘visudo’ not found. Installing ‘sudo’ package… »
apt update && apt install -y sudo
fi

# Prompt for password
prompt_password

# Create user
if id « $USERNAME » &>/dev/null; then
log « User ‘$USERNAME’ already exists. Skipping creation. »
else
log « Creating user ‘$USERNAME’… »
useradd -m -s /bin/bash « $USERNAME »

log « Setting password for ‘$USERNAME’… »
ENCRYPTED_PASS=$(openssl passwd -6 « $PASSWORD »)
echo « $USERNAME:$ENCRYPTED_PASS » | chpasswd -e
fi

# Add to sudo group
log « Adding ‘$USERNAME’ to the ‘sudo’ group… »
usermod -aG sudo « $USERNAME »

# Create sudoers file
log « Creating sudoers file ‘$SUDOERS_FILE’… »
cat <<EOF > « $SUDOERS_FILE »
$USERNAME ALL=(root) PASSWD: /usr/sbin/dmidecode -s system-uuid
$USERNAME ALL=(root) PASSWD: /usr/bin/kvm -S *
$USERNAME ALL=(root) PASSWD: /usr/bin/qemu-img info *
$USERNAME ALL=(root) PASSWD: /usr/bin/qemu-img create *
$USERNAME ALL=(root) PASSWD: /usr/sbin/qm create *
$USERNAME ALL=(root) PASSWD: /usr/sbin/qm ^showcmd [0-9]+ –pretty$
$USERNAME ALL=(root) PASSWD: /usr/sbin/qm ^unlock [0-9]+$
$USERNAME ALL=(root) PASSWD: /usr/bin/socat ^TCP-LISTEN:[0-9]+,bind=127\.0\.0\.1 UNIX-CONNECT:/[a-zA-Z0-9_./-]+$
$USERNAME ALL=(root) PASSWD: /usr/bin/mkdir -p /var/lib/vz/snippets/
$USERNAME ALL=(root) PASSWD: /usr/bin/pvenode cert info –output-format json
$USERNAME ALL=(root) PASSWD: /usr/bin/pvesh ^get storage/([a-zA-Z0-9_-]+) –output json$
$USERNAME ALL=(root) PASSWD: /usr/bin/pvesh ^set /nodes/([a-zA-Z0-9_-]+)/qemu/([0-9]+)/config –lock ([a-zA-Z]+)$
$USERNAME ALL=(root) PASSWD: /usr/bin/pkill -9 -e -f -x socat *
$USERNAME ALL=(root) PASSWD: /usr/sbin/lvchange -ay *
$USERNAME ALL=(root) PASSWD: /usr/sbin/lvchange -an *
$USERNAME ALL=(root) PASSWD: /usr/bin/rbd device map *
$USERNAME ALL=(root) PASSWD: /usr/bin/mv ^-n /tmp/([a-zA-Z0-9_-]+\.config) /var/lib/vz/snippets/([a-zA-Z0-9_-]+\.config)$
$USERNAME ALL=(root) PASSWD: /usr/bin/rm ^/[a-zA-Z0-9_/-]+/VeeamTmp[a-zA-Z0-9_.-]+$
$USERNAME ALL=(root) PASSWD: /usr/bin/rm ^-f /[a-zA-Z0-9_/-]+/VeeamTmp[a-zA-Z0-9_.-]+$
$USERNAME ALL=(root) PASSWD: /usr/bin/rm ^-f /var/lib/vz/snippets/[a-zA-Z0-9_-]+\.config$
$USERNAME ALL=(root) PASSWD: /usr/bin/rm ^-f /var/lib/vz/template/iso/[a-zA-Z0-9_.-]+\.img$
EOF

# Validate sudoers file
log « Setting permissions and validating syntax… »
chmod 440 « $SUDOERS_FILE »
visudo -cf « $SUDOERS_FILE »

# Add user to Proxmox configuration
log « Adding ‘$USERNAME’ to Proxmox VE as ‘Administrator’ on ‘/’… »
if ! pveum user list | grep -q « ^$USERNAME@$REALM »; then
pveum user add « $USERNAME@$REALM »
pveum aclmod / -user « $USERNAME@$REALM » -role Administrator
log « Proxmox user ‘$USERNAME@$REALM’ added successfully. »
else
log « Proxmox user ‘$USERNAME@$REALM’ already exists. Skipping. »
fi

log « User ‘$USERNAME’ configured successfully. »
log « End of script. »

Une fois le script executé, le résultat est le suivant :

2025-05-28 13:56:39 | ———————————————
2025-05-28 13:56:39 | Starting user creation script for ‘veeamdep’
Enter password for ‘veeamdep’:
Confirm password:
2025-05-28 13:56:44 | User ‘veeamdep’ already exists. Skipping creation.
2025-05-28 13:56:44 | Adding ‘veeamdep’ to the ‘sudo’ group…
2025-05-28 13:56:44 | Creating sudoers file ‘/etc/sudoers.d/veeamdep’…
2025-05-28 13:56:44 | Setting permissions and validating syntax…
/etc/sudoers.d/veeamdep: parsed OK
2025-05-28 13:56:44 | Adding ‘veeamdep’ to Proxmox VE as ‘Administrator’ on ‘/’…
2025-05-28 13:56:45 | Proxmox user ‘veeamdep@pam’ added successfully.
2025-05-28 13:56:45 | User ‘veeamdep’ configured successfully.
2025-05-28 13:56:45 | End of script.

Vous pouvez vérifier sur la webUI :

Il n’y a plus qu’a utiliser cet utilisateur dans VBR :

 

Et c’est tout bon !

credit bertrand c.

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur la façon dont les données de vos commentaires sont traitées.