Linux Foundation Certified System Administrator: Print Usernames and Primary Groups from /etc/passwd

Print Usernames and Primary Groups from /etc/passwd

Question

Which of the following commands prints a list of usernames (first column) and their primary group (fourth column) from the /etc/passwd file?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

C

The correct answer is C. cut -d : -f 1,4 /etc/passwd.

Explanation:

The /etc/passwd file is a text file that contains information about user accounts on a Linux system. Each line in the file represents one user account and is composed of several fields separated by colons. The first field contains the username, the second field contains the password (encrypted), the third field contains the UID (user ID), the fourth field contains the GID (group ID), and so on.

To print a list of usernames and their primary group from the /etc/passwd file, we need to extract the first and fourth fields from each line and display them in a readable format. The cut command is a perfect tool for this task.

The cut command is used to extract specific columns (fields) from a text file. In our case, we want to extract the first and fourth columns from the /etc/passwd file, which contain the username and the GID, respectively. We can specify the delimiter (separator) between fields using the -d option. In this case, the delimiter is a colon (:). We can specify the fields we want to extract using the -f option. In this case, we want to extract the first and fourth fields, so we use the option -f 1,4.

Therefore, the correct command to print a list of usernames and their primary group from the /etc/passwd file is:

bash
cut -d : -f 1,4 /etc/passwd

This will output the list of usernames and their primary groups in the following format:

makefile
username:gid

where username is the username of the user account and gid is the GID of the user's primary group.