Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the useradd command in linux to create a new user

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you how to use the useradd command in linux to create a new user. I hope you will get something after reading this article. Let's discuss it together.

A new user

You can use the useradd command to create a new user. Run useradd with superuser or root privileges, even with sudo or su, depending on your system, you can:

Sudo useradd-m guest

And enter your password. Or it could be like this:

Su-c "useradd-m guest"

Then enter the password of the root or superuser.

(for the sake of brevity, we will assume from now on that you use sudo to get superuser or root privileges. )

By using the-m parameter, useradd creates a home directory for the new user. You can view its contents by listing / home/guest.

Then you can use the following command to set the password for the new user:

Sudo passwd guest

Or you can use adduser, an interactive command that asks you questions about the shell you want to assign to users (yes, there is more than one shell), where you want their home directories to be, which groups you want them to belong to (more on this later), and so on. At the end of running adduser, you can set the password. Note that by default, adduser is not installed in many distributions, but useradd is installed.

By the way, you can use userdel to remove a user:

Sudo userdel-r guest

Using the-r option, userdel deletes not only guest users, but also their home directory and entries in their messages, if any.

Contents of the home directory

When it comes to the user's home directory, it depends on the distribution you use. You may have noticed that when you use the-m option, useradd populates the user's directory with subdirectories, including music, documents, and things like that, as well as various hidden files. To see everything in the guest home directory, run sudo ls-la / home/guest.

What is entered into the new user directory is usually determined by the / etc/skel schema directory. Sometimes it may be a different directory. To check the directory in use, run:

Useradd-DGROUP=100HOME=/homeINACTIVE=-1EXPIRE=SHELL=/bin/bashSKEL=/etc/skelCREATE_MAIL_SPOOL=no

This will give you some extra interesting information, but what you are interested in now is the SKEL=/etc/skel line, in which case, by convention, it points to / etc/skel/.

Because everything in Linux is customizable, you can change the contents that are placed in the newly created user directory. Try this: create a new directory in / etc/skel/:

Sudo mkdir / etc/skel/Documents

Then create a file containing the welcome message and copy it over:

Sudo cp welcome.txt / etc/skel/Documents

Now delete the guest account:

Sudo userdel-r guest

Create again:

Sudo useradd-m guest

Hey! Hey! Your Documents/ directory and welcome.txt files magically appear in the home directory of guest.

You can also modify other content by editing / etc/default/useradd when creating a user. Mine looks like this:

GROUP=usersHOME=/homeINACTIVE=-1EXPIRE=SHELL=/bin/bashSKEL=/etc/skelCREATE_MAIL_SPOOL=no

Most of these options are self-evident, but let's take a closer look at the GROUP option.

Group mentality

Linux and other Unix-like operating systems rely on user groups rather than assigning permissions and privileges to users one by one. A group is what you think: a group of users who are related to each other in some way. There may be a group of users on your system who are allowed to use printers. They belong to the lp (that is, "line printer") group. Traditionally, a member of the wheel group is *. You can become a superuser or a member of root by using su. The network user group can start or shut down the network. And a lot of things like that.

Different distributions have different groups, and groups with the same or similar names have different permissions, depending on the distribution you use. So don't be surprised if what you read in the previous paragraph doesn't match what's in your system.

Anyway, to see which groups are in the system, you can use:

Getent group

The getent command lists the contents of some system databases.

To find the group to which the current user belongs, try:

Groups

When you use useradd to create new users, unless you specify otherwise, the users will belong to only one group: themselves. The guest user belongs to the guest group. Groups give users the right to manage their own things, that's all.

You can use the groupadd command to create a new group and then add users:

Sudo groupadd photos has read this article, I believe you have a certain understanding of "how to use the useradd command in linux to create a new user". If you want to know more about it, welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report