List Groups in Ubuntu Command Line

List Groups in Ubuntu Command Line Техника
  • Primary group: Also known as the login or default group. If the user creates a file, the primary group takes the group ownership. Usually, the primary group has the same name as the user.
  • Secondary group: Also known as a supplementary group. It is used to grant specific permissions to users. For example, in Ubuntu, only members of the sudo groups can run commands with sudo.
Содержание
  1. How to check the groups of a user
  2. List groups of the current user
  3. List groups of the other users
  4. List all the groups in Ubuntu
  5. How to list groups using the getent command
  6. List every member of a specific group
  7. Want to know how you can add a user to a group?
  8. List all users with /etc/passwd file
  9. So many users? Which ones are real?
  10. Only list user names, not other details in /etc/passwd
  11. Use getent command to list users
  12. Use compgen to get the list of users
  13. Check if a user exists on the system
  14. New Book: Efficient Linux at the Command Line
  15. List normal users only (for scripting)
  16. List currently logged in users
  17. In the end.
  18. How to test whether a user has sudo privileges or not
  19. Method 1: Check if user is sudoer with the sudo command
  20. Method 2: Check if user is part of the sudo group
  21. Bonus Tip: How to find all sudo users in your system
  22. Check user group in Linux command line
  23. Find out groups of other users in Linux
  24. Bonus Tip: Get group information along with gid
  25. Bonus Tip 2: Get primary group of a user in Linux
  26. Add a User to a Group in Linux
  27. Remove a User from a Group in Linux
  28. Adding User Accounts
  29. Linux File Permissions
  30. Group Management
  31. Special Linux File Attributes
  32. Accessing the root Account and Using sudo
  33. PAM (Pluggable Authentication Modules)
  34. Summary
Дополнительно:  What are Root Hints?

How to check the groups of a user

List groups of the current user

groups
list groups of the curent user

Yep, all you have to do is execute the groups command!

List groups of the other users

groups username
groups root
groups sagar
list groups of the specific user

List all the groups in Ubuntu

The /etc/group file contains all the necessary information about the local group.

So you can read the contents of that file and easily list the groups:

cat /etc/group
use groups file to list groups in ubuntu

Similarly, if you want to find a specific group, you can use the grep command to filter the output:

cat /etc/group | grep sudo
find speciofic group in linux

In case you are wondering about the meaning of the output, here’s what that means:

  • sudo is the name of the group.
  • x indicates that shadow passwords are being used and can be found at /etc/gshadow.
  • 27 is the group ID.
  • sagar is the list of the group users. You can have multiple of them.

And if they seem irrelevant, you can use the cut command and print the group names only:

cut -d: -f1 /etc/group
use the cut command to trim output in ubuntu terminal

Another option is to use the getent command

How to list groups using the getent command

The getent command is used to get entries from the database, including passwd and group database.

This means it can be used for our purpose too!

To get a list of groups, all you need to do is append group to the getent command:

getent group
use getent command to list groups in ubuntu terminal

List every member of a specific group

getent group [group_name]
getent group sudo
list each member of user group in ubuntu

Want to know how you can add a user to a group?

Cannot see the group you are looking for? How about creating that group?

Дополнительно:  Как изменить ttl на андроид без root прав билайн

How to Add New Groups in Ubuntu

Need to create a new group? Here’s how to create groups in Ubuntu using the groupadd command.

List Groups in Ubuntu Command Line

List Groups in Ubuntu Command Line

List Groups in Ubuntu Command Line

I hope you will find this guide helpful and if you have any queries, let me know in the comments.


Subscribe

Join the newsletter to get the latest updates.

Great! Check your inbox and click the link

Please enter a valid email address!


List all users with /etc/passwd file

cat /etc/passwd

You’ll probably see a huge output like this:

List users in Ubuntu by looking at the contents of /etc/passwd file
  • User name
  • Password, x means that a password is set for the user
  • User ID (UID)
  • User’s group ID (GID)
  • Full name, room number, phone number etc (optional)
  • Home directory of the user
  • Default login shell for the user
Understanding the content of /etc/passwd file in Linux

So many users? Which ones are real?

grep -E '^UID_MIN|^UID_MAX' /etc/login.defs

In some Linux systems, UID_MIN is 500. But in Ubuntu it is 1000.

[email protected]:~# grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
UID_MIN			 1000
UID_MAX			60000

DigitalOcean – The developer cloud

Helping millions of developers easily build, test, manage, and scale applications of any size – faster than ever before.

List Groups in Ubuntu Command LineExplore our products

List Groups in Ubuntu Command Line

Get started on DigitalOcean with a $100, 60-day credit for new users.

Only list user names, not other details in /etc/passwd

Use the cut command to use the : as column separator and then display the first column.

cat /etc/passwd | cut -d: -f1

You can do the same with the awk command:

cat /etc/passwd | awk -F: '{print $1}'

Both commands will give you output like this:

root
daemon
bin
sys
sync
games
man
lp

Forgot your own password or have to reset it for others? Here’s how to change the password in the Ubuntu command line.

List Groups in Ubuntu Command Line

Use getent command to list users

The getent command queries the configuration files located at /etc/nsswitch.conf. The /etc/passwd file is one of them.

So, you query it with getent like this:

getent passwd

This will give you a result similar to what you saw in the contnet of /etc/passwd file:

[email protected]:~# getent passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
getent passwd | cut -d: -f1

Use compgen to get the list of users

compgen -u
root
daemon
bin
sys
sync
games
man
lp

Check if a user exists on the system

getent passwd : grep user_name
[email protected]:~# getent passwd | grep prakash
prakash:x:1002:1002:,,,:/home/prakash:/bin/bash

If you don’t see it, it doesn’t.

[email protected]:~# getent passwd | grep random
[email protected]:~# 

List Groups in Ubuntu Command Line

New Book: Efficient Linux at the Command Line

Get it from Amazon

List normal users only (for scripting)

This is good enough for day-to-day, manual use. But things will be different if you want to use this information in a script.

In that case, you could do something like this:

eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1

List currently logged in users

There are multiple ways to do that. The most common is the who command:

who

It will show additional details like the time of the last login and the IP address from where it was accessed.

[email protected]:~# who
root     pts/0        2022-09-07 02:23 (103.211.12.162)
users
[email protected]:~# users
root

In the end.

That’s a pretty good addition to your Linux knowledge. Feel free to leave your questions and suggestions in the comments.

Wondering if you have sudo rights on your system? It’s easy to verify. Just run any command with sudo. If you don’t have sudo rights, you should see it in the output:

[email protected]:~$ sudo -v
Sorry, user standard may not run sudo on linuxhandbook.

How to test whether a user has sudo privileges or not

Method 1: Check if user is sudoer with the sudo command

sudo -l -U user_name
Matching Defaults entries for abhi on test-server:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User abhi may run the following commands on test-server:
    (ALL : ALL) NOPASSWD: ALL
User abhi is not allowed to run sudo on test-server.
sudo -l

Did you know?

This command also shows what commands are forbidden for you to run with sudo. Yes, that’s right. You can configure what commands can be run with sudo and what cannot. This is useful in an environment where the dev team needs to run only a certain applications with sudo. Limiting their use of sudo to these applications only will help the system from unwarranted misuse of the sudo rights by the hands of the developers.

Method 2: Check if user is part of the sudo group

groups user_name
[email protected]:~$ groups abhi
abhi : abhi sudo

Bonus Tip: How to find all sudo users in your system

In Linux, there are multiple ways to achieve the same result. Unsurprisingly, you have multiple ways to list the members of a group in Linux.

getent group sudo

And this would list all the sudoers:

[email protected]:~$ getent group sudo
sudo:x:27:abhi,seeni

If you have some other cool tip on this topic, please leave a comment below.

Now that might make you curious about knowing which groups you belong to and this is exactly what I am going to show you in this quick tutorial.

Check user group in Linux command line

groups

This will show all the groups you belong to.

[email protected]:~$ groups 
abhishek adm cdrom sudo dip plugdev lpadmin sambashare kvm
How To Find Group Of A Linux User

Find out groups of other users in Linux

groups user_name
[email protected]:~$ groups prakash
prakash : prakash sudo
groups user_1 user_2 user_3
[email protected]:~$ groups abhishek prakash
abhishek adm cdrom sudo dip plugdev lpadmin sambashare kvm
prakash : prakash sudo

Bonus Tip: Get group information along with gid

id user_name
uid=1000(abhishek) gid=1000(abhishek) groups=1000(abhishek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare),127(kvm)

5 Commands for Group Management in Linux

Group is an essential part of Linux system management and security. Check out various commands that you can use for managing groups in Linux.

List Groups in Ubuntu Command Line

List Groups in Ubuntu Command Line

Bonus Tip 2: Get primary group of a user in Linux

id -gn user_name

If you have questions or suggestions, please feel free to use the comment section.

# groups tecmint

tecmint : tecmint wheel
# group

root
Check a User Group in Linux

Add a User to a Group in Linux

# usermod -aG postgres tecmint
# groups tecmint
Add User to Group in Linux

Remove a User from a Group in Linux

# gpasswd -d tecmint postgres
# groups tecmint
Remove User from Group in Linux
$ sudo deluser tecmint postgres
$ man groups
$ man usermod
$ man gpasswd
$ man deluser
Linux Users and Groups Management
Linux Foundation Certified Sysadmin – Part 8

Adding User Accounts

# adduser [new_account]
# useradd [new_account]

.bash_logout
.bash_profile
.bashrc
Understanding /etc/passwd
[username]:[x]:[UID]:[GID]:[Comment]:[Home directory]:[Default shell]
  1. Fields [username] and [Comment] are self explanatory.
  2. The x in the second field indicates that the account is protected by a shadowed password (in /etc/shadow), which is needed to logon as [username].
  3. The [UID] and [GID] fields are integers that represent the User IDentification and the primary Group IDentification to which [username] belongs, respectively.
  4. The [Home directory] indicates the absolute path to [username]’s home directory, and
  5. The [Default shell] is the shell that will be made available to this user when he or she logins the system.
Understanding /etc/group
[Group name]:[Group password]:[GID]:[Group members]
  1. [Group name] is the name of group.
  2. An x in [Group password] indicates group passwords are not being used.
  3. [GID]: same as in /etc/passwd.
  4. [Group members]: a comma separated list of users who are members of [Group name].
Add User Accounts
# usermod [options] [username]
Setting the expiry date for an account
# usermod --expiredate 2014-10-30 tecmint
Adding the user to supplementary groups
# usermod --append --groups root,users tecmint
Changing the default location of the user’s home directory
# usermod --home /tmp tecmint
Changing the shell the user will use by default
# usermod --shell /bin/sh tecmint
Displaying the groups an user is a member of
# groups tecmint
# id tecmint

Now let’s execute all the above commands in one go.

# usermod --expiredate 2014-10-30 --append --groups root,users --home /tmp --shell /bin/sh tecmint
usermod Command Examples
Disabling account by locking password
# usermod --lock tecmint
Unlocking user password
# usermod --unlock tecmint
Lock User Accounts
Creating a new group for read and write access to files that need to be accessed by several users
# groupadd common_group # Add a new group
# chown :common_group common.txt # Change the group owner of common.txt to common_group
# usermod -aG common_group user1 # Add user1 to common_group
# usermod -aG common_group user2 # Add user2 to common_group
# usermod -aG common_group user3 # Add user3 to common_group
Deleting a group
# groupdel [group_name]

If there are files owned by group_name, they will not be deleted, but the group owner will be set to the GID of the group that was deleted.

Linux File Permissions

Besides the basic read, write, and execute permissions that we discussed in Archiving Tools and Setting File Attributes – Part 3 of this series, there are other less used (but not less important) permission settings, sometimes referred to as “special permissions”.

Like the basic permissions discussed earlier, they are set using an octal file or through a letter (symbolic notation) that indicates the type of permission.

Deleting user accounts
# userdel --remove [username]

Group Management

  1. user1 (primary group: user1)
  2. user2 (primary group: user2)
  3. user3 (primary group: user3)
# chmod 660 common.txt
OR
# chmod u=rw,g=rw,o= common.txt [notice the space between the last equal sign and the file name]

This is where groups come in handy, and here’s what you should do in a case like this.

Understanding Setuid
passwd Command Examples
passwd Command Examples
Change User Password
Understanding Setgid
# chmod g+s [filename]

To set the setgid in octal form, prepend the number 2 to the current (or desired) basic permissions.

# chmod 2755 [directory]
Setting the SETGID in a directory
Add Setgid in Linux
Add Setgid to Directory
Understanding Sticky Bit
# chmod o+t [directory]

To set the sticky bit in octal form, prepend the number 1 to the current (or desired) basic permissions.

# chmod 1755 [directory]

Without the sticky bit, anyone able to write to the directory can delete or rename files. For that reason, the sticky bit is commonly found on directories, such as /tmp, that are world-writable.

Add Stickybit in Linux
Add Stickybit to Directory

Special Linux File Attributes

# chattr +i file1
# chattr +a file2

After executing those two commands, file1 will be immutable (which means it cannot be moved, renamed, modified or deleted) whereas file2 will enter append-only mode (can only be open in append mode for writing).

Protect File from Deletion
Chattr Command to Protect Files

Accessing the root Account and Using sudo

$ su

and then entering root’s password.

If authentication succeeds, you will be logged on as root with the current working directory as the same as you were before. If you want to be placed in root’s home directory instead, run.

$ su -

and then enter root’s password.

Enable sudo Access on Linux
Enable Sudo Access on Users
# visudo

These are the most relevant lines.

Defaults    secure_path="/usr/sbin:/usr/bin:/sbin"
root        ALL=(ALL) ALL
tecmint     ALL=/bin/yum update
gacanepa    ALL=NOPASSWD:/bin/updatedb
%admin      ALL=(ALL) ALL

Let’s take a closer look at them.

Defaults    secure_path="/usr/sbin:/usr/bin:/sbin:/usr/local/bin"

The next lines are used to specify permissions.

root        ALL=(ALL) ALL
  1. The first ALL keyword indicates that this rule applies to all hosts.
  2. The second ALL indicates that the user in the first column can run commands with the privileges of any user.
  3. The third ALL means any command can be run.
tecmint     ALL=/bin/yum update
gacanepa    ALL=NOPASSWD:/bin/updatedb
%admin      ALL=(ALL) ALL

To see what privileges are granted to you by sudo, use the “-l” option to list them.

Sudo Access Rules
Sudo Access Rules

PAM (Pluggable Authentication Modules)

Pluggable Authentication Modules (PAM) offer the flexibility of setting a specific authentication scheme on a per-application and / or per-service basis using modules. This tool present on all modern Linux distributions overcame the problem often faced by developers in the early days of Linux, when each program that required authentication had to be compiled specially to know how to get the necessary information.

For example, with PAM, it doesn’t matter whether your password is stored in /etc/shadow or on a separate server inside your network.

Files inside /etc/pam.d indicate which applications are using PAM natively. In addition, we can tell whether a certain application uses PAM by checking if it the PAM library (libpam) has been linked to it:

# ldd $(which login) | grep libpam # login uses PAM
# ldd $(which top) | grep libpam # top does not use PAM
Check Linux PAM Library
Check Linux PAM Library
# cat /etc/passwd
PAM Configuration File for Linux Password
PAM Configuration File for Linux Password

The first column indicates the type of authentication to be used with the module-path (third column). When a hyphen appears before the type, PAM will not record to the system log if the module cannot be loaded because it could not be found in the system.

  1. account: this module type checks if the user or service has supplied valid credentials to authenticate.
  2. auth: this module type verifies that the user is who he / she claims to be and grants any needed privileges.
  3. password: this module type allows the user or service to update their password.
  4. session: this module type indicates what should be done before and/or after the authentication succeeds.

The second column (called control) indicates what should happen if the authentication with this module fails:

  1. requisite: if the authentication via this module fails, overall authentication will be denied immediately.
  2. required is similar to requisite, although all other listed modules for this service will be called before denying authentication.
  3. sufficient: if the authentication via this module fails, PAM will still grant authentication even if a previous marked as required failed.
  4. optional: if the authentication via this module fails or succeeds, nothing happens unless this is the only module of its type defined for this service.
  5. include means that the lines of the given type should be read from another file.
  6. substack is similar to includes but authentication failures or successes do not cause the exit of the complete module, but only of the substack.

The fourth column, if it exists, shows the arguments to be passed to the module.

For example, if you append

remember=2
password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok
password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok remember=2
Linux Password Fields
Linux Password Fields

Summary

Оцените статью
Master Hi-technology
Добавить комментарий