Linux system configuration and service management-02-file and user management

Linux system configuration and service management-02-file and user management

file management

1. Linux directory structure

WINDOWS/LINUX directory comparison

  • Windows: Organize files in a multi-root manner C:\ D:\ E:\
  • Linux: Organize files as a single root /

Introduction

  • Directory structure: FSH (Filesystem Hierarchy Standard)
 [root@localhost ~]# ls /
bin dev lib media net root srv usr
boot etc lib64 misc opt sbin sys var
cgroup home lost+found mnt proc selinux tmp

insert image description here

  • Directory function (understand)
bin 普通用户使用的命令 /bin/ls, /bin/date

sbin 管理员使用的命令 /sbin/service 

dev 设备文件 /dev/sda,/dev/sda1

root root用户的HOME

home 存储普通用户家目录 

tmp 临时文件(全局可写:进程产生的临时文件) 

var 存放的是一些变化文件,比如数据库,日志,邮件....

==设备(主要指存储设备)挂载目录==
media 移动设备默认的挂载点

mnt 手工挂载设备的挂载点

etc 配置文件(系统相关如网络/etc/sysconfig/network

proc 虚拟的文件系统,反映出来的是内核,进程信息或实时状态 ,硬件的状态

usr 系统文件,相当于C:\Windows

/usr/local 软件安装的目录,相当于C:\Program

boot 存放的系统启动相关的文件,例如kernel,grub(引导装载程序)

lib 库文件Glibc

lib64 库文件Glibc

lost+found  fsck修复时,存储没有链接的文件或目录

2. File management

1. File management commands

  • 1. Create a file
- 语法
		命令 空格  文件名.后缀
		touch    文件名字
	
- 示例1
		创建一个文件
		[root@localhost ~]#  touch      file1.txt
	
		查看文件
		[root@localhost ~]#  ls
  • 2. Create a directory
- 语法
	mkdir   空格   路径和目录名
	选项
	 	mkdir  -p    # 父系,当创建目录没有上一级时,自动创建
	    mkdir -v     # verbose,显示创建过程
	    mkdir /home/a{1,2}  # 创建a1和a2   逗号
		mkdir /home/a{1..3}  # 创建从a1到a3    两个点
- 示例1
	 创建一个目录
	    [root@localhost ~]#  mkdir      dir1
	 查看目录
	    [root@localhost ~]#  ls
  • 3. copy
- 语法
		- 命令   参数1   参数2
			- cp   源文件路径     目标文件夹   # 复制文件
		- 选项
			- cp  -r    源目录    目标目录  # recursion 递归复制整个目录

	- 示例1
		- [root@localhost ~]#cp  file1.txt   dir1/
		- [root@localhost ~]#ls   dir1/
  • 4. Move
- 语法
	- 命令  参数1   参数2
	- mv    源文件路径   目标文件路径

- 示例1
	 [root@localhost ~]#mv   file3.txt     dir1/
	 [root@localhost ~]#ls
  • 5. delete
- 语法
	- rm -rf    文件或目录的路径
	- rm -rf /tmp/tt1/*  不会删除tt1目录中的隐藏文件,即 . 开头的文件

- 示例
	- [root@localhost ~]#rm   -rf    file5.txt  file7.txt
	- [root@localhost ~]#ls
  • 6. View file content
- cat全部
	cat  /root/file1.txt

- more翻页
	more  /root/file1.txt

- head头部
	head   /root/file1.txt

- tail尾部
	-tail      /root/file1.txt

- grep过滤关键字
	- 针对文件内容进行过滤
	- 语法:grep   关键字   文件名
		   grep      'abc'    /root/file1.txt
  • 7. Modify the file content
    File editor
    command: vi, vim
    The three modes of vi
    insert image description here
    VISUAL visual mode, press V to enter, the small v cursor to select by yourself, the big V to directly select a line, Y to copy and p to paste, do not press twice y, press y once in visual mode. Press d to delete the selected
    ctrl+V, select up and down, and then shift+> for overall indentation.

command mode

光标定位 

hjkL              //上下左右
0 $               //行首行尾
gg G 			//页首页尾
3G 进入第三行    :3  也可以
/string (n N 可以循环的)     //查找字符,按n键选下一个(重要)N选上一个

Text Editor

yy 复制
dd 删除
p 粘贴
u undo撤销

enter other mode

a 进入插入模式
i 进入插入模式
o 进入插入模式
A 进入插入模式

: 进入末行模式(扩展命令模式)
v 进入可视模式
ESC 返回命令模式

Extended Command Mode

保存退出
:w 保存 
:q 退出 
:wq 保存并退出.  强制性写入文件并退出. 即使文件没有被修改也强制写入, 并更新文件的修改时间.
:x 写入文件并退出. 仅当文件被修改时才写入, 并更新文件修改时间, 否则不会更新文件修改时间.

查找替换
:范围    s/原内容/新内容/全局 
:1,5    s/root/qianfeng/g    #从1-5行的root 替换为qianfeng    s:switch  交换    g:global全局

另存为
:w file9.txt     #另存为 file9.txt

:set nu   # 设置行号 
:set nonu   # 取消设置行号 
:set list  # 显示控制字符

redirect

ls -a > list.txt   # 覆盖文件内容
ls -a >> list.txt  # 追加到原文后

Temporary files: Temporary files generated when vim is not closed normally. If the source file is 1.txt, the temporary file is .1.txt.swap, in the same directory, just delete the file

  • 8. Change directory
cd    绝对路径

ls   列出目录当中的内容
举例说明
ls   /
- 路径分为
	- 绝对:从根开始描述路径。
	- 相对
	- 当前

		- ./
		- 不输入任何路径

- 上一级
	- ../

shell explanation
insert image description here

2. File type (understand)

  • type
- 常见类型
	- :普通文件(文本文件,二进制文件,压缩文件,电影,图片。。。)
	d: 目录文件(蓝色)

- 非常见类型
	b 设备文件(块设备)存储设备硬盘,U盘 /dev/sda, /dev/sda1
	c 设备文件(字符设备)打印机,终端 /dev/tty1
	l 链接文件(淡蓝色)
	s 套接字文件
	p 管道文件
  • example
查看不同的文件类型。

[root@localhost ~]# ll -d   /bin/ls    /dec/sda    /home
-rwxr-xr-x. 1 root root 117616 Nov 20 2018 /bin/ls
brw-rw---- 1 root disk 8, 0 Mar 14 09:03 /dev/sda
drwxr-xr-x. 10 root root 4096 Mar 14 11:00 /home
  • Notice
1.通过颜色判断文件的类型是不一定正确的!!!
2.Linux系统中文件是没有扩展名!!!

User Management

1. Basic concept of user/group

concept

  • Users and groups:
    . Every process (running program) on the system runs as a particular user.
    . Every file is owned by a particular user.
    . Access to files and directories are restricted by user.
    . The user associated with a running process determines the files and directories accessible to that process.
  • User's role
- 查看当前登录的用户信息:

   [root@localhost ~]# id
   uid=0(root) gid=0(root) groups=0(root)

- 查看文件的owner:

   [root@localhost ~]# ll /home/ 
   drwx------. 4 alice alice 4096 Jun 2 15:59 alice
   drwx------. 2 root root 16384 Jun 1 23:09 lost+found
   drwxr-xr-x 2 root root 4096 Jun 2 15:33 qianfeng

- 查看运行进程的username:

  [root@localhost ~]# ps aux 
  USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
  root 1 0.0 0.0 19360 1536 ? Ss 13:29 0:00 /sbin/init
  root 2 0.0 0.0 0 0 ? S 13:29 0:00 [kthreadd]
  root 3 0.0 0.0 0 0 ? S 13:29 0:00 [migration/0]
  root 4 0.0 0.0 0 0 ? S 13:29 0:00 [ksoftirqd/0]

Files where user group information is stored

  • 1. User basic information file
/etc/passwd(冒号分割为7列字段)
      root:x:0:0:root:/root:/bin/bash
      用户名:x:uid:gid:描述:HOME:shell

      - root:用户名:登录系统的名字
      - X:密码占位符:,具体内容不在这里
      - 0:UID: 用户的身份证号

	        - 系统约定: RHEL7
	          uid: 0 特权用户
	          uid: 1~999 系统用户
	          uid: 1000+ 普通用户

				The root user
				. uid is 0
				. all power
				. This user has the power to override normal privileges on the file system
				. installing or removing software and to manage system files and directorie
				. Most devices can only be controlled by root

	- 0:GID:GROUP 组号

		- 每创建一个用户,系统会自动创建同名的组

	- root:描述:比如经理 manager

		- 默认是用户名

	- /root:家目录:登录系统时,所在目录
	- /bin/bash:登录shell:命令解释器,当今用的组多的

- 不能用vim的方式改这个文件,主要是来观察
  • 2. User password information file
/etc/shadow(9列)

root:$1$MYG2N:15636:0:99999:7:   :   :
- 加密算法$id$salt$encrypted
	$1: MD5
	$5: SHA-256
	$6: SHA-512

root:$1$MYG2NDG6$a1wtyr5GDM2esAPjug0YP0:15636:0:99999:7:: :

1)“登录名”是与/etc/passwd文件中的登录名相一致的用户账号
2)“口令”字段存放的是加密后的用户口令字,如果为空,则对应用户没有口令,登录时不需要口令;   
	星号*代表帐号被锁定;
	双叹号!!表示这个密码已经过期了。
	$6$开头的,表明是用SHA-512加密的,
	$1$ 表明是用MD5加密的
	$2$ 是用Blowfish加密的
	$5$ 是用 SHA-256加密的。 

3)“最后一次修改时间”表示的是从某个时刻起,到用户最后一次修改口令时的天数。时间起点对不同的系统可能不一样。例如在SCOLinux中,这个时间起点是1970年1月1日。
4)“最小时间间隔”指的是两次修改口令之间所需的最小天数。0表示随时可改密码,如果是5表示5天才能修改一次密码
5)“最大时间间隔”指的是口令保持有效的最大天数。99999表示永不过期
6)“警告时间”字段表示的是从系统开始警告用户到用户密码正式失效之间的天数。假如设置7,表示还有7天到期时会警告是否要修改。软限制。
7)“不活动时间”表示的是用户没有登录活动但账号仍能保持有效的最大天数。(软限制。到期后多少天就不能用账号了。)可不写。如写28表示代表用户不登录系统,会保留28天,过了28天禁用该用户。只要活跃,就不会禁用
8)“失效时间”字段给出的是一个绝对的天数,如果使用了这个字段,那么就给出相应账号的生存期。期满后,该账号就不再是一个合法的账号,也就不能再用来登录了。(硬限制。)
9) 保留
  • 3. Group information file
/etc/group 

root:x:0:
组名:组密码:组ID:组成员

组成员默认为空

2. User/group management**

user

  • CREATE USER WITHOUT OPTIONS SPECIFIED
[root@localhost ~]# useradd user01 

[root@localhost ~]# id  user01  查看用户,不带参数默认是当前用户
uid=1001(user01) gid=1001(user01) 组=1001(user01)
用户编号=具体数字(用户名)  组编号=具体数字(组名)  组=组ID(成员名字)
小结:
如果创建一个用户时,未指定任何选项,系统会创建一个和用户名相同的组作为用户的Primary Group.

从/etc/passwd中查看用户信息
	grep user01 /etc/passwd  
	cat /etc/passwd | grep user01
  • Create user-specified options
创建用户user02,指定uid
 [root@localhost ~]# useradd user02 -u 1503

创建用户user03 指定家目录
 [root@localhost ~]# useradd   user03     -d     /user03
  • delete users
[root@localhost ~]# userdel -r user02   # -r 表示删除该用户的家目录,即/home/user02
  • Modify user password
方法一:root修改其他用户(alice)密码

	[root@localhost ~]# passwd alice

方法二:用户(zhuzhu)登录,自己修改密码。

    [zhuzhu@localhost ~]$ passwd
  • Other option management
[root@localhost ~]# usermod -s /sbin/nologin   user02   # 修改用户信息,此处是指定user02的shell禁止登录,执行完毕/etc/passwd中会做修改
  • Group Membership Management
- 查看用户原先信息

	  [root@localhost ~]# id user02
	  uid=1002(user02) gid=1002(user02) 组=1002(user02)

- 将用户追加到hr组(即用户有多个附加组)

	- 语法

		- usermod    -aG  组名  用户名
		注:当不带选项 a 时,会修改附加组而非追加,无论原来有几个附加组,都会被重置为此时指定的唯一附加组
	- [root@localhost ~]# usermod -aG hr user02 

- 再次查看用户信息

	- [root@localhost ~]# id user02
	- uid=1002(user02) gid=1002(user02) 组=1002(user02),1005(hr)

- 将用户移除组(扩展)

	- 语法

		- gpasswd   -d    user02    hr

user group:

operate
  • Create a hr group
- [root@localhost ~]# groupadd hr
- 查看

	- [root@localhost ~]# tail -3 /etc/group
	- user03:x:1003:
	- user04:x:1004:
	- hr:x:1005:
	- 说明该组已经 创建,但和任何用户都没有关系。

- 组名不能只有数字,最好只有字母
  • Create group net01 and specify gid 1007
- [root@localhost ~]# groupadd net01 -g   1007
- 查看
	- [root@localhost ~]# grep 'net01' /etc/group 
	- //查看/etc/group中组net01信息
  • delete group
- [root@localhost ~]# groupdel net01 
- 查看

	- [root@localhost ~]# tail -3 /etc/group
	- user03:x:1003:
	- user04:x:1004:
	- hr:x:1005:
Classification
  • basic group
随用户创建,自动创建的同名组,只有一个。从/etc/passwd查看最直接

useradd -g 指定基本组
usermod -g 修改用户基本组
基本组可以改变,多个用户的基本组可以相同
  • Additional group
- 用户加入的其他组。可以有多个
useradd -G 指定附加组

The concepts of basic group and additional group are relative to users, such as user Zhang San, Zhang San’s group is the basic group, when he joins Wang Wu’s group, Wang Wu’s group is an additional group for Zhang San

3. Escalation of rights (understanding)

Permanent privilege escalation Switching users with su

When switching users, the other party's password is required when switching the other party's identity

[user01@localhost ~]$ id user01
uid=1003(user01) gid=1003(user01) 组=1003(user01)
[user01@localhost ~]$ whoami
user01
[alice@localhost ~]$ useradd u1
bash: /usr/sbin/useradd: 权限不够

[alice@localhost ~]$ su - root
password:
[root@localhost ~]# useradd u1

- 号的作用:带和不带 结果一致。最好带上 ,带 - 会切换环境变量,不带则不会切换环境变量

Temporary privilege escalation Running commands as root with sudo

  • Introduction to sudo
- 将当前用户切换到超级用户下
- 然后以超级用户身份执行命令,执行完成后,直接退回到当前用户。可以完成部分特权指令,但不是所有命令都可以执行
- 具体工作过程如下:
	- 当用户执行sudo时,系统会主动寻找 /etc/sudoers 文件,判断该用户是否有执行sudo的权限
	- -->确认用户具有可执行sudo的权限后,让用户输入用户自己的密码确认
	- -->若密码输入成功,则开始执行sudo后续的命令
  • sudo configuration file syntax
- ##语法
- ##      user    MACHINE=COMMANDS
- ##       用户 登录的主机=(可以变换的身份) 可以执行的命令 
- wheel是一个用户组,系统装机时准备的特权组
- 在该配置文件中配置了该用户或该用户所在的组,然后才能使用sudo命令,否则使用sudo命令时会提示xxx is not in the sudoers file
  • Target
- 创建普通用户hehe,使其具有sudo特权
  • Example 1
- 观察授权信息

	- 以root身份,授权普通用户hehe
	- [root@localhost ~]# vim /etc/sudoers

		- 系统的授权文件

	- %wheel         ALL=(ALL)             ALL
	- 允许wheel用户组       不输入密码         使用所有命令

- 将用户加入到组

	- 创建hehe账号并加入组

		- [root@localhost ~]# useradd hehe -G wheel

	- 查看hehe信息

		- [root@localhost ~]# id hehe
		- uid=1001(hehe) gid=1001(hehe) 组=1001(hehe),10(wheel)

	- 如果用户没有加入wheel这个已经创建的组,使用sudo命令时会说  xxx is not in the sudoers file。这里wheel组只是为演示说明加入该组,也可以配置其他组,或单独配置某个用户。

- 切换用户登录

	- 用户需要密码

		- passwd hehe

	- 切换用户hehe登陆

		- 该用户一定要注销

- 使用sudo提权
	- [hehe@localhost ~]$ useradd gougou10
	- -bash: /usr/sbin/useradd: 权限不够
	- [hehe@localhost ~]$ sudo useradd gougou10
	- [hehe@localhost ~]$ id gougou10
	- uid=1002(gougou10) gid=1002(gougou10) 组=1002(gougou10)

Guess you like

Origin blog.csdn.net/HYESC/article/details/127358983