User Group
In Linux, each user has a home (user group). A user group is a collection of users who share some resources and permissions and have private resources. This kind of structure is similar to a family structure.
How to see the user group name?
Use groups
Command
groups labex
View the group:
labex:project/ $ groups labex
labex : labex sudo ssl-cert public
As shown in the figure, the word before :
is the user name, and the words after :
are the group names for that user. If you do not specify a user group when creating a new user, the command will automatically create a user group with the same name as the user name.
By default, the sudo user group can use the sudo
command to gain root privileges. LabEx users can also use the sudo
command. You can view the /etc/sudoers.d/labex
file. We have created this file in the /etc/sudoers.d
directory to give sudo
permissions to the user label:
labex:project/ $ sudo cat /etc/sudoers.d/labex
labex ALL=(ALL) NOPASSWD: ALL
Defaults:labex !requiretty
View /etc/group
File
cat /etc/group | sort
The command cat
is used to read the contents of the specified file and print it out.| sort
means that the text is sorted and output by dict sort (dictionary sort).
labex:project/ $ cat /etc/group | sort
adm:x:4:
audio:x:29:pulse
avahi:x:114:
backup:x:34:
...
If you do not find it, it doesn't matter. However, you can use the following command to filter out some of the results you do not want to see:
cat /etc/group | grep -E "labex"
labex:project/ $ cat /etc/group | grep -E "labex"
sudo:x:27:labex
ssl-cert:x:121:labex
labex:x:5000:
public:x:5002:labex
Add other users to the sudo user group
By default, newly created users do not have root privileges, nor are they in the sudo user group. However, you can invite them to join the sudo user group to get root privileges:
su -l jack
sudo ls
After executing the commands, it will warn that "jack" is not in the sudoers file.
labex:project/ $ su -l jack
sudo ls
Password:
jack@65d2bb692aaf42ea18bb8350:~$ sudo ls
[sudo] password for jack:
jack is not in the sudoers file. This incident will be reported.
You can use the command usermod
to add a user to a user group.
We use the LabEx user to execute the sudo command to add “jack” to the sudo user group so that it can also get root privileges using the sudo
command:
exit
sudo groups jack
sudo usermod -G sudo jack
sudo groups jack
Then you switch back to “jack”, and now you can use sudo
to get root privileges.