linux howto get user's group,How to Create Users in Linux Using the 'useradd' Command

What to Know

To add a new user: Use the command useradd test ("test" is the new user's name). Use sudo useradd test if you lack the proper privileges.

To create a user with a home directory: Use sudo useradd -m testor sudo useradd -m -d /test testto change the default directory.

To set a user's password: Use passwd testand to switch users, use thesu - testcommand.

This article explains how to create users in Linux using the command line and the "useradd" command. We also share commands to create users with a home directory, set a user's password, switch users, set an expiry date when creating a user, assign specific groups for a new user, adjust login defaults, create a user without a home folder, specify a user's full name, and view details about a particular user.

How to Create a User

The following command adds a new user named testto your system.

The basic command to create a user (named test) would be:

useradd test

You'll need privileges to /etc/password to do this; as such, you'll likely sudo the above command:

sudo useradd test

What happens when this command is run depends on the contents of the configuration file located in /etc/default/useradd.

To view the contents of /etc/default/useradd, run the following command:

sudo nano /etc/default/useradd

The configuration file sets a default shell which in Ubuntu is bin/sh. All the other options are commented out.

format,png

The commented-out options allow you to set a default home folder, a group, the number of days after the password has expired before the account becomes disabled, and a default expiry date.

The important thing to glean from the above information is that running the useradd command without any switches may produce different results on different distributions and it is all to do with the settings in the /etc/default/useradd file.

The sudo utility is not installed on every distribution. If it's not installed, log in to an account with appropriate permissions for creating users.

How to Create a User With a Home Directory

Based on the /etc/defaults/useradd file, the user may or may not have been assigned a home directory based on the settings file.

To force the creation of a home directory, use the following command:

sudo useradd -m test

The above command creates a /home/test folder for the user test.

format,png

How to Create a User With a Different Home Directory

If you want the user to have a home folder in a different place than the default, use the -d switch.

sudo useradd -m -d /test test

The above command creates a folder called testfor user testunder the root folder.

format,png

Within the -m switch, the folder may not be created. It depends on the setting in the /etc/login.defs file.

To get this to work without specifying a -m switch, edit the /etc/login.defs file. At the bottom of the file, add the following line:

CREATE_HOME yes

How to Change a User's Password Using Linux

After you create a user with a home folder, change the user's password.

To set a user's password, use the following command:

passwd test

The passwd command allows you to set the test user's password. You will be prompted for the password you wish to use.

format,png

How to Switch Users

Test your new user's account by typing the following into a terminal window:

su - test

The above command switches user to the test account and, assuming you created a home folder, you will be placed in the home folder for that user.

format,png

Create a User With an Expiry Date

If you work in an office and a new contractor is going to be at your office for a short period of time, set an expiry date on their user account. Similarly, if you have family coming to stay, create a user account for that family member that expires after they depart.

To set an expiry date when creating a user, use the following command:

sudo useradd -m -e 2017-04-25 test

The date must be specified in the format YYYY-MM-DD where YYYY is the year, MM is the month number, and DD is the day number.

format,png

How to Create a User and Assign Them to a Group

When a new user joins your company, assign specific groups for that user so that they have access to the same files and folders as other members of their team. For example, John is joining as an accountant.

The following command would add johnto the accountsgroup.

sudo useradd -g accounts john

format,png

Adjust Login Defaults Within Linux

The /etc/login.defs file is a configuration file that provides the default behavior for login activities. There are some key settings in this file.

To open the /etc/login.defs file, enter the following command:

sudo nano /etc/login.defs

format,png

The login.defs file contains many settings including the following, which you might want to change:

PASS_MAX_DAYS: How long before a password expires.

PASS_MIN_DAYS: How often a password can be changed.

PASS_WARN_AGE: Number of days warning before a password expires.

LOGIN_RETRIES: Number of login attempts before failure.

LOGIN_TIMEOUT: How long before the login times out.

DEFAULT_HOME: A user can log in if no home folder exists.

These are the default options and can be overridden when creating a new user.

How to Specify Login Password Expiry

Set a password expiry date, the number of login retries, and the timeout when creating a user. The following example shows how to create a user with a password warning, a maximum number of days before the password expires, and login-retries set.

sudo useradd -m -K PASS_MAX_DAYS=5 -K PASS_WARN_AGE=3 -K LOGIN_RETRIES=3 test

format,png

Force Creation of a User Without a Home Folder

If the login.defs file has the CREATE_HOME yesoption set then when a user is created, a home folder is automatically created.

To create a user without a home folder regardless of the settings, use the following command:

sudo useradd -M test

format,png

It is confusing that -mstands for create home and -M stands for do not create home.

Specify the User's Full Name

As part of your user creation policy, you might choose to use the first initial followed by the last name. For example, the username for John Smith would be jsmith. When looking for details about a user, you might not then be able to distinguish between John Smith and Jenny Smith.

Add a comment when you create an account so it is easier to find the user's real name. Execute the following to add a comment:

sudo useradd -m smithj -c "John Smith"

format,png

Analyze the /etc/passwd File

When you create a user, the details of that user are added to the /etc/passwd file. To view the details about a particular user, use the grep command as follows:

grep smithj /etc/passwd

The above command returns details about all users with the word johnas part of the username.

format,png

The /etc/passwd file contains a colon-separated list of fields about each user. The fields are as follows:

Username

Encrypted password (which will always display as x)

Userid

User's group id

Full name of the user

User's home directory

Login shell

While many desktop Linux distributions provide a graphical tool for creating users, it is a good idea to learn how to do it from the command line so that you can transfer your skills from one distribution to another without learning new user interfaces.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值