How to Change User Data With chfn and usermod on Linux

Configurare noua (How To)

Situatie

If you use a Linux or Unix-like computer, you’ll have a user account. The name given to the account is your user account name. This is the name you use to log in with. It’s also (by default) the name of your login group and the name of your home directory. They all use the same identifier.

There is another set of information that can be stored for each user account. Real-world data such as the person’s full name, their office number, and their work telephone number, for example, can be defined and attached to them. Indeed, completely arbitrary snippets of information can be tagged to user accounts.

In practical terms, this will be more useful for system administrators looking after computers that have many user accounts configured on them. But even for the single-user Linux computer, it’s always interesting to know what’s going on beneath the hood.

Solutie

The GECOS Field

At one time, the pioneers of Unix had no printer of their own. They had to spool their print jobs to a General Electric mainframe that was running the General Electric Comprehensive Operating System (GECOS). To accomplish that, the users on the Unix systems needed to store and use the credentials of an account on the GECOS system.

The GECOS field was created to store those credentials. That very niche requirement has long since passed into history, and the GECOS field has been re-purposed to store other data related to the owner of the user account. It may have a new job, but it retains its old name. It is still called the GECOS field.

The field is stored in the /etc/passwd file, along with other information regarding the user account:

  • The account user name.
  • The user ID.
  • The group ID.
  • The path to the home directory for the user account.
  • The shell that is started when the user logs in.

This is where the finger command and the pinky command retrieve the information that they display.

RELATED: How to Use the finger Command on Linux

The chfn Command

The chfn (change finger information) allows you to set and alter the information stored within the GECOS field. The information is stored as a comma-separated list within the field.

The chfn command was already present on Ubuntu 18.04.1 and Manjaro 18.1.0. It had to be installed on Fedora 31. Use this command to install it:

sudo dnf install util-linux-user
sudo dnf install util-linux-user in a terminal window

Let’s see what finger can find out about the owner of the user account “dave.” To get the maximum information, we’ll use the -l (long format) option:

finger dave -l
finger dave -l in a terminal window

This is what it could find:

Output of finger dave -l in a terminal window

It retrieves the account user name, the home directory, and the default shell. These are all related to the user account. They don’t refer to the real person who uses that account. We can use the chfn command to store information about the actual person.

chfn dave
chfn dave in a terminal window

Using chfn in this way starts a short, interactive process. You are prompted to type in a data value for each piece of information that the GECOS field can hold. The current value for each data value is displayed in brackets []. If you want to keep the current data, you can press the “Enter” key, and the value within the brackets will be retained.

The chfn interactive process in a terminal window

The information we entered was:

  • Room number: 512
  • Work phone: 555-4567
  • Home phone: 555-5432
Completed chfn session in a terminal window

Let’s check that finger picks up that new information:

finger dave -l
finger dave -l in a terminal window

Yes, it does. But you may have noticed that it didn’t prompt to change the full name. It’ll only do that if you use sudo. But we don’t have to go through the whole chfn session again, just because we forgot to use sudo. We can change any of the data values individually.

Changing the Individual Data Values

We can set the full name for the owner of the user account by using the -f (full name) option, together with sudo.

sudo chfn -f "Dave McKay" dave
sudo chfn -f "Dave McKay" dave ina terminal window

And if we check once more with finger:

Output from finger in a terminal window

We can see that the full name has been added.

There are other options to change the room number, home phone number, and work phone number. To change the room (office) number, use the -r (room number) option:

sudo chfn -r 633 dave
sudo chfn -r 633 dave in a terminal window

Note that on some distributions, you might use the -o (office room number) option instead of the -r option. More on this shortly. For now, to see the options for your version of chfn, use:

man chfn

To change the office telephone number, use the -w (work telephone) option:

sudo chfn -w 555-1122 dave
sudo chfn -w 555-1122 dave in a terminal window

And finally, to change the home telephone number use the -h (home telephone number) option:

sudo chfn -h 555-6576 dave
sudo chfn -h 555-6576 dave in a terminal window

Let’s use finger again to see if all of those changes have been accepted:

finger dave -l
finger dave -l in a terminal window

Success. All of those commands have worked, and the data values have been updated.

Using chfn On Other User Accounts

So much for changing our own information, what about changing the GECOS information for other users? That’s just as easy. You must use sudo when you do, but that’s the only difference. Let’s check what data is set for user account mary:

finger mary -l
finger mary -l in a terminal window

The usual default information is set for this user account. Let’s add to the information:

sudo chfn mary
sudo chfn mary  in a terminal window

We’ll run through the same session as we did before, being prompted for a data value for each piece of information that can be stored. And because we used sudo , we are asked for the full name data value.

Completed session for sudo chfn mary in a terminal window

Wait a moment, what’s that last item, called “Other?”

it isn’t supported by all versions of chfn, and you only see it if you use sudo, which is why we didn’t see it earlier when we (deliberately) didn’t use sudo with:

chfn dave

The “Other” data value can hold anything you like. It doesn’t have any prescribed or expected use.

Versions of chfn that support the “Other” field also provide the -o (other) option to change this value directly. It’s because of the conflict between -o for “office number” and -o for “other” that some systems use -r for “room number”.

We can see the data stored for user account mary in the /etc/paswd file, by using less:

less /etc/passwd
lass /etc/passwd in a terminal window

On systems that support it (check the man page for your version of chfn ) you can set the “other” field directly using the -o (other) option:

sudo chfn -o "HTG Freelancer" dave
sudo chfn -o "HTG Freelancer" dave in a terminal window

And we can check the data for user account dave using the less command:

less /etc/passwd
<img alt=”less /etc/p

[asswd in a terminal window” width=”646″ height=”382″ />

That raises an obvious question. If your version of chfn doesn’t support the “Other” field, how do you change it? We can do that with the usermod command.

The usermod Command

The usermod command allows you to modify aspects of a user account through such actions as adding or remove the user account from groups, and changing their default shell. It can also be used to directly manipulate the GECOS field.

Let’s take a closer look at the line in the /etc/passwd file for user account mary. We’ll use grep to isolate that line for us. This will work because user names have to be unique. There can only be one user account called mary.

grep mary /etc/passwd
grep mary /etc/passwd in a terminal window

The fields in the etc/passwd file are displayed. A colon “:” is used as the field separator. from left to right, the fields are:

  • The account user name.
  • An “x” which indicates the password for this user account is held encrypted in the /etc/shadow file.
  • The user ID for the mary user account.
  • The group ID for the mary user account.
  • The GECOS field.
  • The path to the home directory for the mary user account.
  • The shell that is started when the owner of the mary account logs in.

A comma “,” is used as the separator for the data values within the GECOS field. The values within the GECOS field are, from left to right:

  • Full Name.
  • Room (or office) number.
  • Work telephone number.
  • Home telephone number.
  • Other information.

Note that the usermod command sets the entire GECOS field to the new value you provide. If all you provide is the full name of the person, then the only thing in the GECOS field will be the full name. This means you have to provide any existing data values that you wish to keep.

Here’s an example. The user Mary has been promoted, and she is going to move to the fourth floor. She’s getting a new office number, a new work telephone number, and we’re going to add in her middle name. Her “Other” field value is also going to change. Although her home telephone number is not going to change, we must provide it in the string to usermod.

We need to use the -c (comment) option and we need to run the command with sudo.

sudo usermod -c "Mary Carol Quinn,405,5559654,555-7704,Linux Advocate" mary
sudo usermod -c "Mary Carol Quinn,405,5559654,555-7704,Linux Advocate" mary in a terminal window

Using grep to report on the contents of the /etc/passwd file for mary, shows us the new values have been added.

grep mary /etc/passwd
grep mary /etc/passwd in a terminal window

RELATED: Add a User to a Group (or Second Group) on Linux

All is Flux, Nothing Abides

Inaccurate data is worthless. When the information about people changes—office moves, name changes, role titles—you can easily update their meta-data to reflect the current position.

Tip solutie

Permanent

Voteaza

(5 din 10 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?