Cet article décrit le processus d'octroi de l'accès sudo à un utilisateur nouveau ou existant sur CentOS®.
Créer un nouvel utilisateur
-
Créez un nouvel utilisateur en utilisant le
adduser
commande suivie du<username>
du nouvel utilisateur comme illustré dans l'exemple suivant :[root@server-01 ~]# adduser newuser
-
Utilisez le
passwd
commande suivie du<username>
du nouvel utilisateur pour configurer un mot de passe pournewuser
. Saisissez deux fois le nouveau mot de passe dans l'invite de vérification.[root@server-01 ~]# passwd newuser Changing password for user newuser. New password: Retype new password: passwd: all authentication tokens updated successfully
Accorder des autorisations root à un utilisateur nouveau ou existant
-
Modifier les sudoers fichier en utilisant la commande suivante :
[root@server-01 ~]# visudo
-
Vous voyez alors une version similaire au texte suivant :
## Next comes the main part: which users can run what software on ## which machines (the sudoers file can be shared between multiple ## systems). ## Syntax: ## ## user MACHINE=COMMANDS ## ## The COMMANDS section may have other options added to it. ## ## Allow root to run any commands anywhere root ALL=(ALL) ALL ## Allows members of the 'sys' group to run networking, software, ## service management apps and more. # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS ## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALL ## Same thing without a password # %wheel ALL=(ALL) NOPASSWD: ALL ## Allows members of the users group to mount and unmount the ## cdrom as root # %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom ## Allows members of the users group to shutdown this system # %users localhost=/sbin/shutdown -h now ## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment) #includedir /etc/sudoers.d
-
Appuyez sur le i touche de votre clavier pour passer en mode insertion. Appuyez ensuite sur le j touche pour déplacer le curseur vers le bas et le k touche pour le déplacer jusqu'à la section suivante :
## Allow root to run any commands anywhere root ALL=(ALL) ALL
-
Ajoutez l'utilisateur nouvellement créé en insérant
<username> ALL=(ALL) ALL
sur une nouvelle ligne comme le montre l'exemple suivant :## Allow root to run any commands anywhere root ALL=(ALL) ALL newuser ALL=(ALL) ALL
-
Appuyez sur le i touche pour quitter le mode insertion, suivie de la saisie :wq pour enregistrer et quitter.
Vérifier le changement d'autorisation
-
Utilisez le
su
commande suivie de- <username>
pour accéder au nouveau compte utilisateur.[root@server-01 ~]# su - newuser [newuser@server-01 ~]$
-
Utilisez le
sudo -i
commande pour tester si le nouveau compte d'utilisateur peut élever les autorisations. Entrez le mot de passe du nouvel utilisateur. Vérifiez ces étapes en utilisant l'exemple suivant :[newuser@server-01 ~]$ sudo -i We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. [sudo] password for newuser: [root@server-01 ~]#
-
Utilisez le
whoami
pour vérifier que vous êtes actuellement l'utilisateur root.[root@server-01 ~]# whoami root