In Linux, you use the useradd, passwd, usermod, and gpasswd commands to modify users or group-related information, like creating them, deleting them, assigning passwords, etc.
Note that these commands only provide you an interface to modify the “/etc/passwd
“, “/etc/shadow
“, “/etc/group
“, and “/etc/gshadow
” files with safety measures.
But some Linux users, like me, are total freaks and directly modify these files using a text editor instead of using the aforementioned commands, which keeps us safe from file corruption.
Problem with Direct File Modification
Now, you might be wondering what’s wrong with using the text editor to directly modify these configuration files instead of using the mentioned commands.
Look, when you modify the configuration files using the mentioned commands, there are some safety features in use, like locking the configuration file while you change it.
So, when you modify these configuration files and another user tries to modify the same file, they will not be able to save changes until you close the file to prevent file corruption.
But if you modify this file directly in a text editor, this safety feature doesn’t work, and another user can make changes and save them. This can lead to a corrupted file, which can be very bad if you’re working on a server.
You can still modify this file using your favorite text editor (with safety measures) with the help of the “vipw
” or “vigr
” commands, which you are about to learn in this article.
Tutorial Details
Description | Usage of Vipw and Vigr Commands |
Difficulty Level | Moderate |
Root or Sudo Privileges | Yes |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | vipw, vigr |
Internet Required | No |
What are the Vipw and Vigr Commands in Linux?
The “vipw
” (stands for “vi password“) and “vigr
” (stands for “vi group“) commands are very similar (we’ll discuss their differences later) and use your default text editor (choosing either from the “$VISUAL
” or “$EDITOR
” environment variables) to safely modify the following configuration files:
- “
/etc/passwd
“: User account information. - “
/etc/shadow
“: Secure user account information. - “
/etc/group
“: Group account information. - “
/etc/gshadow
“: Secure group account information.
So, when you use the “vipw
” and “vigr
” commands to modify the above-mentioned files, it sets the right locks so that other people can’t mess up the file by trying to change it during the editing session.
Syntax of the Vipw and Vigr Commands
Both commands only accept options as arguments.
$ vipw [OPTION]
$ vigr [OPTION]
Aside from the options listed below, these commands do not require a username.
Options | Description |
---|---|
-p | Edit the “/etc/passwd ” file. |
-g | Edit the “/etc/group ” file. |
-q | Vipw command will edit the “/etc/passwd ” file and vigr command will edit the “/etc/group ” file in quiet mode. |
-R | Apply changes to the “CHROOT_DIR ” directory and use the configurationfiles from the “ CHROOT_DIR ” directory. |
-s | Vipw command will edit the “/etc/shadow ” file and vigr command will edit the “/etc/gshadow ” file. |
-h | Display the help section. |
Difference Between the Vipw and Vigr Commands
The vipw command is specifically designed to modify user-related information, while the vigr command is mostly focused on editing group-related information.
The options table mentioned their difference, but you should check the following table to know their exact difference.
Vipw command | Vigr command |
---|---|
The “-q ” flag will modify the “/etc/passwd ” file in quiet mode. | The “-q ” flag will modify the “/etc/group ” file in quiet mode. |
The “-s ” flag will modify the “/etc/shadow ” file. | The “-s ” flag will modify the “/etc/gshadow ” file. |
Usage of Vipw and Vigr Commands in Linux
There isn’t much to say about this command; simply look through the options table to find the relevant option for your configuration file of interest.
For example, if you are interested in editing the “/etc/passwd
” file, then simply use one of the following commands which will safely open the configuration file in your default text editor.
$ sudo vipw -p
#OR
$ sudo vigr -p
Output:
The best part about using these commands to edit the “/etc/passwd
” file is that if another user tries to modify this file using commands like “useradd
“, then the command will not be executed until the above configuration file is closed.
Once the configuration file is closed, the above command will run automatically (if there are no errors) to create the user you specified on your Linux system.
So, using both of these commands, you can safely modify the configuration files that they offer to edit using the different options in your favorite text editor.
I hope this article might be useful for you to understand these two unknown (or less known) commands that provide you safety while editing the user or group configuration files.
If you have more amazing tools like this, then let us know in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.