Red hat on a mysterious woman (generated by Midjourney). Credit: libre-software.net. License: CC BY-SA 4.0
How to use sudo on RHEL, CentOS, AlmaLinux, Rocky Linux or Oracle Linux
Last updated on October 6, 2022
This short howto is about setting up sudo on Red Hat Entreprise Linux (RHEL) and its derivates – for example CentOS, AlmaLinux, Rocky Linux or Oracle Linux. It also gives a quick introduction on using the vi text editor through visudo.
Becoming root: su & sudo
CentOS, Scientific Linux and RHEL do not use sudo as Ubuntu does. Instead, you are supposed to use
su root to get higher permissions. su stands for super user or switch user. Type
exit to exit the superuser mode. More information on su on the CentOS Wiki.
After years using Ubuntu and Linux Mint, I got used to sudo. To be able to run sudo on CentOS, one has to add the current user to the /etc/sudoers file. Editing the sudoers file is done via the visudo utility.
Editing sudoers with visudo
Visudo edits the sudoers file in a safe fashion, locks the sudoers file against multiple simultaneous edits, provides basic sanity checks, and checks for parse errors. (from the visudo man page)
You can only run visudo as root, so that we’ll need to run su:
The next step is to run visudo without any parameters. It will open the /etc/sudoers file in vi (a text editor):
Using the vi text editor
There is no need to be familiar with vi, just to know the some basics. Vi has two so called “modes”:
– The insert mode allows you to insert/write text
– The command mode is for saving files, text manipulation and much more.
To edit a file, type i to enter the insert mode.
Press Esc to exit the insert mode and enter the command mode.
In command mode, use a : colon to start commands:
Press : + w + q + Enter to save your changes and exit (write and quit)
Type : + q + ! + Enter to exit without saving changes.
Add yourself to the sudoers in visudo
All you have to do now is to add the following line at the end of the file (press i to enter insert mode):
user_name ALL=(ALL) ALL
ALL must be written in capital letters. Replace user_name with your user name.
Finally, press Esc to enter the command mode, and : + w + q + Enter to save and exit the editor. Changes are effective immediately, you may now use sudo.
N.B. If you plan to use Truecrypt, comment out the following line in /etc/sudoers by inserting a dash:
By Johannes Eva, 2012 – October 2022