How to change user data with chfn and usermod on Linux

A user account is more than a username. Learn how to set and change all metadata associated with a Linux user account from the command line.

What’s in a name

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

Additional information can be saved for each user account. Real-world data such as the person’s full name, office number, and business phone number, e.g. example, can be defined and appended to them. In fact, completely arbitrary snippets of information can be assigned to user accounts.

In practice, this is more useful for system administrators who take care of computers that have many user accounts configured. But even for the single-user Linux computer, it is always interesting to know what is going on under the hood.

The GECOS field

The Unix pioneers once did not have their own printer. They had their print jobs on one General electrics Mainframe on which the General Electric comprehensive operating system (GECOS). To achieve this, the users on the Unix systems had to save and use the access data of an account on the GECOS system.

The GECOS field was created to store these credentials. Exactly this niche requirement is long gone, and the GECOS field has been repurposed to store other data relating to the owner of the user account. It may have a new job, but it keeps its old name. It is still referred to as the GECOS field.

The field is in /etc/passwd File, along with further information about the user account:

  • The username of the account.
  • The user ID.
  • The group ID.
  • The path to the home directory for the user account.
  • The shell that starts when the user logs on.

Here is the one finger Command and the pinkie finger Command to get the information displayed.

How to use the finger command in Linux

The chfn command

the chfn (Change finger information) you can set and change the information stored in 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

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

finger dave -l

This could be found:

It gets the username of the account, the home directory, and the default shell. These all relate to the user account. They are not related to the real person using this account. We can use that chfn Command to save information about the real person.

chfn dave

Using chfn This will start a short, interactive process. You will be prompted to enter a data value for any information that the GECOS field may contain. The current value for each data value is shown in parentheses []. If you want to keep the current data, you can press the button “Enter”And the value in brackets is retained.

The information we entered was:

  • Room number: 512
  • Work phone: 555-4567
  • Landline phone: 555-5432

Let’s check this out finger picks up on this new information:

finger dave -l

Yes, it does. But you may have noticed that it wasn’t prompted to change its full name. This is only possible if you use it sudo. But we don’t have to go through it all chfn Meeting again just because we forgot to use sudo. We can change any of the data values ​​individually.

Change the individual data values

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

sudo chfn -f "Dave McKay" dave

And if we take another look finger:

We can see 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

Note that with some distributions you might find the -o (Office room number) option instead of the -r Opportunity. More on this shortly. To begin with the options for your version of. to see chfn, use:

man chfn

To change the office phone number, use the -w (Work phone) Option:

sudo chfn -w 555-1122 dave

And finally, to change the home phone number, use the -h (private phone number) Option:

sudo chfn -h 555-6576 dave

Let’s use finger again to see if all of these changes were accepted:

finger dave -l

Success. All of these commands worked and the data values ​​were updated.

Using chfn for other user accounts

So much for changing our own information, how about changing GECOS information for other users? It’s just as easy. You have to use sudo if you do, but that’s the only difference. Let’s take a look at what data is set for the mary user account:

finger mary -l

The usual standard information is set for this user account. Let’s complete the information:

sudo chfn mary

We go through the same session as before and are asked to enter a data value for each storable information. And because we used sudo , we are asked for the full name data value.

Wait a moment, what’s the last item named “Other?”

it is not supported by all versions of chfn, and you only see it when you use it sudowhich is why we didn’t see it earlier than we didn’t (intentionally) use it sudo with:

chfn dave

The data value “Other” can contain anything you want. It has no prescribed or expected use.

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

We can save the data stored for the Mary user account in the /etc/paswd File, with less:

less /etc/passwd

On systems that support this (check the man Page for your version of chfn ) you can open the “Other” field directly via the -o (other option:

sudo chfn -o "HTG Freelancer" dave

And we can dave the data for the user account with the less Command:

less /etc/passwd

weniger /etc/p<span data-mce-type=[AsswdineinemTerminalfenster“width=“646″height=“382″/>[asswdinaterminalwindow”width=”646″height=“382″/>[AsswdineinemTerminalfenster“width=“646″height=“382″/>[asswdinaterminalwindow”width=”646″height=”382″/>

That begs an obvious question. If your version of chfn does not support the Other field, how do you change it? We can do that with the usermod Command.

The usermod command

the usermod Command enables you Change aspects of a user account by such actions as adding or removing the user account to groups and changing their default shell. It can also be used to manipulate the GECOS field directly.

Let’s take a closer look at the line in the /etc/passwd File for the user account mary. We use grep to isolate this line for us. This works because usernames must be unique. There can only be one user account named mary.

grep mary /etc/passwd

The fields in the etc/passwd File will be displayed. A colon “:”Is used as a field separator. From left to right are the fields:

  • The username of the account.
  • An “x” indicating the password for this user account is 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 starts when the owner of the Mary account logs in.

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

  • Full name.
  • Room (or office) number.
  • Professional phone number.
  • Landline number.
  • Other information.

Note that the usermod sets the entire GECOS field to the new value you specify. If you only enter the full name of the person, only the full name will appear in the GECOS field. That means you need to provide any existing data values ​​that you want to keep.

Here is a example. User Mary has been promoted and will be moving to the fourth floor. She’ll get a new office number, a new business phone number, and we’ll add her middle name. Your “Other” field value will also change. Although your home phone number won’t change, we need to include it in the string in order to usermod.

We have to use them -c (Comment) option and we have to execute the command with sudo.

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

Using grep about the content of the /etc/passwd File for Mary, shows us that the new values ​​have been added.

grep mary /etc/passwd

Adding a user to a group (or second group) on Linux

Everything is flow, nothing remains

Inaccurate data is worthless. When information about people changes – office moves, name changes, role titles – you can easily update their metadata to reflect their current location.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · Rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts