How to List Users on CentOS 7

29/12/2020
Linux based operating systems such CentOS are designed to work with mass number of users at the same time. If you’ve ever used shared Linux based web hosting, then you may have noticed. Your username may be something like xyz_232323. It technically means there are lot of users and each of the users are numbered. So as a Linux system administration, knowing how to list the users on CentOS and other Linux distribution is must.

In this article, I will show you how to list users on CentOS 7. Let’s get started.

The /etc/passwd File:

The users details are stored in /etc/passwd file on CentOS operating system. This is a plain text file. So you can read that with less or cat command as follows:

$ less /etc/passwd

As you can see, a list of all the users is printed. Information about a single user is stored in a single line /etc/passwd file. Each line contains a lot of information about that specific users separated by colon (:)

A line in the /etc/passwd file contains the following information separated by colon (:)

Username : Password : UID : GID : User Details : Home Directory Path :  Default Shell

Here, Username – The login name of the user. It can’t have spaces and other special characters. Only alpha-numeric characters are allowed.

Password – On Linux based operating system such as CentOS the password is stored on /etc/shadow file. If the password is set, x is stored in this field.

UID – On Linux based operating systems, each user has an alphanumeric name or Username and a numeric ID which is called User ID or UID. For ordinary users, the UID starts from 1000. The root user has the UID 0.

GID – On Linux based operating systems, each group has a group name and a numeric ID which is called Group ID or GID. On Linux, every user must be a member of at least one group. So for each user created, a group is also created with the same name as the user name. The user is then added to the group. In this field, the GID of the primary group is added. Primary group is the default group of the user. If a user is a member of many groups, he/she can change it while he/she is logged in with newgrp command. The GID for the ordinary groups starts at 1000. If your Linux system is not specifically configured, the GID should be the same as the UID. The root group has the GID 0.

User Details – This field keeps full name and other personal information about the user as comma separated list. The most common use of this field is to set the Full Name of the user. Just type in the Full Name of your username here and it should be set.

Home Directory Path – This field keeps the path to a directory where all the user files will be stored. The $HOME shell variable is set from this field.

Default Shell – On Linux based operating systems, you must have a shell set for your user. By default, the sh (/bin/sh) and bash (/bin/bash) shell should be set for ordinary users who are able to log in to the system . But there are other shells such as zsh, csh etc. The system users has /sbin/nologin shell set, so they won’t be able to log into the system. If you want to disable login for some user, just set this field for these users to /sbin/nologin.

You can get the same information as it is in the /etc/passwd file with the following command:

$ getent passwd

Output of the getent password:

Extracting only List of Username from /etc/passwd File:

The /etc/passwd file has a lot of information. If you want to extract only the username from the colon(:) separated list, then you can use awk or cut command to do that.

You can run the following command to extract the login name or username using the cut command:

$ cut -d: -f1 /etc/passwd

Or

$ getent passwd | cut -d: -f1

As you can see, all the usernames are extracted.

You can do the same thing with awk as follows:

$ awk -F: ‘{print $1}’ /etc/passwd

Or,

$ getent passwd | awk -F: ‘{print $1}’

As you can see, I get the same output.

Listing Users that Can Login to the System:

If you want to list all the users that can login to your CentOS operating system, then run the following command:

$ getent passwd | egrep -v ‘/s?bin/(nologin|shutdown|sync|halt)’ | cut -d: -f1

As you can see, only 4 users of my CentOS machine are able to log in and other users are system users. They can’t log in to the system.

So that’s how you list users on CentOS. Thanks for reading this article.

Sandclock IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, Sandclock IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

CLI #3: Giới thiệu lệnh chattr để bảo vệ sự toàn vẹn của file

Đối với Linux, thông thường chúng ta sử dụng phân quyền bằng lệnh chmod để bảo vệ tập tin....
30/12/2020

[Graylog] [LAB] [Phần 10] Cấu hình Graylog server tích hợp cảnh báo qua Telegram

Chắc hẳn có rất nhiều bạn đang sử dụng ứng dụng Telegram để trò chuyện, trao đổi thông tin....
30/12/2020

Performance Testing [Phần 1] Tổng quan về khái niệm Performance Testing

Kiểm thử hiệu năng (Performance Testing) là kỹ thuật kiểm thử nhằm xác định băng thông, khả năng...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019
26/10/2021

How to Update Ubuntu Linux
24/10/2021

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu
20/10/2021

Hướng dẫn cài đặt Apache CloudStack 4.15.2.0
19/10/2021

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader
19/10/2021