Home > System Tutorial > LINUX > body text

Tutorial on using useradd and passwd commands in Linux system

PHPz
Release: 2024-06-12 22:36:26
Original
452 people have browsed it

useradd 和 passwd 命令在 Linux 系统中的使用教程

#useradd–d/usr/sam-msam

This command creates a user sam, in which the -d and -m options are used to form a home directory /usr/sam for the login name sam (/usr is the parent directory of the default user directory).

Assuming that the current user is sam, the following command changes the user's own password:

#passwd

Oldpassword:******

Newpassword:*******

Re-enternewpassword:*******

If you are a super user, you can specify the password of any user in the following way:

#passwdsam

Newpassword:*******

Re-enternewpassword:*******

>>Reference 1======================================

1. Add user

First use the adduser command to add a normal userLinux user joins sudo, the command is as follows:

#addusertommy//Add a user named tommy

#passwdtommy//Change password

Changingpasswordforusertommy.

NewUNIXpassword://Enter new password here

RetypenewUNIXpassword://Enter the new password again

passwd:allauthenticationtokensupdatedsuccessfully.

2. Grant root permissions

Method 1: Change the /etc/sudoers file, find the line below, and remove the comment (#) above

##Allowspeopleingroupwheeltorunallcommands

%wheelALL=(ALL)ALL

Change the user laterlinux user joins sudolinux shell to make it belong to the root group (wheel), the command is as follows:

#usermod-groottommy

After the change, you can now log in with the tommy account, and then use the command su- to obtain root permissions to operate.

Tip 2: Change the /etc/sudoers file, find the line below, and add a line under root, as shown below:

##Allowroottorunanycommandsanywhere

rootALL=(ALL)ALL

tommyALL=(ALL)ALL

After the change is completed, you can now log in with the tommy account, and then use the command su- to obtain root permissions for operation.

>>Reference 2======================================

Users added using useradd cannot directly use ssh for remote access and need to change ssh related configurations

as follows:

vi/etc/ssh/sshd_config

Add to

<code class="language-html hljs">AllowUsers root@192.168.1.32 admin
多个用户用空格隔开</code>
Copy after login

>>Reference 3======================================

How to allow ordinary users to obtain the permissions of the root user to perform operations without knowing the root user's password or switching to the root user? There is a command sudo that can achieve this function.

Sudo’s workflow:

(1) The root user edits the /etc/sudoers file and adds the ordinary user records to be allocated. There is a line like this: rootALL=(ALL)ALL. In front of this line, add: SamALL=(ALL)ALL

(2) When the sam user performs an operation, if the operation he performs does not have permission, he will go to the /etc/sudoers file to find whether there is a corresponding record. If there is, he will temporarily obtain root permissions and execute Operations that only root can perform.

Note: You will be prompted to enter a password here, and the password entered is the password of the sam user, not the password of the root user. Such as:

Shell>sudomkdir–pa/b/c/d

WetrustyouhavereceivedtheusuallecturefromthelocalSystem

Administrator.Itusuallyboilsdowntothesethreethings:

#1)Respecttheprivacyofothers.

#2)Thinkbeforeyoutype.

#3)Withgreatpowercomesgreatresponsibility.

Password:#The password here is the sam user's own password, not root's password

Several operations:

1) Edit the /etc/sudoers file and use the visudo command,

Shell>visudo

Although the visudo command also calls vi to edit the sudoer file, and will detect the sentence pattern of the file you changed when saving, if there is an error, it cannot be saved, and if you directly use vi to edit the file, it will be forced to save. Success, and if there are words

There is no prompt for method errors, which makes the previous sudo command unavailable.

2) Record analysis:

SamALL=(ALL)ALL

The host where the user account is logged in = (the identity that can be changed) the commands that can be issued

The above sentence means that user sam can log in anywhere and can switch to any user to perform any operation. If (ALL) is not written here, just write samALL=ALL, he can only switch to the root user by default.

>>Reference 4======================================

Interpretation of linuxchown command parameters and usage--Changing the owner of the file:

To modify the owner of the file program.c: chownjimprogram.c

User access rights for program.c now apply to jim. As the owner, jim can use the chmod command to grant or deny other users access to program.c.

To change the owner and group of all files in the directory /tmp/src to user john and group build: chown-Rjohn:build/tmp/src

For example: changing the owner of the tomcat installation directory (#chown-Ruser:group/usr/java/tomcat5.5)

>>Reference 5======================================

Add, delete, change, view users and user groups under Linux

1, create group

groupaddtest

Lower a test group

2, change group

groupmod-ntest2test

Change the name of the test group to test2

3, delete the group

groupdeltest2

Delete group test2

4, view group

a), check the group groups where the currently logged in user is, check the group where apacheuser is, groupsapacheuser

b), view all groups cat/etc/group

c), some Linux systems do not have the /etc/group file. In this case, please refer to the following tips

cat/etc/passwd|awk-F[:]‘{print$4}’|sort|uniq|getentgroup|awk-F[:]‘{print$1}’

A command used here is getent, which can search for group information through the group ID. If this command does not exist, it will be difficult to find all the groups in the system.

2. User operation

1, reduce user

View copy?

[root@krlcgcms01mytest]#useradd–help

Usage:useradd[options]LOGIN

Options:

-b,–base-dirBASE_DIR sets the base path as the user’s login directory

-c,–commentCOMMENTComments to users

-d,–home-dirHOME_DIR sets the user’s login directory

-D,–defaultsChange settings

-e,–expiredateEXPIRE_DATE Set the user’s expiration date

-f,–inactiveINACTIVE makes the password invalid after the user expires

-g,–gidGROUP makes the user only belong to a certain group

-G,–groupsGROUPS allows users to join a group

-h,–helpHelp

-k,--skelSKEL_DIR specifies other skel directories

-K,–keyKEY=VALUE overwrites the /etc/login.defs configuration file

-m,--create-home Manually create the login directory

-l, do not add users to the lastlog file

-M, do not manually create the login directory

-r, build system account

-o,–non-unique allows users to have the same UID

-p,–passwordPASSWORD Use encrypted password for new users

-s,–shellSHELLThe shell when logging in

-u,–uidUID Specify a UID for the new user

-Z,–selinux-userSEUSERuseaspecificSEUSERfortheSELinuxusermapping

[root@krlcgcms01mytest]#useradd--help

Usage:useradd[options]LOGIN

Options:

-b,--base-dirBASE_DIR sets the base path as the user's login directory

-c,--commentCOMMENTComments to users

-d,--home-dirHOME_DIR Set the user's login directory

-D,--defaultsChange settings

-e,--expiredateEXPIRE_DATE Set the user's expiration date

-f,--inactiveINACTIVE makes the password invalid after the user expires

-g,--gidGROUP makes the user only belong to a certain group

-G,--groupsGROUPS allows users to join a group

-h,--helpHelp

-k,--skelSKEL_DIR specifies other skel directories

-K,--keyKEY=VALUE overwrite the /etc/login.defs configuration file

-m,--create-home Manually create the login directory

-l, do not add users to the lastlog file

-M, do not manually create the login directory

-r, build system account

-o,--non-unique allows users to have the same UID

-p,--passwordPASSWORD Use encrypted password for new users

-s,--shellSHELLThe shell when logging in

-u,--uidUID Specify a UID for the new user

-Z,--selinux-userSEUSERuseaspecificSEUSERfortheSELinuxusermappinguseraddtest

passwdtest

Download user test, one thing to note is that after useradd drops a user, don’t forget to set a password for him, otherwise he will not be able to log in.

2, change user

usermod-d/home/test-Gtest2test

Change the login directory of the test user to /home/test and join the test2 group. Note that there is a big G here.

gpasswd-atesttest2 adds user test to test2 group

gpasswd-dtesttest2 removes user test from the test2 group

3, delete user

userdeltest

Delete test user

4, view user

a), view the current logged in user

[root@krlcgcms01~]#w

[root@krlcgcms01~]#who

b), check your username

[root@krlcgcms01~]#whoami

c), view single user information

[root@krlcgcms01~]#fingerapacheuser

[root@krlcgcms01~]#idapacheuser

d), view user login records

[root@krlcgcms01~]#lastView user records of successful login

[root@krlcgcms01~]#lastbView user records of unsuccessful logins

e), view all users

[root@krlcgcms01~]#cut-d:-f1/etc/passwd

[root@krlcgcms01~]#cat/etc/passwd|awk-F:‘{print$1}’

Linux basic operations

The above is the detailed content of Tutorial on using useradd and passwd commands in Linux system. For more information, please follow other related articles on the PHP Chinese website!

source:itcool.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!