search
HomeSystem TutorialLINUXManage users through user groups in Linux systems!

Manage users through user groups in Linux systems!

Feb 22, 2024 am 09:07 AM
linuxlinux tutorialRed Hatlinux systemlinux commandlinux certificationred hat linuxlinux video

In Linux, this is completely achievable. But first you must understand how to manage users through user groups and access control lists (ACLs).
We'll start with simple users and work our way up to complex access control lists (ACLs). You can do everything you need to do in the Linux distribution of your choice. The focus of this article is user groups, so basic knowledge about users will not be covered.

For demonstration purposes, I will assume:

You need to create two new users with the following two usernames:

  • olivia
  • nathan

You need to create the following two user groups:

  • readers
  • editors

olivia belongs to the editors group and nathan belongs to the readers group. The reader user group only has read permissions on the /DATA directory, while the editors user group has both read and write permissions on the /DATA directory. Granted, this is a very small task, but it will give you the basic information and you can expand this task to fit your other, larger needs.

I will demonstrate on Ubuntu 16.04 Server platform. These commands are common, the only difference is that if you do not use the sudo command in your distribution, you must switch to the root user to execute these commands.

Create user

The first thing we need to do is create two users for our experiment. You can use the useradd command to create a user. We don't just simply create a user, but we also need to create the user and their home directory, and then set a password for them.

sudo useradd -m olivia
sudo useradd -m nathan

We have now created two users, and if you look in the /home directory, you can find their home directories (because we used the -m option to create their home directories at the same time as the user was created.

Afterwards, we can set passwords for them with the following command:

sudo passwd olivia
sudo passwd nathan

In this way, we created two users.

Create user groups and add users

Now we will create the readers and editors user groups and then add users to them. The command to create a user group is:

addgroup readers
addgroup editors

(LCTT Translation: When you use some Linux distributions such as CentOS, the system may not have the addgroup command. It is recommended to use the groupadd command to replace the addgroup command to achieve the same effect)
Linux 系统中通过用户组管理用户!

Figure 1: We can use the new user group we just created.

After creating the user group, we need to add our users to these two user groups. We use the following command to add user nathan to the readers user group:

sudo usermod -a -G readers nathan

Add olivia to the editors user group using the following command:

sudo usermod -a -G editors olivia

Now we can manage users through user groups.

Grant directory permissions to user groups

Suppose you have a directory /READERS and allow all members of the readers group to access this directory. First, we execute the following command to change the user group to which the directory belongs:

sudo chown -R :readers /READERS

Next, execute the following command to revoke the write permissions of the user group to which the directory belongs:

sudo chmod -R g-w /READERS

Then we execute the following command to revoke other users’ access rights to this directory (to prevent any user who is not in the readers group from accessing files in this directory):

sudo chmod -R o-x /READERS

At this time, only the owner of the directory (root) and users in the user group reader can access the files in /READES.

Suppose you have a directory /EDITORS. You need to give members of the user group editors read and write permissions to this directory. In order to achieve this purpose, it is necessary to execute the following commands:

sudo chown -R :editors /EDITORS
sudo chmod -R g+w /EDITORS
sudo chmod -R o-x /EDITORS

At this time, all members of the editors user group can access and modify the files in it. Otherwise no other user (except root) can access any files in /EDITORS.

The problem with using this method is that you can only operate one group and one directory at a time. This is where access control lists (ACLs) come in handy.

Use Access Control List (ACL)

现在,让我们把这个问题变得棘手一点。假设你有一个目录 /DATA 并且你想给 readers 用户组的成员读取权限,并同时给 editors 用户组的成员读和写的权限。为此,你必须要用到 setfacl 命令。setfacl 命令可以为文件或文件夹设置一个访问控制表(ACL)。

这个命令的结构如下:

setfacl OPTION X:NAME:Y /DIRECTORY

其中 OPTION 是可选选项,X 可以是 u(用户)或者是 g (用户组),NAME 是用户或者用户组的名字,/DIRECTORY 是要用到的目录。我们将使用 -m 选项进行修改。因此,我们给 readers 用户组添加读取权限的命令是:

sudo setfacl -m g:readers:rx -R /DATA

现在 readers 用户组里面的每一个用户都可以读取 /DATA 目录里的文件了,但是他们不能修改里面的内容。

为了给 editors 用户组里面的用户读写权限,我们执行了以下命令:

sudo setfacl -m g:editors:rwx -R /DATA

上述命令将赋予 editors 用户组中的任何成员读取权限,同时保留 readers 用户组的只读权限。

更多的权限控制

使用访问控制表(ACL),你可以实现你所需的权限控制。你可以添加用户到用户组,并且灵活地控制这些用户组对每个目录的权限以达到你的需求。如果想了解上述工具的更多信息,可以执行下列的命令:

  • man usradd
  • man addgroup
  • man usermod
  • man sefacl
  • man chown
  • man chmod

The above is the detailed content of Manage users through user groups in Linux systems!. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Linux就该这么学. If there is any infringement, please contact admin@php.cn delete
Compare and contrast the security models of Linux and Windows.Compare and contrast the security models of Linux and Windows.Apr 24, 2025 am 12:03 AM

The security models of Linux and Windows each have their own advantages. Linux provides flexibility and customizability, enabling security through user permissions, file system permissions, and SELinux/AppArmor. Windows focuses on user-friendliness and relies on WindowsDefender, UAC, firewall and BitLocker to ensure security.

How does hardware compatibility differ between Linux and Windows?How does hardware compatibility differ between Linux and Windows?Apr 23, 2025 am 12:15 AM

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

What are the differences in virtualization support between Linux and Windows?What are the differences in virtualization support between Linux and Windows?Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Is it hard to learn Linux?Is it hard to learn Linux?Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

What is the salary of Linux administrator?What is the salary of Linux administrator?Apr 17, 2025 am 12:24 AM

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

What is the main purpose of Linux?What is the main purpose of Linux?Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Does the internet run on Linux?Does the internet run on Linux?Apr 14, 2025 am 12:03 AM

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.