/etc/passwd

The /etc/passwd file in Unix and Linux systems is a critical configuration file that stores user account information. Each line in the file represents a single user account and contains several fields separated by colons (:). Here’s a detailed explanation of the structure, purpose, and usage of the /etc/passwd file:

Each line in /etc/passwd has the following format:

username:x:uid:gid:comment:home_directory:shell
  • username: The login name of the user. It must be unique.

  • x: A placeholder for the password. The actual encrypted password is stored in the /etc/shadow file.

  • uid: The user ID number. It must be unique.

  • gid: The primary group ID number for the user.

  • comment: A comment field. Often used to store the full name of the user or other information.

  • home_directory: The absolute path to the user's home directory.

  • shell: The absolute path to the user's login shell.

Example Entry

john:x:1001:1001:John Doe:/home/john:/bin/bash
  • john: The username.

  • x: Indicates the password is stored in /etc/shadow.

  • 1001: The user ID.

  • 1001: The primary group ID.

  • John Doe: The comment field, usually containing the user's full name.

  • /home/john: The home directory.

  • /bin/bash: The default shell for the user.

Fields in Detail

  1. Username

    • Used to log in and should be unique.

    • Typically lowercase and can include numbers and underscores.

  2. Password Placeholder (x)

    • Historically, this field stored the encrypted password, but for security reasons, it now contains an x, and the actual password is stored in /etc/shadow.

  3. User ID (UID)

    • A unique identifier for the user.

    • UIDs 0-99 are typically reserved for system accounts.

  4. Group ID (GID)

    • The primary group for the user.

    • Corresponds to an entry in /etc/group.

  5. Comment

    • Often contains the full name of the user.

    • Can also include other information, such as contact details.

  6. Home Directory

    • The directory the user is placed in after login.

    • Should be unique and writable by the user.

  7. Shell

    • The program that runs when the user logs in.

    • Common shells include /bin/bash, /bin/sh, and /bin/zsh.

Managing /etc/passwd

Adding a User

To add a user, the useradd command is used:

sudo useradd -m -s /bin/bash john
  • -m: Creates the home directory.

  • -s: Specifies the login shell.

Modifying a User

To modify user details, the usermod command is used:

sudo usermod -s /bin/zsh john
  • -s: Changes the user's login shell.

Deleting a User

To remove a user, the userdel command is used:

sudo userdel -r john
  • -r: Removes the home directory and mail spool.

Viewing /etc/passwd

To view the contents of the /etc/passwd file, use:

cat /etc/passwd

Security Considerations

  • Password Storage: Passwords are not stored in /etc/passwd. Instead, they are stored in /etc/shadow, which is readable only by the root user.

  • File Permissions: /etc/passwd should be readable by all users but writable only by the root user. Typically, the permissions are set to 644:

-rw-r--r-- 1 root root 1234 Jul 19 12:34 /etc/passwd

Conclusion

The /etc/passwd file is essential for user management in Unix and Linux systems. Understanding its structure and the purpose of each field helps in effectively managing user accounts and ensuring the security and proper functioning of the system. Proper handling and administration of this file are crucial for maintaining system integrity and security.

Last updated