Skip to main content

useradd command


Use of this command:

'useradd' command is used to add a new user into the system. In other words, it creates a new login account for a different user. 

Remember that only the root user can add new users into the system.


You can check who are the existing users in /etc/passwd file


To simply add a user to the system, this command is used:

useradd virat

This command adds the user named virat into the system and you can check whether virat is added or not in the /etc/passwd file.

Here, You can see that I have shifted to root user to add the user 'virat'

You can see that user virat has been added to /etc/passwd file
This is just a basic way of adding users into the system.

Now let us explore the various options of useradd command.

But before going to that, let us understand all the fields in /etc/passwd file.

There are 7 fields in /etc/passwd file for every user.

1. Username: Name of the user.

2. Password: This password is not visible. (marked x)

3. UID: User ID

4. GID: The group ID of which the user belongs to.

5. Comment field: Here the user can write anything which he/she wants to. This is optional.

6. User home directory: The user's home directory is shown in this field. By default, the user's home directory will be /home/username.

7. Default shell: This shows the name of the default shell with which user interacts. By default, it is bash shell.

Now that you've known about the fields of /etc/passwd file, let us explore various options of useradd command.

1. useradd -c: To add a comment

In /etc/passwd file, You can see that comment field is not empty

2. useradd -d: To set a home directory. If you don't set, the default one will be /home/username

Originally, the home directory should be /home/Mahender. but with -c option, we changed to /home/Mahi

3. useradd -u: Customize UID, set UID of your choice.

Here, we have set UID of Anand as per our choice (100787)

4. useradd -g: Add the user to an existing group with that existing group's GID.

Here we have added Praneeth to an existing group. The existing group's id is 1001.

5. useradd -G: Add the user to supplementary groups, i.e, other than the default primary group.

Here, -g for primary group, -G option for supplementary groups. You can view all the groups of a user with 'lid username'


6. useradd -e: Add the user with an expiration date. After the specified date, the user is disabled.

You can see the expiration date and other info the user with chage -l username

7. useradd -s: sets the default shell for the user. If this is not set, the default shell will be bash.

By default it should be /bin/bash. But with -s option, we changed to korne shell. (/bin/ksh)



Fun part:

Which command is used to create a user by taking password on the spot?

Post your answer in the comment section if you got it.










Comments

Popular posts from this blog

First post!

Hello World, My name is Sai Rohith Karanam, currently pursuing BTech Computer Science Engineering at Lovely Professional University, Punjab. I am a tyro in writing blogs. I'm just exploring a new field in my life. Actually, I would like to share something that happened before writing this blog. From the past 5 days, I was constantly trying to write a blog and was searching like hell all over the internet. All the questions in my mind like, where to publish my blog? what to write in my blog? These type of questions kept flashing through my mind honestly till today afternoon. I then saw some videos of "how to own a domain name and buy web hosting for a cheaper price", the cheaper price I finally got is 6000 INR, but I don't want to take a risk by affording money as I am still a novice. If at all I want to afford the money, let me gain a little bit of experience by writing here and then afford it later. Then, all of a sudden, an idea flashed through my mind and s

10 lessons I have learnt in 2020...

2020 is a year of shocks, surprises, learnings and a lot of introspection for me. I know with the arise of corona pandemic, many people have lost their lives, lost their jobs, lost their own families but still, there are a lot of things we need to be grateful for. For example, at this moment, you are alive, you are reading this, your senses are working properly and above all, your health is perfectly alright in spite of the pandemic.  Now tell me, how many good things are there that we need to be grateful for amidst of corona chaos? Every year is an experience we gain for the future, experience from the mistakes that we've done, experience from the way we have responded in a particular situation and experience from our self-fulfilling prophecies. So like everyone else, this year has really taught me some valuable lessons which I believe are the first step to achieving my goals. So I thought, why not share my experience of this year with people so that they may learn something out o

Purpose in Life vs Purpose of Life

A gentle disclaimer, This post is only written on the basis of my thoughts and perspectives. It is not intended to hurt anyone's beliefs and traditions. Okay, let's start. We study at prestigious universities, we get the highest paying jobs, we get lots of money which is well enough to buy every single thing for our own comfort, our parents will be very proud of everything we have achieved and a lot more praise from the society. Are these enough for your life to be successful? If you said no, you're on the right path. But if you said yes, Think again! Today we are in the year 2020, let's move a 1000 years back. Because you were born in the 20th or 21st century, you are struggling to become a doctor or an IT professional. What if you were born 1000 years ago? Well, you might opt fishing as your career, right? What did this symbolise?  Our purpose in life, which means it is the purpose which we should fulfil to make a living for our life keeps on varying.  Today, you are