Phase two Linux advanced programming: Linux advanced command three 022

File permission commands

learning target

  • Able to use the chmod command to complete the modification of file permissions

1. Introduction to the chmod command

command

Description

chmod

Modify file permissions

There are two ways to modify file permissions with chmod:

  • Alphabet
  • Digital method

2. The use of chmod alphabet

Role description:

Roles

Description

u

user, represents the owner of the file

g

group, which means user group

O

other, means other users

a

all, means all users

Permission setting instructions:

Operator

Description

+

Increase permissions

-

Revoke authority

=

Setting permissions

Permission description:

Authority

Description

r

Readable

w

Writable

x

Executable

-

No permission

chmod command letter method renderings:

Phase two Linux advanced programming: Linux advanced command three 022

 

The chmod command sets the renderings of multiple characters at the same time:

Phase two Linux advanced programming: Linux advanced command three 022

 

3. The use of chmod digital method

The digital law is "rwx". These permissions can also be replaced by numbers

Permission description:

Authority

Description

r

Readable, the permission value is 4

 

 

w

Writable, permission value is 2

x

Executable, the permission value is 1

-

No permission, permission value is 0

chmod command digital method renderings:

Phase two Linux advanced programming: Linux advanced command three 022

 

4. Summary

  • Use the  chmod  command to control file operation permissions.
  • Letter format: chmod permissions files set by different roles
  • Digital format: chmod the file name of the permission value of different roles

Relevant commands for obtaining administrator rights

learning target

  • Be able to know the command to switch to the admin user

1. Use of sudo command

command

Description

sudo -s

Switch to root user and get administrator privileges

sudo

The execution of a command needs to obtain administrator privileges. You can add sudo before the execution command.

sudo -s renderings:

Phase two Linux advanced programming: Linux advanced command three 022

 

sudo command effect diagram:

Phase two Linux advanced programming: Linux advanced command three 022

 

Description:

  • If you only need to use administrator privileges for a certain operation, it is recommended to use sudo, that is, temporarily use manager privileges.
  • If a large number of operations require the use of administrator privileges sudo -s, but the operation needs to be cautious.

2. The use of whoami command

command

Description

whoami

View current user limits

Whoami command effect diagram:

Phase two Linux advanced programming: Linux advanced command three 022

 

3. Use of exit command

command

Description

exit

Log out user

Effect diagram of exit command:

Phase two Linux advanced programming: Linux advanced command three 022

 

Description:

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

4. Use of who command

command

Description

who

View all logged-in users

The rendering of the who command:

Phase two Linux advanced programming: Linux advanced command three 022

 

5. Use of passwd command

command

Description

passwd

修改用户密码,不指定用户默认修改当前登录用户密码

passwd 命令的效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

6. which命令的使用

命令

说明

which

查看命令位置

which 命令的效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

7. 关机和重启命令的使用

命令

说明

shutdown –h now

立刻关机

reboot

重启

8. 小结

  • sudo 是临时获取管理员权限
  • sudo -s 是切换到管理员用户,一直使用管理员权限,需要谨慎操作。
  • exit 是退出当前用户
  • passwd 默认修改当前用户密码

用户相关操作

学习目标

  • 能够知道创建用户的命令

1. 创建用户

命令

说明

useradd

创建(添加)用户

useradd命令选项:

选项

说明

-m

自动创建用户主目录,主目录的名字就是用户名

-g

指定用户所属的用户组,默认不指定会自动创建一个同名的用户组

创建用户效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

查看所有用户信息的文件效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

Phase two Linux advanced programming: Linux advanced command three 022

 

说明:

  • useradd 命令的使用需要使用管理员权限,前面需要加上 sudo
  • 创建用户如果不指定用户组,默认会自动创建一个同名的用户组
  • 查看用户是否创建成功,可以查看/etc/passwd这个文件
  • 查看用户组是否创建成功,可以查看/etc/group这个文件

passwd文件中的每项信息说明,以root:x:0:0:root:/root:/bin/bash为例:

  • 第一个:用户名
  • 第二个:密码占位符
  • 第三个:uid, 用户id
  • 第四个:gid, 用户所在组id
  • 第五个:用户描述, 可选,
  • 第六个:用户的主目录所在位置
  • 第七个:用户所用 shell 的类型,一般由bash或者sh,默认不设置是sh类型

group文件中的每项信息说明, 以laowang:x:1001:为例:

  • 第一个:用户组名
  • 第二个:用户组密码占位符,一般Linux系统的用户组都没有密码的
  • 第三个:组id

id命令查看用户信息:

命令

说明

id

查看用户信息

id命令效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

每项信息说明:

uid=1001(laowang) gid=1001(laowang) 组=1001(laowang)

  • 第一个: uid 表示用户id
  • 第二个: gid 表示用户组id
  • 第三个: 组 表示用户所在的用户组

2. 设置密码

给其它用户设置密码,需要使用: sudo passwd 用户名

设置密码效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

3. 切换用户

命令

说明

su

切换用户

语法格式: su - 用户名

切换用户效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

在laowang用户使用sudo -s效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

解决办法:

  • 给laowang用户设置一个sudo附加组, 需要使用usermod修改用户信息

4. 修改用户信息

命令

说明

usermod

修改用户信息

usermod选项:

命令

说明

-G

设置一个附加组

-g

修改用户组

usermod命令效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

设置附加组sudo以后,在sudo -s切换到管理员用户效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

5. 扩展: 删除附加组

命令

说明

gpasswd

添加和删除附加组信息

gpasswd命令选项:

选项

说明

-a 用户名

给用户添加附加组

-d 用户名

给用户删除附加组

删除附加组效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

删除附加组再次使用 sudo -s 效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

5. 删除用户

命令

说明

userdel

删除用户

userdel命令选项:

选项

说明

-r 用户名

删除用户主目录,必须要设置,否则用户主目录不会删除

删除用户效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

id查看用户信息效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

查看group文件信息效果图:

Phase two Linux advanced programming: Linux advanced command three 022

 

说明:

  • 删除用户,默认同名的用户组也会被删除

6. 小结

  • 创建用户命令格式: sudo useradd -m 用户名, 默认会创建一个同名的用户组。
  • 查看用户信息使用 id 命令或者 /etc/passwd文件
  • 查看用户组信息使用 /etc/group文件
  • 给用户设置密码使用 sudo passwd 用户名
  • Switch user using  su-username
  • Set up additional groups using  sudo usermod -G  group name username
  • To delete a user, use  sudo userdel -r user name , the user group with the same name will be deleted by default.

User group related operations

learning target

  • Be able to know the command to create a user group

1. Create User Group

command

Description

groupadd

Create (add) user group

Create user group renderings:

Phase two Linux advanced programming: Linux advanced command three 022

 

2. Create users and specify user groups

Create user and specify user group renderings:

Phase two Linux advanced programming: Linux advanced command three 022

 

3. Modify User Group

Modify the user group renderings:

Phase two Linux advanced programming: Linux advanced command three 022

 

4. Delete User Group

command

Description

groupdel

Delete user group

Delete user group renderings:

Phase two Linux advanced programming: Linux advanced command three 022

 

Description:

  • If there are users under the user group, delete the user first, delete the user group

5. Summary

  • Create a user group using:  sudo groupadd user group name
  • Create a user and specify the user group to use:  sudo useradd -m -g user group username
  • Modify user group use:  sudo usermod -g user group username
  • Delete user group use:  sudo groupdel user group name
  • A set of materials for programming learning is prepared for you, as well as the corresponding codes. Video tutorials are available for you. You can’t send them online. You can get it by adding the deduction group 1135960211

Guess you like

Origin blog.csdn.net/bobin666/article/details/115176558