What we’ll cover in this article is adding and deleting users, as well as modifying exiting users. We’ll then focus on groups and how to add/delete them. I will also point out key files that are associated with this process for those of you that are new to Linux or are looking to pass some type of certification.shell
I know you didn’t ask, but before we get into the main course of this article I want to introduce two files that we will be using as examples. In the /etcdirectory, the passwd & the group files hold all of the users and group information. These files are essential when logging on to the system. Anytime you add a user, that user is added to the passwd file. Let’s take a look at/etc/passwd first.app
When you add a user to the system, that user is placed into the passwd file.less
Issue command: less /etc/passwdthis
Use the arrows keys to go up and down and “q” to exit.spa
You can edit the file directly or use the commands we will go over shortly. I recommend using the commands especially if you are a beginner. You do not want to corrupt the passwd file.unix
Let’s take a look at the group file:code
The /etc/group file holds all of the group information as well as the users belonging to each group. The structure is very similar to that of/etc/password.orm
Again, these files are vital to the system and you will need to know them if you are taking any Linux exams.ci
若是用 usermod -g 组名 用户名 , 是修改用户的所在组,是覆盖性的。rem
若是要加入多个组,应该用: usermod -G 组名,组名,组名.....+空格+用户名!
Adding a user is easy. The command used is: useradd “name of the user”
Note – You must be logged-in as root to add, delete, and modify users. It is not recommended to stay logged-in as root other than when necessary and only certain members should have root access.
Example:
说明:添加 ftpuser 用户组为root 组 -d 表明用户登陆后的主目录 -s:指定用户登入后所使用的shell -M:不要自动创建用户的登入目录。
You can then use “su” plus the name of the user you just added to logon. “exit” will take you out.
The command for deleting a user is “userdel”.
These commands are very basic, but there are other options we have as well. Options:
-d sets home directory for the user (if other than the default which is: /home/”user’s name”)
-m creates the home directory
Using the –d option on its own will only set the home directory for the user, but does not create it.
You can see I confirm this by “echo $HOME” which tells me my home directory and I use “ls” to confirm.
Adding the –m option will create the directory.
If you just add the user, default directory is /home/”users name” and you can just use the –m to create.
Lastly, using the “-r” option along with userdel will delete the user as well as the home directory.
If you are logged in as root, the command is “username” password.
Example: passwd roman
If you are logged on as the user, the command is “passwd”.
Let’s say we want to add roman to the group accounting. “-g” is used to change the user’s primary group.
Command is: useradd –g accounting roman
I then ran the grep command to confirm.
However, say I want to add roman to the group accounting and make his primary group sales. We can add the “-G” option (other groups).
“-G” basically says add this user to a new group, but keep them in the old one (append).
Then issue command “id roman” – to confirm.
We can use “-G” on its own to add a user to another group.
Note: The groups must exit before we can add users to them.
If a user is created and you just want to add that user to a group, or change the home directory, etc:
Example: usermod -Gmanagement roman
Or you can change the home directory for the user:
Example: usermod –d/home/newfolder roman
The command for adding groups is “groupadd” or “groupdel”.
You can confirm by checking the /group/etc file.
Example: grep software /etc/group or cat /etc/group
The “groupdel” command will remove the group entirely.
There are a number of options you have when creating users and groups. Again, you could just go into /etc/passwd directly and add a user there, but unless you are familiar with file editors and putting a lock on, you should work with the commands. We will go over alternate methods in the Vi section.
Commands: useradd, userdel, usermod, groupadd, groupdel
Options
-d change user’s home directory
-m create directory
-s used to change the default shell
-r remove home directory when deleting user
“Passwd” will change the user’s password
新建Linux用户却没法登陆的缘由,多是第三部没有赋权限。
备注:新建Linux用户而且能够登陆三步走:
一、sudo mkdir -p /home/irene 创建目录 二、sudo useradd irene -d /home/irene 创建用户和用户所在目录 ###对于hp unix 系统 登陆名是放在后面的。以下 useradd -d /home/irene -g group irene 三、sudo chown -R irene:irene /home/irene 给用户目录赋权限