Understanding the /etc/passwd File in Linux

etc passwd

In Linux, the etc passwd file is a critical file that stores user account information. This file is used by the system to authenticate users and manage user accounts. In this article, we will explore the etc passwd file in detail, including its structure, usage, and related concepts.

What is the etc passwd file?

The etc passwd file is a plain text file that contains user account information for the system. This file is located in the /etc/ directory and contains one entry per line for each user account. Each line in the file contains seven fields, separated by a colon (:), as shown below:

username:password:UID:GID:comment:home_directory:shell

Here is a brief explanation of each field:

  • username: the name of the user account.
  • password: the encrypted password for the user account. This field is usually set to an ‘x’ to indicate that the password is stored in the /etc/shadow file.
  • UID: the user ID for the user account.
  • GID: the primary group ID for the user account.
  • comment: a comment that describes the user account.
  • home_directory: the home directory for the user account.
  • shell: the default shell for the user account.

How to use the etc passwd file

Viewing the contents of the etc passwd file

To view the contents of the etc passwd file, you can use the cat command as follows:

$ cat /etc/passwd

This will display the contents of the file in the terminal.

Adding a new user account

To add a new user account to the system, you can use the useradd command as follows:

$ sudo useradd -m -s /bin/bash newuser

In this example, we are creating a new user account called newuser. The -m option creates a home directory for the user, and the -s option sets the default shell for the user to /bin/bash.

Modifying an existing user account

To modify an existing user account, you can use the usermod command as follows:

$ sudo usermod -s /bin/zsh existinguser

In this example, we are modifying the default shell for an existing user account called existinguser to /bin/zsh.

Deleting a user account

To delete a user account from the system, you can use the userdel command as follows:

$ sudo userdel -r olduser

In this example, we are deleting a user account called olduser. The -r option removes the user’s home directory and mail spool.

Shadow password file

The etc passwd file stores user account information, but it does not store the encrypted passwords for the user accounts. Instead, the encrypted passwords are stored in the /etc/shadow file, which is only accessible by the root user. This is done for security reasons, as it prevents unauthorized access to user passwords.

User and group IDs

Every user account in Linux has a unique user ID (UID) and primary group ID (GID). The UID is used to identify the user account, while the GID is used to identify the user’s primary group. These IDs are used by the system to control access to files and directories.

Default shell

The default shell is the command interpreter that is used when a user logs in to the system. The shell is responsible for interpreting commands entered by the user and executing them. Linux supports a variety of shells, including Bash, Zsh, and Fish.

Conclusion

In this article, we have explored the etc passwd file in detail, including its structure, usage, and related concepts. We have seen how this file is used to manage user accounts in Linux and how it stores important user account information. By understanding the etc passwd file, you can manage user accounts more effectively and ensure the security of your Linux system.