Tuesday | 30 APR 2024
[ previous ]
[ next ]

Adding E-mail Addresses to Linux Users

Title:
Date: 2023-06-09
Tags:  sysadmin, linux

I recently wanted to save e-mail addresses of users in the Linux system and I found that you can add it to /etc/passwd. There is a field called the GECOS field which stands for General Electric Comprehensive Operating Supervisor field. Which to be completely honest doesn't make much sense. It is the 5th field of the /etc/passwd file. Usually you'll see the first name here but this field can actually be a comma delimited list of name, location, and phone numbers.

The field shows its age in that e-mail isn't one of the fields but we can use one of the phone number fields to store the e-mail address.

The command to update this field is chfn, which stands for change finger. I really need a history these early protocols because finger looks like a way to check a person's phone number and location. Was that the point of these linux systems before, to be a yellow pages of sorts?

chfn -p "hey@example.com" username

This will then prompt you for the password and there we go, we have added our e-mail address to the phone number field.

Now we can list out our /etc/passwd entry:

getent passwd nivethan

Which should display:

username:x:1000:1000:username,,hey@example.com:/home/username:/usr/bin/fish

As you can see the 5th field contains the GECOS field, the e-mail address is the 3rd field in that comma delimited list. The first field is name, the second is the office location, the third is the work phone number and the fourth field is the home phone number.

I didn't know that getent was a tool to read these system files which is helpful. I was originally using cat /etc/passwd and grep to find things but run into issues with matching not being exact and getting too many results. Now I can use getent to get a specific user by username or by userid.

Now that we have our e-mail addresses, I can write a quick command to get it any time I need it in a shell script:

getent passwd 1030 | cut -d: -f5 | cut -d, -f3

This should result in:

hey@example.com

Now we know how to add e-mail addresses to the system.

Interesting things to look into

  1. The Finger protocol
  2. What was the point of the finger protocol
  3. getent and /etc/nsswitch.conf
  4. Why is it called the GECOS field
  5. Why was GECOS field never updated
  6. Can getent query deeper into the comma delimited list