Linux Commands - User, Rights Management

Users are an important part of the work of Unix/Linux systems, and user management includes the management of user and group accounts.

In Unix/Linux systems, no matter whether the system is logged in locally or remotely, each system must have an account and have different permissions for different system resources.

The root account in Unix/Linux systems is usually used for system maintenance and management, and it has unrestricted access to all parts of the Unix/Linux operating system.

During the Unix/Linux installation process, the system will automatically create many user accounts, and these default users are called "standard users".

In most versions of Unix/Linux, it is not recommended to log into the system directly with the root account.

<1> View the current user: whoami

whoami This command allows the user to view the username of the current account in the current system. You can view system user information through cat /etc/passwd.

Because system administrators usually need to use multiple identities to log in to the system, for example, they usually use ordinary users to log in to the system, and then use the su command to switch to the root identity for traditional management. At this time, you can use whoami to view the identity of the current user.

<2> View the logged in user: who

The who command is used to view the information of all users currently logged in to the system.

Common options:

Options meaning
-m or am I Display only the username, login terminal and login time of the who command was run
-q or --count Only display the user's login account and the number of logged in users
-u or --heading Show column headers

<3> Log out of the login account: exit

If it is a graphical interface, exit the current terminal;

If you are using ssh to log in remotely, log out of the login account;

If it is a logged-in user after switching, log out and return to the previous login account.

<4>Add user account: useradd

To add a user account in Unix/Linux, you can use the adduser or useradd command, because the adduser command is a link to the useradd command, so the format of these two commands is exactly the same.

The usage format of the useradd command is as follows: useradd [parameter] Create a new user account

parameter meaning
-d Specifies the home directory when the user logs in to the system. If this parameter is not used, the system will automatically create a home directory with the same name as the user name in the /home directory.
-m Create directory automatically
-g Specify group name

Related instructions:

  • Each Linux user must have a home directory. The home directory is the default current directory of the user (/home/user) when logging in to the system for the first time;
  • Each user must have a home directory, so when creating a user with useradd, be sure to specify a home directory for the user;
  • The user's home directory is generally placed in the home directory of the root directory, and the user's home directory and username are the same;
  • If you do not specify a group name when creating a user, the system will automatically create a group name that is the same as the user name.
Order meaning
useradd -d /home/abc abc -m Create abc user, if the /home/abc directory does not exist, this directory will be created automatically, and the user belongs to the abc group
useradd -d /home/a a -g test -m Create a user named a, the home directory is /home/a, if the home directory does not exist, the home directory will be created automatically, and the user belongs to the test group
cat /etc/passwd View the current user name of the system

<5> Set user password: passwd

In Unix/Linux, superusers can use the passwd command to set or modify user passwords for ordinary users. Users can also use this command directly to modify their own passwords without using the user name after the command.

<6> Delete user: userdel

Order meaning
userdel abc(username) Delete the abc user, but not automatically delete the user's home directory
userdel -r abc(username) Delete a user, also delete the user's home directory

<7> Switch user: su

You can use the su command to switch users, and "-" can be added after su. The difference between su and su -commands is that when su -switches to the corresponding user, the current working directory will be automatically converted to the user's home directory after the switch:

Note: If it is the ubuntu platform, you need to add "sudo" before the command. If some operations require an administrator to operate, ubuntu can operate without switching to the root user, just add "sudo". sudo is a tool under the ubuntu platform that allows system administrators to let ordinary users execute some or all of the root commands, which reduces the login and management time of root users and improves security.

Order meaning
his switch to root user
su root switch to root user
his - Switch to root user and switch directory to /root
su - root Switch to root user and switch directory to /root
su normal user 切换到普通用户
su - 普通用户 切换到普通用户,同时切换普通用户所在的目录

Ubuntu下切换到root的简单命令:

<8>查看有哪些用户组

方法一:

cat /etc/group

方法二:

groupmod +三次tab键

<9>添加、删除组账号:groupadd、groupdel

groupadd 新建组账号 groupdel 组账号 cat /etc/group 查看用户组

<10>修改用户所在组:usermod

使用方法:usermod -g 用户组 用户名

<11>查看用户在哪些组

<12>为创建的普通用户添加sudo权限

新创建的用户,默认不能sudo,需要进行一下操作

sudo usermod -a -G adm 用户名

sudo usermod -a -G sudo 用户名

<13>usermod -g 与 -G的区别

-g用来制定这个用户默认的用户组

-G一般配合'-a'来完成向其它组添加

<14>修改文件权限:chmod

chmod 修改文件权限有两种使用格式:字母法与数字法。

字母法:chmod u/g/o/a +/-/= rwx 文件

[ u/g/o/a ] 含义
u user 表示该文件的所有者
g group 表示与该文件的所有者属于同一组( group )者,即用户组
o other 表示其他以外的人
a all 表示这三者皆是
[ +-= ] 含义
+ 增加权限
- 撤销权限
= 设定权限
rwx 含义
r read 表示可读取,对于一个目录,如果没有r权限,那么就意味着不能通过ls查看这个目录的内容。
w write 表示可写入,对于一个目录,如果没有w权限,那么就意味着不能在目录下创建新的文件。
x excute 表示可执行,对于一个目录,如果没有x权限,那么就意味着不能通过cd进入这个目录。

如果需要同时进行设定拥有者、同组者以及其他人的权限,参考如下:

数字法:“rwx” 这些权限也可以用数字来代替

字母 说明
r 读取权限,数字代号为 "4"
w 写入权限,数字代号为 "2"
x 执行权限,数字代号为 "1"
- 不具任何权限,数字代号为 "0"

如执行:chmod u=rwx,g=rx,o=r filename 就等同于:chmod u=7,g=5,o=4 filename

chmod 751 file:

  • 文件所有者:读、写、执行权限
  • 同组用户:读、执行的权限
  • 其它用户:执行的权限

注意:如果想递归所有目录加上相同权限,需要加上参数“ -R ”。 如:chmod 777 test/ -R 递归 test 目录下所有文件加 777 权限

<15>修改文件所有者:chown

<16>修改文件所属组:chgrp

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324796038&siteId=291194637