RHCSA阶段笔记

命令终端字段含义介绍

  • [root@localhost ~]#
  • 解释:
  • root:当前登录系统用户名(root超级管理员)
  • localhost :当前主机名
  • :当前用户所在目录( 为家目录) ,root超级管理员家目录:/root
  • : 当前用户身份是超级管理员
  • [student@localhost ~]$
  • $:当前用户身份为普通用户,普通用户的家目录:/home/用户名同名

Linux系统基本概念

  • 多用户系统:允许多个用户同时登录系统并使用系统中的资源
    [En]

    Multi-user system: allows multiple users to log in to the system at the same time and use the resources in the system*

  • 多任务系统:允许同时执行多个任务
    [En]

    multitasking system: allows multiple tasks to be executed simultaneously*

  • 严格区分大小写:命令、选项、参数、文件名和目录名严格区分大小写
    [En]

    strictly case-sensitive: commands, options, parameters, file names and directory names are strictly case-sensitive*

  • 一切皆文件:硬件设备(内存、CPU、网卡、显示器、硬盘等等)都是以文件的形式存在的
  • 不管是文件还是目录都是以倒挂的树形结构,存在于系统的”/”根目录下,根目录是Linux系统的起点
  • 对于Linux系统而言,目录/文件没有扩展名一说,扩展名如:.sh(脚本文件) .conf(配置文件) .log(日志文件) .rpm(软件包).tar(压缩包)是易于用户方便识别
  • 没有提示是最好的提示(成功)
    [En]

    No hint is the best hint (successful)*

  • Linux系统没有回收站

命令行编辑技巧

键盘上下键调出历史命令

Ctrl + c:废弃当前命令行中的命令,取消当前执行的命令,例如ping

Ctrl + l,clear:清屏

tab建自动补齐:可补齐命令、参数、文件路径、软件名

esc + . :将上一条命令参数变成当前命令的执行对象

Ctrl + a:将当前光标移动至行首

Ctrl + e:将当前光标移动至行尾

Ctrl + u 清空至行首

Ctrl + w 删除一个单词

exit,logout:退出系统

命令行一般命令格式

  • 命令字 [-选项…] [参数…]
  • 命令字:命令本身(功能)
  • 选项:调整命令功能的
    • 短选项:-l -a -d -h(单个字符),短选项可以合并使用:-lad -lh
    • 长选项:–help(单词),长选项通常是不能合并使用的
  • 参数:命令的执行对象,文件/目录/程序等
  • []:可选的
  • …:可以同时有多个选项或参数

学习方法

  • 遇到问题:不要求你有早期犯错的能力
    [En]

    encounter problems: you are not required to have the ability to make mistakes in the early stage.*

  • 思考自己能不能决绝:百度、Google、最后在问老师
  • 主动学习爱好,而不是被动学习
    [En]

    active learning hobbies, not passive learning*

  • 不死守技术点,低头读书别忘了抬头看路
    [En]

    Don’t cling to a technical point, and don’t forget to look up at the road when you look down to study.*

Linux系统辨别目录与文件的方法

蓝色表示目录(windows系统里的文件夹)

白色表示文件

浅蓝色表示链接文件(类似于windows系统的快捷方式)

绿色表示可执行文件(如脚本、命令程序文件)

[En]

Green indicates executable files (such as scripts, command program files)

红色表示压缩文件

黄色表示设备文件(硬盘、键盘、鼠标、网卡、CPU硬件设备都是以文件的形式存在的)

红色闪动文件——>表示链接文件不可用

ls 查看目录/文件命令

  • ls命令(英文全拼:list):用于查看目录下内容及目录和文件详细属性信息
  • 命令格式:ls [-选项…] [参数…]
  • 常用选项:
  • -a 显示目录下所有内容,包含隐藏的内容
  • -l 以长格式显示目录下的内容及详细属性
  • -h 人性化显示目录下内容大小(kB、MB、GB)
  • -d 仅显示目录本身而不显示目录下的内容
  • -i 查看inode号(系统任何的文件或目录都有一个唯一的编号)
  • -R:递归查看目录下所有内容(从头到尾)
  • 注(补充):递归是指所有目录从头到尾呈现。
    [En]

    Note (additional): recursion means that all directories are presented from beginning to end.*

Linux 系统文件类型

  • 文件:

d 目录:

l 链接文件

b 跨设备文件

c 字符设备文件

p 管道设备文件

s 套接字

Linux 系统下的归属关系

  • 在Linux系统下,文件给用户分成了三类
  • 所有者:文件或目录的拥有者,拥有者的权限通常是最大的
  • 所属组:文件或目录属于哪一个组,所属组的权限略微比所有者小
  • 其他人:既不是文件或目录的所有者,也不属于文件或目录组内的成员,其他人的权限通常最小的权限
  • ls命令示例:
#显示当前所在目录下的所有内容
[root@localhost ~]# ls

#查看根目录下所有内容
[root@localhost ~]# ls   /
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

#查看/etc目录下所有内容
[root@localhost ~]# ls /etc

#查看/bin目录下所有内容
[root@localhost ~]# ls /bin

#查看/dev目录下所有内容
[root@localhost ~]# ls /dev

#查看目录下所有目录和文件,包括隐藏的内容
[root@localhost ~]# ls -a

#以长格式显示目录下所有内容,包括详细的属性信息
[root@localhost ~]# ls -l
-rw-r--r--. 1 root root 0 10月 24 15:16 hello

#解释
-:文件类型
1:代表文件的引用次数
root:文件的所有者
root:文件的所属组
0:文件的大小,默认以字节为单位显示大小
10月 24 15:16:文件最近一次的修改时间
hello:文件名

#以长格式显示目录所有内容,以人性化的方式显示详细的属性信息
[root@localhost ~]# ls -l -h

#短选项合并使用
[root@localhost ~]# ls -lh

#以长格式显示目录所有内容,以人性化的方式显示详细的属性信息,包括隐藏的内容
[root@localhost ~]# ls -lha

#以长格式显示根目录下所有内容,包括详细的属性信息
[root@localhost ~]# ls -l /
lrwxrwxrwx.   1 root root    7 3月  13 17:15 bin -> usr/bin

#创建hello.txt文件
[root@localhost ~]# touch hello.txt

#查看文件的元数据信息
[root@localhost ~]# stat hello.txt
  文件:"hello.txt"
  大小:0          块:0          IO 块:4096   普通空文件
设备:fd00h/64768d Inode:33575020    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-03-14 16:38:14.349861770 +0800
最近更改:2021-03-14 16:38:14.349861770 +0800
最近改动:2021-03-14 16:38:14.349861770 +0800
创建时间:-

Linux 基本权限的类别

  • r 读取 w 写入 x 执行 – 没有权限
  • 权限顺序:rwx rwx rwx
[root@localhost ~]# ls -l
-rw-r--r--. 1 root root 1831 3月  13 17:45 initial-setup-ks.cfg
#解释
-:文件类型
rw- r-- r--:所有者u、所属组g、其他人o的权限
u   g   o

r 读取权限,w写入权限,x执行权限,-没有任何权限

1:代表文件的引用次数,只针对与做了硬连接的文件才有效
root:文件的所有者
root:文件的所属组
1831:文件的大小,默认以字节为单位显示大小
3月  13 17:45:文件最近一次的修改时间
initial-setup-ks.cfg:文件名

#查看/root目录本身详细属性信息
[root@localhost ~]# ls -ld /root
dr-xr-x---. 14 root root 4096 3月  14 16:38 /root

#查看当前目录下所有内容的inode号
[root@localhost ~]# ls -i
33574979 anaconda-ks.cfg  33574984 initial-setup-ks.cfg  33575035 模板  33575036 图片  17470701 下载            17470702 音乐
33575020 hello.txt        51909391 公共                  51909392 视频   3204374 文档  33575017 新建文件夹.zip   3204373 桌面

#查看hello.txt文件的inode号
[root@localhost ~]# ls -i hello.txt
33575020 hello.txt

#查看/etc/目录本身的inode号
[root@localhost ~]# ls -id /etc
16777281 /etc

课后练习

1.命令行以$作为结尾代表什么含义?

普通用户

2.请写出Linux系统一般的命令格式?

命令字 [-选项…] [参数…]

3.在Linux系统下,如何辨别目录与文件及其他的文件?

白色:文件

蓝色:目录

浅蓝色:链接文件

绿色:可执行文件

红色:压缩文件

红色闪烁的文件:链接的文件不可用

[En]

Red flashing files: linked files are not available

黄色:设备文件(硬盘,网卡,CPU,鼠标,键盘)

4.如何查看一个文件的详细属性?

ls -l 文件名

5.如何查看一个目录本身的详细属性?

ls -dl 目录名字

6.查看文件详细属性,并以KB、MB、GB的方式显示文件的大小?

ls -lh 文件名

7.如何查看一个文件的inode号?

ls -i 文件名

8.请写出Linux下文件和目录的三个归属关系?

u 所有者

g 所属组

o 其他人

9.请写出Linux下基本权限的表示方式?

r:读取,w写入,x执行

10.命令行以#作为结尾代表什么含义?

超级管理员

mkdir 创建目录命令

  • mkdir(英文全拼:make directory)用于创建新目录
  • 命令格式:mkdir [-选项] 目录名
  • 常用选项:
  • -p 递归创建多个目录
  • 注意事项:
  • 目录还是文件的名字,除了以”/”以外的任意名称,”/”根目录,路径分隔符
  • 文件或目录的名字长度不能超过255个字符
  • mkdir命令示例
#在当前所在目录创建test目录
[root@localhost ~]# mkdir test
[root@localhost ~]# ls

#在当前所在目录同时创建多个目录
[root@localhost ~]# mkdir test1 test2 test3
[root@localhost ~]# ls

#指定在/tmp目录下创建abc目录
[root@localhost ~]# mkdir /tmp/abc
[root@localhost ~]# ls /tmp
abc

#在指定目录下同时创建多个目录
[root@localhost ~]# mkdir /tmp/abc1 /tmp/abc2 /tmp/abc3
[root@localhost ~]# ls /tmp

#在/opt目录下创建student,在当前目录创建student1..3
[root@localhost ~]# mkdir /opt/student student1  student2 student3
[root@localhost ~]# ls /opt
rh  student

#mkdir默认无法在一个不存在的目录下创建目录,需要通过-p选项
[root@localhost ~]# mkdir /opt/xx/oo
mkdir: 无法创建目录"/opt/xx/oo": 没有那个文件或目录

[root@localhost ~]# mkdir /opt/a/b/c/d
mkdir: 无法创建目录"/opt/a/b/c/d": 没有那个文件或目录

#在/opt目录下递归创建目录
[root@localhost ~]# mkdir -p /opt/xx/oo
[root@localhost ~]# ls /opt
rh  student  xx

[root@localhost ~]# mkdir -p /opt/a/b/c/d
[root@localhost ~]# ls /opt
a  rh  student  xx

#ls -R选项可以递归目录下所有内容
[root@localhost ~]# ls -R /opt/a
/opt/a:
b

/opt/a/b:
c

/opt/a/b/c:
d

cd 切换工作目录命令

  • cd(英文全拼:change directory)切换目录

命令格式:cd [-选项] [目录名]

  • 提示:目录名可以是绝对路径也可以是相对路径。如果不指定目录名,请切换到当前用户的主目录~
    [En]

    Tip: the directory name can be an absolute path or a relative path. If you do not specify a directory name, switch to the current user’s home directory ~*

绝对路径与相对路径

  • 绝对路径:以/(根)为起点,到达你想去的目标目录称为绝对路径
  • 相对路径:从当前路径开始,到达要转到的目标目录
    [En]

    relative path: start from the current path and reach the destination directory you want to go to*

  • 常用快捷操作:
  • ~ 表示为家目录
  • . 表示为当前目录
  • .. 表示上一级目录
  • -可在两路径之间来回切换

pwd 打印当前所在目录

  • pwd(英文全拼:print work directory)打印当前所在的工作目录,执行pwd命令后,可显示当前所在的工作目录的绝对路径名称
  • 命令格式:pwd [-选项]
[root@localhost ~]# cd /opt/a/b/c/d#打印当前所在目录绝对路径[root@localhost d]# pwd/opt/a/b/c/d#切换到用户家目录[root@localhost d]# cd ~[root@localhost ~]# pwd/root[root@localhost ~]# cd /opt/a/b/c/d[root@localhost d]# pwd/opt/a/b/c/d[root@localhost d]# cd[root@localhost ~]# pwd/root[root@localhost ~]# cd /bin[root@localhost bin]# pwd/bin[root@localhost bin]# cd /boot[root@localhost boot]# pwd/boot[root@localhost boot]# ls[root@localhost boot]# cd /dev[root@localhost dev]# pwd/dev[root@localhost dev]# ls[root@localhost dev]# cd /etc[root@localhost etc]# pwd/etc[root@localhost etc]# ls[root@localhost etc]# ls /bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var#"."表示当前所在目录,对于cd命令而言作用不大[root@localhost etc]# cd .[root@localhost etc]# cd /opt/a/b/c/d[root@localhost d]# pwd/opt/a/b/c/d#".."切换到当前目录的上一级目录[root@localhost d]# cd ..[root@localhost c]# pwd/opt/a/b/c[root@localhost c]# cd ..[root@localhost b]# pwd/opt/a/b[root@localhost b]# cd ..[root@localhost a]# cd ..[root@localhost opt]# pwd/opt[root@localhost opt]# cd ..[root@localhost /]# cd ..[root@localhost /]# cd[root@localhost ~]# ls[root@localhost ~]# cd /opt/a/b/c/d[root@localhost d]# pwd/opt/a/b/c/d#"-"可在两个路径之间来回切换[root@localhost d]# cd /etc/yum[root@localhost yum]# cd -/opt/a/b/c/d[root@localhost d]# pwd/opt/a/b/c/d[root@localhost d]# cd -/etc/ym[root@localhost yum]# cd -/opt/a/b/c/d[root@localhost d]# cd -/etc/yum

rmdir 删除空目录命令

  • rmdir(英文全拼:remove directory)删除空目录
  • 命令格式:rmdir [-选项] 目录名
#rmdir只能删除空目录,如果目录下存在数据无法删除[root@localhost ~]# rmdir /opt/armdir: 删除 "/opt/a" 失败: 目录非空[root@localhost ~]# ls -R /opt/a/opt/a:b/opt/a/b:c/opt/a/b/c:d/opt/a/b/c/d:[root@localhost ~]# rmdir /opt/a/b/c/d[root@localhost ~]# ls -R /opt/a/opt/a:b/opt/a/b:c/opt/a/b/c:[root@localhost ~]# rmdir /opt/a/b/c[root@localhost ~]# ls -R /opt/a/b/opt/a/b:[root@localhost ~]# rmdir /opt/a/b[root@localhost ~]# ls -R /opt/a/opt/a:[root@localhost ~]# rmdir /opt/a[root@localhost ~]# ls /optrh  student  xx[root@localhost ~]# rmdir /opt/rmdir: 删除 "/opt/" 失败: 目录非空

touch 创建文件命令

  • touch 命令用于创建新的空白文件
  • 命令格式:touch [-选项] 文件名
#在当前路径创建空文件[root@localhost ~]# touch hello[root@localhost ~]# ls#在当前路径同时创建多个文件[root@localhost ~]# touch t1 t2 t3 t4[root@localhost ~]# ls#在指定路径同时创建多个文件[root@localhost ~]# touch /opt/test1 /opt/test2 /opt/test3[root@localhost ~]# ls /optrh  student  test1  test2  test3  xx#如果存在同名目录时,无法创建[root@localhost ~]# mkdir testmkdir: 无法创建目录"test": 文件已存在#如果存在同名文件时,touch命令没有提示,但原有文件不会被覆盖[root@localhost ~]# touch t1#对于目录而言,只有单个目录的时候,"/"可有可无[root@localhost ~]# ls /opt/rh  student  test1  test2  test3  xx[root@localhost ~]# ls /optrh  student  test1  test2  test3  xx#对于目录而言,查看目录下的内容时,必须要有"/"[root@localhost ~]# ls /opt/xxoo#对于文件而言,后边绝对不能有"/"[root@localhost ~]# ls /opt/test1/opt/test1[root@localhost ~]# ls /opt/test1/ls: 无法访问/opt/test1/: 不是目录

cp 复制命令

  • cp(英文全拼:copy file)用于复制文件或目录,cp命令在复制时也可修改目录或文件名字
  • 命令格式:cp [-选项] 源文件或目录 目标目录
  • 常用选项:
  • -p 保留源文件属性不变(如:修改时间、归属关系、权限)
  • -r 复制目录(包含该目录下所有的子目录和文件)
#复制当前目录文件到/opt目录(相对路径方式复制)[root@localhost ~]# cp t1 /opt/[root@localhost ~]# ls /optrh  student  t1  test1  test2  test3  xx#复制文件到/opt目录(绝对路径方式复制)[root@localhost ~]# cp /root/t2 /opt[root@localhost ~]# ls /optrh  student  t1  t2  test1  test2  test3  xx#同时复制多个文件[root@localhost ~]# cp t3 t4 /opt/[root@localhost ~]# ls /opt#创建目录[root@localhost ~]# mkdir abc#使用-r对目录执行复制[root@localhost ~]# cp -r abc /opt[root@localhost ~]# ls /opt#同时复制多个目录[root@localhost ~]# mkdir abc1 abc2 abc3[root@localhost ~]# cp -r abc1 abc2 abc3 /opt[root@localhost ~]# ls /opt#复制hello文件到/opt并改名为hello.txt[root@localhost ~]# cp hello /opt/hello.txt[root@localhost ~]# ls /opt#复制xxxx目录到/opt并改名xxoo[root@localhost ~]# mkdir xxxx[root@localhost ~]# cp -r xxxx /opt/xxoo[root@localhost ~]# ls /opt#使用"."配合cp命令执行复制[root@localhost ~]# cd /etc/sysconfig/network-scripts/[root@localhost network-scripts]# pwd/etc/sysconfig/network-scripts[root@localhost network-scripts]# cp /root/t1 .[root@localhost network-scripts]# ls#操持属性不变复制文件[root@localhost ~]# cp -p anaconda-ks.cfg /optcp:是否覆盖"/opt/anaconda-ks.cfg"? y[root@localhost ~]# ls -l /opt/anaconda-ks.cfg-rw-------. 1 root root 1800 3月  13 17:34 /opt/anaconda-ks.cfg#对比以上两个文件的详细属性信息(最后一次修改时间)[root@localhost ~]# ls -l anaconda-ks.cfg-rw-------. 1 root root 1800 3月  13 17:34 anaconda-ks.cfg#这两个操作代表什么意思?[root@localhost ~]# cp -r xxxx /mnt/oooo  #拷贝并改名[root@localhost ~]# cp -r xxxx /mnt/oooo  #拷贝

mv 移动命令

  • mv(英文全拼:move file)用于移动文件或目录到其他位置,也可用于修改目录或文件名
  • 命令格式:mv [-选项] 源文件… 目标路径
#移动当前路径hello文件到/mnt目录[root@localhost ~]# mv hello /mnt[root@localhost ~]# ls /mnthello  home  oooo  test#同时移动多个文件[root@localhost ~]# mv t1 t2 t3 t4 /mnt[root@localhost ~]# ls /mnthello  home  oooo  student1  t1  t2  t3  t4  test#移动/opt目录下文件到/mntroot@localhost ~]# mv /opt/test1 /opt/test2 /opt/test3 /mnt/[root@localhost ~]# ls /mnthello  home  oooo  student1  t1  t2  t3  t4  test  test1  test2  test3#移动目录[root@localhost ~]# mv student1 /mnt[root@localhost ~]# ls /mnthello  home  oooo  student1  test#移动文件并改名[root@localhost ~]# mv hello.txt /media/hello[root@localhost ~]# ls /media/hello#移动目录并改名[root@localhost ~]# mv test /media/testxx[root@localhost ~]# ls /media/hello  testxx

cat 查看文件内容命令

  • cat (英文全拼:concatenate)命令用于查看文本文件内容
  • 命令格式:cat [选项] 文件名
  • 常用选项
  • -n #查看文件时以行号的形式显示文件内容
#查看文件内容[root@localhost ~]# cat anaconda-ks.cfg[root@localhost ~]# cat initial-setup-ks.cfg[root@localhost ~]# cat /etc/hosts#查看网卡文件内容,网卡配置文件[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32...NAME="ens32"   //网卡名UUID="16085f4c-f690-4058-b29e-d55c73387026"DEVICE="ens32"ONBOOT="yes"IPADDR="192.168.0.50"     //网卡IP地址PREFIX="24"               //子网掩码GATEWAY="192.168.0.254"   //网关DNS1="114.114.114.114"    //DNS#查看当前系统用户基本信息文件内容[root@localhost ~]# cat /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin#查看当前系统主机名配置文件内容[root@localhost ~]# cat /etc/hostnamelocalhost.localdomain#查看当前系统版本信息文件内容[root@localhost ~]# cat /etc/redhat-releaseCentOS Linux release 7.6.1810 (Core)#查看当前系统开机自动挂载配置文件内容[root@localhost ~]# cat /etc/fstab#查看系统组基本信息文件内容[root@localhost ~]# cat /etc/group#使用"-n"以行号形式显示文件内容[root@localhost ~]# cat -n /etc/passwd[root@localhost ~]# cat -n /etc/hostname[root@localhost ~]# cat -n /etc/fstab[root@localhost ~]# cat -n /etc/group[root@localhost ~]# cat -n /etc/services

less命令

  • less工具是对文件的输出进行分页显示的工具,常用于查看内容量较大的文件
  • 命令格式:less [-选项] 文件
  • 常用选项:
  • -N #以行号形式显示文件内容
  • 使用技巧:
  • 键盘上下键逐行查看
  • pgdn :向下翻一页(Fn + 下键)
  • pgup :向上翻一页(Fn + 上键)
  • /字符串 :搜索指定字符串(n从上向下搜索,N从下向上搜索)
  • G:直接跳转到文件最后一行
  • gg:直接跳转到文件行首
  • :1000 #精准的定位到某一行
  • q :退出
[root@localhost ~]# less -N /etc/services

head与tail命令

  • head命令:用来显示文件开头部分内容,默认显示文件开头10行内容
  • 命令格式:head [选项] 参数
  • 常用选项:
  • -n
[root@localhost ~]# head /etc/passwd[root@localhost ~]# head /etc/fstab[root@localhost ~]# head /etc/group[root@localhost ~]# head /etc/hostname[root@localhost ~]# head /etc/hosts[root@localhost ~]# head /etc/sysconfig/network-scripts/ifcfg-ens32#查看存放DNS配置文件信息[root@localhost ~]# head /etc/resolv.conf#使用-n指定显示文件前多少行内容[root@localhost ~]# head -n 5 /etc/passwd[root@localhost ~]# head -n 6 /etc/passwd[root@localhost ~]# head -n 15 /etc/passwd[root@localhost ~]# head -n 20 /etc/passwd
  • tail命令:用来显示文件末尾部分内容,默认显示文件末尾10行内容
  • 命令格式:tail [选项] 参数
  • 常用选项:-n
[root@localhost ~]# tail /etc/passwd#使用"-n"指定显示文件末尾多少行内容[root@localhost ~]# tail -n 5 /etc/passwd[root@localhost ~]# tail -n 5 /etc/sysconfig/network-scripts/ifcfg-ens32IPADDR="192.168.0.50"PREFIX="24"GATEWAY="192.168.0.254"DNS1="114.114.114.114"IPV6_PRIVACY="no"#动态查看文件内容[root@localhost ~]# touch t1root@localhost ~]# tail -f t1#另开一个终端向文件写入内容[root@localhost ~]# echo 123 > t1

rm删除命令

  • rm(英文全拼:remove)命令用于删除文件或者目录。
  • 命令格式:rm [-选项…] 目录或文件…

  • 常用选项

  • -f 强制删除
  • -r 删除目录
  • “*”特殊字符:系统常用符号,用来代表任意所有字符
[root@localhost ~]# ls /opt
abc  abc1  abc2  abc3  anaconda-ks.cfg  hello.txt  home  rh  student  t1  t2  t3  t4  xx  xxoo

[root@localhost ~]# ls /mnt
hello  home  oooo  student1  t1  t2  t3  t4  test  test1  test2  test3

#删除指定目录下文件
[root@localhost ~]# rm /opt/anaconda-ks.cfg
rm:是否删除普通文件 "/opt/anaconda-ks.cfg"?y  #默认需要确认(y|n)

#查看文件是否被成功删除
[root@localhost ~]# ls /opt
abc  abc1  abc2  abc3  hello.txt  home  rh  student  t1  t2  t3  t4  xx  xxoo

[root@localhost ~]# rm /opt/hello.txt
rm:是否删除普通空文件 "/opt/hello.txt"?y

#同时删除目录下指定文件
[root@localhost ~]# rm /opt/t1 /opt/t2 /opt/t3 /opt/t4
rm:是否删除普通空文件 "/opt/t1"?y
rm:是否删除普通空文件 "/opt/t2"?y
rm:是否删除普通空文件 "/opt/t3"?y
rm:是否删除普通空文件 "/opt/t4"?y

#查看文件是否被成功删除
[root@localhost ~]# ls /opt
abc  abc1  abc2  abc3  home  rh  student  xx  xxoo

#使用"-f"强制删除文件(无需确认,直接删除)
[root@localhost ~]# rm -f /mnt/hello
[root@localhost ~]# ls /mnt
home  oooo  student1  t1  t2  t3  t4  test  test1  test2  test3

#同时强制删除多个文件
[root@localhost ~]# rm -f /mnt/t1 /mnt/t2 /mnt/t3 /mnt/t4
[root@localhost ~]# ls /mnt

#删除目录
[root@localhost ~]# rm  -r /opt/abc
rm:是否删除目录 "/opt/abc"?y

[root@localhost ~]# ls /opt
abc1  abc2  abc3  home  rh  student  xx  xxoo

#同时删除多个目录
[root@localhost ~]# rm -r /opt/abc1 /opt/abc2 /opt/abc3
rm:是否删除目录 "/opt/abc1"?y
rm:是否删除目录 "/opt/abc2"?y
rm:是否删除目录 "/opt/abc3"?y

[root@localhost ~]# ls /opt
home  rh  student  xx  xxoo

#同时强制删除多个目录
[root@localhost ~]# rm -rf /opt/home /opt/student /opt/xx /opt/xxoo
[root@localhost ~]# ls /opt
rh

#创建目录与文件
[root@localhost ~]# touch /opt/t1
[root@localhost ~]# mkdir /opt/test
[root@localhost ~]# ls /opt
rh  t1  test

#rm命令在删除目录时,包含改目录及目录下所有数据全部删除
[root@localhost ~]# rm -rf /opt/
[root@localhost ~]# ls /

[root@localhost ~]# ls /mnt
home  oooo  student1  test  test1  test2  test3

#使用"*"通配任意所有字符,删除/mnt目录下所有数据
[root@localhost ~]# rm -rf /mnt/*
[root@localhost ~]# ls /mnt

软连接与硬连接

  • Linux中的链接文件类似于windows中的快捷方式
  • 软连接功能:可以跨分区链接软连接,也可以链接目录。删除源文件后,链接文件不可用。
    [En]

    soft connection features: soft connections can be linked across partitions and directories can be linked. After the source file is deleted, the linked file is not available.*

  • 软连接命令格式:ln -s 源文件路径 目标路径
  • 注:创建链接时,请务必写入目录或文件的绝对路径,即使在当前路径下也是如此。
    [En]

    Note: when creating a link, be sure to write the absolute path of the directory or file, even under the current path.*

[root@localhost ~]# touch hello.soft[root@localhost ~]# ls#创建软连接(必须要绝对路径创建)[root@localhost ~]# ln -s /root/hello.soft /opt[root@localhost ~]# ls /opt#查看连接文件详细属性[root@localhost ~]# ls -l /opt/hello.softlrwxrwxrwx. 1 root root 16 3月  21 14:28 /opt/hello.soft -> /root/hello.soft#提示:链接文件的权限最终取决于源文件的权限#普通用户验证[lisi@localhost ~]$ ls /opthello.soft[lisi@localhost ~]$ ls -l /opt/hello.softlrwxrwxrwx. 1 root root 16 3月  21 14:28 /opt/hello.soft -> /root/hello.soft[lisi@localhost ~]$ cat /opt/hello.softcat: /opt/hello.soft: 权限不够#提示:由于源文件存放于/root目录下,而普通用户对/root目录没有任何权限,所以普通用户无法查看#删除源文件[root@localhost ~]# rm -f /root/hello.soft[root@localhost ~]# ls#删除源文件后,软链接文件不可用[root@localhost ~]# ls -l /opt/hello.softlrwxrwxrwx. 1 root root 16 3月  21 14:28 /opt/hello.soft -> /root/hello.soft#创建文件并创建软连接[root@localhost ~]# touch hello.soft[root@localhost ~]# ln -s /root/hello.soft /opt[root@localhost ~]# ls -l /opt/hello.softlrwxrwxrwx. 1 root root 16 3月  21 14:39 /opt/hello.soft -> /root/hello.soft#删除链接文件后,源文件仍然可用[root@localhost ~]# rm -f /opt/hello.soft[root@localhost ~]# ls[root@localhost ~]# cat hello.soft#对目录创建软连接[root@localhost ~]# ln -s /root/test1 /opt/[root@localhost ~]# ls -ld /opt/test1lrwxrwxrwx. 1 root root 11 3月  21 14:44 /opt/test1 -> /root/test13创建链接时一定要写目录或文件的绝对路径,哪怕是在当前路径下,也要写绝对路径[root@localhost ~]# ln -s hello.soft /opt[root@localhost ~]# ls /opthello.soft  test1[root@localhost ~]# ls -l /opt/hello.softlrwxrwxrwx. 1 root root 10 3月  21 14:47 /opt/hello.soft -> hello.soft
  • 硬链接功能:硬连接不能跨分区或目录链接。删除源文件后,链接文件仍可用。
    [En]

    hard link features: hard connections cannot be linked across partitions or directories. After the source file is deleted, the linked file is still available.*

  • 硬连接命令格式:ln 源文件路径 目标路径
#创建文件,并创建硬连接[root@localhost ~]# touch hello.hard[root@localhost ~]# ln /root/hello.hard /opt/[root@localhost ~]# ls /opthello.hard  hello.soft  test1#向硬连接的源文件写入内容root@localhost ~]# echo 123 > /root/hello.hard#查看源文件内容[root@localhost ~]# cat /root/hello.hard123#查看链接文件内容,以同步更新[root@localhost ~]# cat /opt/hello.hard123#向链接文件写入内容,查看源文件以同步更新[root@localhost ~]# echo xx >> /opt/hello.hard#擦看源文件,以同步更新[root@localhost ~]# cat /root/hello.hard123xx#硬连接文件的特点可以保持文件属性不发生改变[root@localhost ~]# ls -l /root/hello.hard-rw-r--r--. 2 root root 7 3月  21 14:55 /root/hello.hard[root@localhost ~]# ls -l /opt/hello.hard-rw-r--r--. 2 root root 7 3月  21 14:55 /opt/hello.hard#并且硬连接文件的i节点号相同[root@localhost ~]# ls -i /root/hello.hard33711090 /root/hello.hard[root@localhost ~]# ls -i /opt/hello.hard33711090 /opt/hello.hard#硬连接不允许对目录进行连接root@localhost ~]# ln /root/test1 /optln: "/root/test1": 不允许将硬链接指向目录#硬连接源文件删除后,链接文件仍然可用[root@localhost ~]# rm -f /root/hello.hard[root@localhost ~]# cat /opt/hello.hard123xx#向硬连接文件写入内容[root@localhost ~]# echo  abc >> /opt/hello.hard[root@localhost ~]# cat /opt/hello.hard123xxabc#硬连接不允许跨分区[root@localhost ~]# lsblkNAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda               8:0    0   20G  0 disk├─sda1            8:1    0    1G  0 part /boot└─sda2            8:2    0   19G  0 part  ├─centos-root 253:0    0   17G  0 lvm  /  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]sr0              11:0    1  4.3G  0 rom[root@localhost ~]# ln /root/hello.soft /bootln: 无法创建硬链接"/boot/hello.soft" => "/root/hello.soft": 无效的跨设备连接

Linux命令的分类

  • 内部命令:bash程序自带的基本管理命令
  • 外部命令:有独立的外部可执行文件命令
    [En]

    external commands: there are independent external executable file commands*

  • type 用于区别内部命令与外部命令
  • which 用于查找可以执行程序文件位置
[root@localhost opt]# type ls[root@localhost opt]# type cat[root@localhost opt]# type hash[root@localhost ~]# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin[root@localhost ~]# hash命中  命令   1    /usr/bin/cat   1    /usr/bin/ls[root@localhost opt]# hash -r[root@localhost opt]#[root@localhost opt]# hashhash: 哈希表为空[root@localhost opt]# lshello.hard  hello.soft  t1  test1  test.txt[root@localhost opt]# hash命中  命令   1    /usr/sbin/ls
  • 总结:
  • shell程序是用户和系统之间的接口,用于解释用户的命令
  • 查找命令对应的程序文件所在位置:which 命令
  • shell程序大多数存放在/etc/shells文件中
  • 系统默认使用的shell为/bin/bash
  • 查看当前使用的shell:echo $SHELL
  • 区别内部命令与外部命令的方式:typt 命令
  • shell程序查找可执行程序文件路径定义在$PATH环境变量中
  • shell查找的外部命令路径结果会记录到缓存的hash表中

help 命令帮助手册

  • help命令用于查看shell内部命令的帮助信息,包括使用方法、选项等…

  • 命令格式:help [选项] 命令

#获取内部命令帮助信息
[root@localhost etc]# help cd

#help无法获取外部命令的帮助信息
root@localhost etc]# help ls
bash: help: 没有与 ls' 匹配的帮助主题。尝试  help' 或者 man -k ls' 或者  ls'。

[root@localhost etc]# type help
help 是 shell 内嵌

#获取help命令本身的帮助信息
[root@localhost etc]# help help

[root@localhost etc]# type cat
cat 是 /usr/bin/cat

[root@localhost etc]# help cat
bash: help: 没有与 cat' 匹配的帮助主题。尝试  help' 或者 man -k cat' 或者  cat'。

#查看命令帮助手册(命令自带)
[root@localhost etc]# cat --help
[root@localhost etc]# ls --help

man 获取命令帮助手册

  • man 命令用于查看系统命令的帮助信息,包括使用方法、选项、使用例子等…,对比–help ,man 输出的信息更加详细
  • 命令格式:man [-选项] 命令
  • 常用快捷操作
  • 向下键向下移一行
  • 向上键向上移一行
  • [Page Down] 向下翻一页
  • [Page Up] 向上翻一页
  • /关键字 #搜索关键字,配合n(向下查询)、N(向上查询)
  • q 退出
[root@localhost etc]# man ls
[root@localhost etc]# man cat
[root@localhost etc]# man touch
[root@localhost etc]# man mkdir
[root@localhost etc]# info ls

Linux系统的运行级别

Linux系统运行级别:linux系统有7个运行级别,不同的运行级别运行的程序和功能都不一样,而Linux系统默认是运行在一个标准的级别上,系统运行级别文件/etc/inittab文件

运行级别 0:所有进程被终止,机器将有序的停止,关机时系统处于这个运行级别(关机)

运行级别 1:单用户模式,(root用户进行系统维护),系统里运行的所有服务也都不会启动

运行级别 2:多用户模式(网络文件系统NFS服务没有被启动)

运行级别 3:完全多用户模式,(有NFS网络文件系统)标准的运行级别,命令行模式

运行级别 4:系统未使用

运行级别 5:登录后,进入带GUI的图形化界面,标准的运行级别

运行级别 6:系统正常关闭并重启

#查看当前系统运行级别
[root@localhost etc]# runlevel
N 5
#解释;当前系统处于的运行级别
#解释:N代表没有从任何级别跳转过来

#切换系统运行级别
[root@localhost ~]# init N

#查看运行级别文件内容
[root@localhost ~]# cat /etc/inittab
inittab is no longer used when using systemd.

#
ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

#
Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
multi-user.target: analogous to runlevel 3   #运行级别3
graphical.target: analogous to runlevel 5    #运行级别5
#
To view current default target, run:
systemctl get-default                    #查看当前系统默认的运行级别
#
To set a default target, run:
systemctl set-default TARGET.target    #修改当前系统默认运行级别

#查看默认运行级别
[root@localhost ~]# systemctl get-default
graphical.target  #默认运行级别为5

 #修改默认运行级别为3
[root@localhost ~]# systemctl set-default multi-user.target
[root@localhost ~]# systemctl get-default
multi-user.target

#修改默认运行级别为5
[root@localhost ~]# systemctl set-default graphical.target
[root@localhost ~]# systemctl get-default
graphical.target

关机与重启

  • linux下常用的关机命令有:shutdown、halt、poweroff、init
  • init 0 关机
  • halt #立刻关机
  • shutdown –h now #立刻关机
  • shutdown -h 10 #10分钟后自动关机
  • *poweroff #立刻关机 (记这个)
[root@localhost ~]# poweroff
  • 重启命令:reboot shutdown
  • reboot #立刻重启 (记这个)
  • shutdown -r now #立刻重启
  • shutdown -r 10 #过十分钟后重启
[root@localhost ~]# reboot

课后练习

1.请在/tmp目录下创建student目录,并在student目录下同时创建t1、t2、t3文件

mkdir /tmp/student

cd /tmp/student/

touch t1 t2 t3

touch /tmp/student/t1 /tmp/student/t2 /tmp/student/t3

2.请在/tmp目录下递归创建test1/test2/test3目录

mkdir -p /tmp/test1/test2/test3

3.切换到/tmp/test1/test2/test3目录下,并打印(查看)当前所在目录

cd /tmp/test1/test2/test3

pwd

4.请同时在/opt、/media目录下创建upload文件

touch /opt/upload /media/upload

5.请将/opt目录下的upload文件移动至/tmp/test1/test2/test3目录下,并改名为upload.bak

mv /opt/upload /tmp/test/1/test/2/test3/upload.bak

6.请将/etc/passwd文件拷贝至/opt目录下,改名为passwd.bak,并保持属性不变

cp -p /etc/passwd /opt/passwd.bak

7.请将/etc/fstab文件拷贝至/opt目录下,并改名为fstab.bak

cp -p /etc/fstab /opt/fstab.bak

8.请将/etc/sysconfig/network-scripts/ifcfg-ens32 文件拷贝至/opt目录下,并改名为ens32.bak

cp /etc/sysconfig/network-scripts/ifcfg-ens32 /opt/ens32.bak

9.请删除/etc/yum.repos.d/目录下所有内容

rm -rf /etc/yum.repos.d/*

10.请在/etc/yum.repos.d/目录下创建local.repo文件

touch /etc/yum.repos.d/local.repo

11.请查看/etc/sysconfig/network-scripts/ifcfg-ens32文件末尾5行内容

tail -5 /etc/sysconfig/network-scripts/ifcfg-ens32

tail -n 5 /etc/sysconfig/network-scripts/ifcfg-ens32

12.请查看/etc/passwd文件第1行内容

head -n 1 /etc/passwd

head -1 /etc/passwd

13.请查看/etc/hostname文件内容

cat /etc/hostname

14.请查看/etc/hosts文件内容

cat /etc/hosts

15.请说出软连接与硬连接的特点

软连接:可以跨分区,可以链接到目录,源文件删除后链接文件不可用

[En]

Soft connection: can be across partitions, can be linked to directories, and link files are not available after source files are deleted

硬连接:无跨分区,无目录连接。删除源文件后,链接文件即可用。

[En]

Hard connection: no cross-partition, no directory connection. After the source file is deleted, the linked file is available.

16.请在/opt目录下创建hello.soft文件,并创建软连接到/tmp目录下

touch /opt/hello.soft

ln -s /opt/hello.soft /tmp

17.请在/opt目录下创建hello.hard文件,并创建硬连接到/tmp目录下,并查看连接文件详细属性

touch /opt/hello.hard

ln /opt/hello.hard /tmp

18.如何获取ls命令的帮助信息?

man ls

ls –help

19.请说出Linux系统的运行级别

0:关机

1:单用户模式

2:多用户模式(没有NFS)

3:完全多用户模式,标准运行级别

4:保留

5:带GUI图形化界面,标准的运行级别

6:系统关闭并重启

20.如何重启Linux系统?

reboot

init 6

计算机硬件组成部分

  • 输入设备:键盘、鼠标、触摸屏等
    [En]

    input devices: keyboard, mouse, touch screen, etc.*

  • 主机设备:主板、中央处理器(CPU)、主存储器(内存)、网卡、声卡、显示卡等
  • 输出设备:屏幕、耳机、打印机、投影仪等
    [En]

    output devices: screens, headphones, printers, projectors, etc.*

  • 外部存储设备:硬盘、软盘、光盘、U盘等、蓝光光驱
  • CPU缓存
  • CPU比较主流的厂商
  • AMD公司
  • Interl公司
  • CPU架构
  • x86架构,8086架构,80286,80386,x86称号
  • 8位、16位、32位、64位,CPU一次可以处理的数据量,
  • 32位CPU一次可以从内存中读取大约3.25G左右的数据量
  • 64位CPU一次可以从内存中读取大约128G左右的数据量
  • CPU核心
  • 单核心,一颗CPU只能有一个运算单元
  • 多核心,一颗CPU里边有两个以上的运算单元

Linux系统目录介绍

  • /(根):系统所有数据都存放在根目录下
  • /bin:存放用户和管理员必备的可执行的二进制程序文件
  • /boot:存放Linux系统内核及引导系统程序所需要的文件目录
  • /dev:存放硬件设备的目录,如键盘、鼠标、硬盘、光盘等等 (记住)
  • /etc:存放服务的配置文件,用户信息文件 (记住)
  • /root:超级管理员的家目录
  • /home:系统普通用户的家目录(记住)
  • /lib:存放系统中的程序运行所需要的共享库及内核模块
  • /opt:额外安装的可选应用程序包所放置的位置
  • /srv:服务启动之后需要访问的数据目录
  • /tmp:一般用户或正在执行的程序临时存放文件的目录,任何人都可以访问,重要数据不可放置在此目录下
  • /var:存放系统执行过程中经常变化的文件,如随时都在变化的日志文件就存放/var/log/下 (记住)
  • /mnt、/media :光盘和镜像等预设的挂载点(记住)
  • /proc:Linux伪文件系统,该目录下的数据存在于内存当中,不占用磁盘空间
  • /lib64 :存放函式库
  • /run :程序或服务启动后,存放PID的目录
  • /sys:存放被建立在内存中的虚拟文件系统
  • /usr:操作系统软件资源所放置的目录
  • /usr/bin:与/bin目录相同,存放用户可以使用的命令程序
  • /usr/lib:与/lib目录相同,存放系统中的程序运行所需要的共享库及内核模块
  • /usr/etc:用于存放安装软件时使用的配置文件
  • /usr/games:与游戏比较相关的数据放置处
  • /usr/include:c/c++等程序语言的档头(header)与包含档(include)放置处
  • /usr/lib64:与/lib64目录相同,存放函式库
  • /usr/libexec:不经常被使用的执行程序或脚本会放置在此目录中
  • /usr/local: 额外安装的软件存放目录 (记住)
  • /usr/sbin:该目录与/sbin目录相同,存放用户可执行的二进制程序文件
  • /usr/share: 放置只读架构的杂项数据文件
  • /usr/src:一般软件源代码建议存放该目录下

查看内核信息

  • uname 命令用于显示系统内核信息
  • 命令格式:uname [-选项…]
  • 常用选项:
  • -s :显示内核名称
  • -r :显示内核版本
[root@localhost ~]# unameLinux[root@localhost ~]# uname -rsLinux 3.10.0-957.el7.x86_64#解释:Linux   #内核名称3       #主版本10      #次版本0       #修改版本957     #补丁次数el7     #Enterprise Linux(企业版Linux)x86_64  #CPU架构#Linux内核官网https://www.kernel.org/

查看CPU信息

  • /proc/cpuinfo文件用于存放系统CPU信息
  • lscpu 用于显示CPU架构信息
  • 命令格式:lscpu [-选项]
#查看/proc/cpuinfo文件内容[root@localhost ~]# cat /proc/cpuinfoprocessor :#系统中逻辑处理核的编号。对于单核处理器,则可认为是其CPU编号,对于多核处理器则可以是物理核、或者使用超线程技术虚拟的逻辑核vendor_id :   #CPU制造商cpu family :  #CPU产品系列代号model   :    #CPU属于其系列中的哪一代的代号model name:    #CPU属于的名字及其编号、标称主频stepping   :   #CPU属于制作更新版本cpu MHz   :    #CPU的实际使用主频cache size   :  #CPU二级缓存大小physical id   : #单个CPU的标号siblings       :#单个CPU逻辑物理核数core id        :#当前物理核在其所处CPU中的编号,这个编号不一定连续cpu cores    :  #该逻辑核所处CPU的物理核数apicid          :#用来区分不同逻辑核的编号,系统中每个逻辑核的此编号必然不同,此编号不一定连续fpu             : #是否具有浮点运算单元(Floating Point Unit)fpu_exception  :  #是否支持浮点计算异常cpuid level   :   #执行cpuid指令前,eax寄存器中的值,根据不同的值cpuid指令会返回不同的内容wp             :  #表明当前CPU是否在内核态支持对用户空间的写保护(Write Protection)flags          :   #当前CPU支持的功能bogomips   :       #在系统内核启动时粗略测算的CPU速度(Million Instructions Per Second)clflush size  :    #每次刷新缓存的大小单位cache_alignment :  #缓存地址对齐单位address sizes     :#可访问地址空间位数power management : #对能源管理的支持,有以下几个可选支持功能:
#使用lscpu查看cpu信息[root@localhost ~]# lscpu Architecture: #架构  CPU(s): #逻辑cpu颗数  Thread(s) per core: #每个核心线程  Core(s) per socket: #每个cpu插槽核数/每颗物理cpu核数  CPU socket(s): #cpu插槽数  Vendor ID: #cpu厂商ID  CPU family: #cpu系列  Model: #型号  Stepping: #步进  CPU MHz: #cpu主频  Virtualization: #cpu支持的虚拟化技术  L1d cache: #一级缓存(google了下,这具体表示表示cpu的L1数据缓存)  L1i cache: #一级缓存(具体为L1指令缓存)  L2 cache: #二级缓存

查看系统内存信息

  • /proc/meminfo文件用于存放系统内存信息
  • free 用于查看内存使用情况
  • 命令格式:free [-选项]
  • 常用选项:-h #以人类易读方式显示大小(KB,MB,GB)
#查看/proc/meminfo文件内容[root@localhost ~]# cat /proc/meminfoMemTotal:          995896 kB    #所有可用的内存大小,物理内存减去预留位和内核使用。系统从加电开始到引导完成,firmware/BIOS要预留一些内存,内核本身要占用一些内存,最后剩下可供内核支配的内存就是MemTotal。这个值在系统运行期间一般是固定不变的,重启会改变。MemFree:            244196 kB   #表示系统尚未使用的内存。MemAvailable:       435080 kB   #真正的系统可用内存,系统中有些内存虽然已被使用但是可以回收的,比如cache/buffer、slab都有一部分可以回收,所以这部分可回收的内存加上MemFree才是系统可用的内存Buffers:             2132 kB   #用来给块设备做缓存的内存,(文件系统的 metadata、pages)Cached:             314632 kB  #分配给文件缓冲区的内存,例如vi一个文件,就会将未保存的内容写到该缓冲区SwapCached:            0 kB    #被高速缓冲存储用的交换空间(硬盘的swap)的大小Active:            295908 kB    #经常使用的高速缓冲存储器页面文件大小Inactive:          271552 kB    #不经常使用的高速缓冲存储器文件大小Active(anon):      251528 kB    #活跃的匿名内存Inactive(anon):     13044 kB    #不活跃的匿名内存Active(file):       44380 kB    #活跃的文件使用内存Inactive(file):    258508 kB   #不活跃的文件使用内存Unevictable:           0 kB    #不能被释放的内存页Mlocked:               0 kB    #系统调用 mlock 家族允许程序在物理内存上锁住它的部分或全部地址空间。这将阻止Linux 将这个内存页调度到交换空间(swap space),即使该程序已有一段时间没有访问这段空间SwapTotal:             0 kB    #交换空间总内存SwapFree:              0 kB    #交换空间空闲内存Dirty:                 4 kB    #等待被写回到磁盘的Writeback:             0 kB    #正在被写回的AnonPages:         15100 kB    #未映射页的内存/映射到用户空间的非文件页表大小Mapped:             7160 kB    #映射文件内存Shmem:               100 kB    #已经被分配的共享内存Slab:               9236 kB    #内核数据结构缓存SReclaimable:       2316 kB    #可收回slab内存SUnreclaim:         6920 kB    #不可收回slab内存KernelStack:        2408 kB    #内核消耗的内存PageTables:         1268 kB    #管理内存分页的索引表的大小NFS_Unstable:          0 kB    #不稳定页表的大小Bounce:                0 kB    #在低端内存中分配一个临时buffer作为跳转,把位于高端内存的缓存数据复制到此处消耗的内存WritebackTmp:          0 kB    #FUSE用于临时写回缓冲区的内存CommitLimit:       22980 kB    #系统实际可分配内存Committed_AS:     536244 kB    #系统当前已分配的内存VmallocTotal:     892928 kB    #预留的虚拟内存总量VmallocUsed:       29064 kB    #已经被使用的虚拟内存VmallocChunk:     860156 kB    #可分配的最大的逻辑连续的虚拟内存
#使用free命令查看内存使用情况[root@localhost ~]# free -h              total        used        free      shared  buff/cache   availableMem:           972M        344M        238M         13M        389M        424MSwap:          2.0G          0B        2.0G#解释:Mem 物理内存统计信息total:      #物理内存总量used:       #以使用的内存总量free:       #空闲内存总量shared:     #共享内存总量buff/cache: #块设备与普通文件占用的缓存数量available:  #还可以被应用程序使用的物理内存大小#解释:Swap 内存交换空间,当物理内存不足时,可以使用硬盘空间充当内存使用total:      #交换分区内存总量used:       #正在使用的交换分区内存free:       #空闲交换分区内存

查看网卡信息

  • 网卡配置文件地址: /etc/sysconfig/network-scripts/网卡名
  • ifconfig 用于显示和设置网卡的参数
  • 命令格式: ifconfig [网卡名]
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32TYPE="Ethernet"         #网卡类型=以太 ※PROXY_METHOD="none"     #代理方式=关闭BROWSER_ONLY="no"       #只是浏览器=否BOOTPROTO="none"        #获取IP地址的方式=固定IP ※DEFROUTE="yes"          #是否设置默认路由=是IPV4_FAILURE_FATAL="no" #是否开启ipv4致命检测=否(如果ipv4配置失败禁用设备)NAME="ens32"            #物理网卡设备名字 ※UUID="3ef0d258-f9a4-49e5-a9da-7b47bc98daa0  "#网卡UUIDDEVICE="ens32"          #网卡名字 ※ONBOOT="yes"            #开机或重启时是否启动网卡  ※IPADDR="192.168.0.210"  #IP地址  ※PREFIX="24"             #子网掩码 ※GATEWAY="192.168.0.254" #网关 ※DNS1="8.8.8.8"          #dns服务器IP地址 ※DNS2=8.8.4.4            #备用dns服务器IP地址 ※
#使用ifconfig命令查看网卡信息[root@localhost ~]# ifconfigens32: flags=4163  mtu 1500        inet 192.168.0.29  netmask 255.255.255.0  broadcast 192.168.0.255        inet6 fe80::8d50:c4d5:97b0:9d64  prefixlen 64  scopeid 0x20        ether 00:0c:29:b0:cf:c8  txqueuelen 1000  (Ethernet)        RX packets 3948  bytes 1811465 (1.7 MiB)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 2538  bytes 459113 (448.3 KiB)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0#解释:ens32:       #网卡名称  ※flags=4163: #标志UP:         #网卡处于活跃状态  ※BROADCAST:  #支持广播RUNNING:    #网线已接入MULTICAST:  #支持组播mtu 1500:   #最大传输单元(字节),表示此网卡一次能传输的最大数据包 ※inet 192.168.0.29       #IPV4地址 ※netmask 255.255.255.0   #子网掩码 ※broadcast 192.168.0.255 #广播地址 ※inet6 fe80::8d50:c4d5:97b0:9d64     #IPV6地址prefixlen 64  scopeid 0x20  #前缀 64 作用域 0x20ether 00:0c:29:b0:cf:c8 #网卡MAC地址 ※xqueuelen 1000          #网卡设置的传送队列长度(Ethernet)              #网卡连接类型RX packets 3948         #接收正确的数据包 ※bytes 1811465 (1.7 MiB) #接收的数据量与字节 ※RX errors 0  dropped 0  overruns 0  frame 0 #接收到的错误包、丢弃的数据包数、由于速度过快而丢失的数据包、发生frame错误而丢失的数据包数 ※TX packets 100      #发送的正确的数据包数 ※bytes 8116 (7.9 KiB)#发送的数据量、字节  ※TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0 #发送时产生错误的数据包数、丢弃的数据包数、由于速度过快而丢失的数据包数、发生carrier错误而丢失的数据包数、冲突信息包的数目 ※#只查看指定的网卡[root@localhost ~]# ifconfig ens32lo: 本地回环网卡,不是物理网卡,通过软件虚拟出来的一个网卡,127.0.0.1,用于测试本机的联通性[root@localhost ~]# ping 127.0.0.1virbr0: 虚拟化的网络接口,通过软件技术虚拟出来的一个网卡,192.168.122.1,KVM虚拟化技术的时候

查看主机名及修改主机名

  • /etc/hostname文件用于存放主机名
  • hostname 命令用于显示和设置主机名
  • 命令格式:hostname [新名称]
#查看主机名[root@localhost ~]# hostnamelocalhost.localdomain#查看主机名配置文件[root@localhost ~]# cat /etc/hostnamelocalhost.localdomain#临时修改主机名(立刻生效,服务器重启以后失效)[root@localhost ~]# hostname test[root@localhost ~]# hostnametest#exit/loguot登出系统[root@localhost ~]# exit[c:\~]$ ssh 192.168.0.50[root@test ~]#[root@test ~]# hostname fhsd.jhglshdjkghjkdfhgkjhgdsahgjklhdsfjghsdhgjlhsd[root@test ~]# logout[root@fhsd ~]# hostname sdhjghsdfjkhgkjdfshkgljhsdjfhgjksdhgjkhsdjgjkl[root@fhsd ~]# exit#命令行永久修改主机名(立刻生效,不需要重启系统)[root@localhost ~]# hostnamectl set-hostname test[root@localhost ~]# exit

vi/vim文本编辑器

  • Vim是从 vi 发展出来的一个文本编辑器,vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正确性
  • vi/vim 共分为三种模式:命令模式、输入模式、底线命令模式(末行模式)
  • 命令模式:刚刚启动 vi/vim,便进入了命令模式
  • 输入模式:在命令模式下按 a/i/o 就进入了输入模式
  • ESC,退出输入模式,切换到命令模式
  • 底线命令模式:在命令模式下按下:(英文冒号)就进入了底线命令模式
  • 命令格式:vim 文件名
  • 若目标文件不存在,则新创建文件并编辑
  • 若目标文件以存在,则打开文件并编辑
  • 命令模式:刚刚启动 vi/vim,便进入了命令模式
  • i 切换到输入模式,在当前光标所在字符前插入
  • a 切换到输入模式,在当前光标所在字符后插入
  • o 切换到输入模式,在当前光标所在行下插入新行
  • : 切换到底线命令模式,以在最底一行输入命令
  • x 在命令模式下删除当前光标所在的单字符
  • dd 删除一整行内容,配合数字可删除指定范围内的行
  • C 删除当前光标及光标后所有内容并进入输入模式
  • u 恢复上一次修改内容,一次恢复一个操作,可多次恢复,直到恢复本次操作初始状态为止
  • $ 将光标移动至行尾
  • 0(零) 将光标移动至行首
  • gg 跳转至文件第一行
  • G 跳转至文件最后一行
  • yy 复制当前行,配合数字可以同时复制多行
  • p 粘贴当前光标所在行下
  • /关键字 搜索文件内关键字,n从上向下快速定位关键字,N从下向上快速定位关键字
  • 底线命令模式允许您输入单个或多个字符命令,可用命令很多。
    [En]

    the bottom line command mode allows you to enter single or multiple character commands, and there are many commands available.*

  • :w 保存
  • :q 退出
  • :wq 保存并退出
  • :x 保存并退出
  • ZZ 保存并退出
  • :q! 强制退出不保存
  • :wq! 强制保存并退出,适用于只读文件(没有写权限)
  • :set nu 以行号形式显示文件内容
  • :set nonu 取消行号显示
  • :行号 快速跳转到指定行
  • :%s 替换文件内容,g替换全文,默认只替换每一行匹配到的第一个关键字(数字s 指定替换的行)
  • :nohl 取消高亮显示
[root@test ~]# vim /etc/services

修改网卡IP地址

  • 网卡配置文件地址: /etc/sysconfig/network-scripts/网卡名
  • ifconfig #用于显示和设置网卡的参数
  • systemctl restart network #重启网络
  • ifup 网卡名 #启动该网卡设备
  • ifdown 网卡名 #禁用该网卡设备
#修改IP地址[root@test ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32TYPE="Ethernet"PROXY_METHOD="none"BROWSER_ONLY="no"BOOTPROTO="none"DEFROUTE="yes"IPV4_FAILURE_FATAL="no"IPV6INIT="yes"IPV6_AUTOCONF="yes"IPV6_DEFROUTE="yes"IPV6_FAILURE_FATAL="no"IPV6_ADDR_GEN_MODE="stable-privacy"NAME="ens32"UUID="16085f4c-f690-4058-b29e-d55c73387026"DEVICE="ens32"ONBOOT="yes"IPADDR="192.168.0.60"   #修改IP地址PREFIX="24"GATEWAY="192.168.0.254"DNS1="114.114.114.114"IPV6_PRIVACY="no"~#重启网络(IP地址发生改变,当前终端会断开)[root@test ~]# systemctl restart network[c:\~]$ ssh 192.168.0.60#关闭网卡并激活网卡[root@localhost ~]# ifdown ens32;ifup ens32#查看所有网卡信息[root@test ~]# ip a
  • 使用命令修改网卡IP地址

nmcli connection modify 网卡名 ipv4.method manual ipv4.addresses Ip地址/掩码 connection.autoconnect yes

解释:2

nmcli connection modify(修改)

网卡名 ipv4.method(配置ipv4地址方法)

manual (手动配置)

ipv4.addresses(ipv4地址)

Ip地址/掩码 connection.autoconnect yes(开机自动连接)

  • 激活网卡:nmcli connection up 网卡名
  • 关闭网卡:nmcli connection down 网卡名
  • 重启网卡:nmcli connection reload 网卡名
#使用命令修改网卡IPV地址
[root@test ~]# nmcli connection modify ens32 ipv4.method manual ipv4.addresses 192.168.0.50/24 connection.autoconnect yes

#激活网卡
[root@test ~]# nmcli connection up ens32
[c:\~]$ ssh 192.168.0.50

host命令

  • host用于将一个域名解析到一个IP地址
[root@test ~]# host www.baidu.com
www.baidu.com has address 110.242.68.3
www.baidu.com has address 110.242.68.4
www.baidu.com is an alias for www.a.shifen.com.

www.baidu.com is an alias for www.a.shifen.com.

nslookup命令

  • nslookup用于查询域名解析是否正常,在网络故障时用来诊断网络问题
[root@test ~]# nslookup www.baidu.com
Server:     114.114.114.114
Address:    114.114.114.114#53

Non-authoritative answer:
Name:   www.baidu.com
Address: 110.242.68.4
Name:   www.baidu.com
Address: 110.242.68.3

alias别名管理

  • alias命令用于设置命令别名,用户可以使用alias自定义命令别名来简化命令的复杂度
  • .bashrc 文件存放命令别名
  • 命令格式:aliasi [别名]=[命令] #注意事项:等号(=)前后不能有空格
  • unalias 别名 #取消别名
#定义别名
[root@test ~]# alias lsnet='ls /etc/sysconfig/network-scripts/'
[root@test ~]# lsnet
[root@test ~]# alias myls='ls -ldh'
[root@test ~]# myls /opt

#查看当前系统可用命令别名
[root@test ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias lsnet='ls /etc/sysconfig/network-scripts/'
alias mv='mv -i'
alias myls='ls -ldh'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

#两条命令效果相同
[root@test ~]# ls -l hello
-rw-r--r--. 1 root root 426 3月  28 15:00 hello
[root@test ~]# ll hello
-rw-r--r--. 1 root root 426 3月  28 15:00 hello

[root@test ~]# which ls
alias ls='ls --color=auto'
    /usr/sbin/ls
[root@test ~]# /usr/sbin/ls
[root@test ~]# ls

#取消本次命令的别名功能"\"
[root@test ~]# \ls

#取消命令别名
[root@test ~]# unalias myls
[root@test ~]# myls
bash: myls: 未找到命令...

#定义别名不要跟系统命令发生冲突
[root@test ~]# alias ls=hostname
[root@test ~]# ls
test

#取消命令别名
[root@test ~]# unalias ls
[root@test ~]# alias

#重新定义别名
[root@test ~]# alias ls='ls --color=auto'
[root@test ~]# ls

history 管理历史

  • history命令用于显示历史记录和执行过的命令,登录shell时会读取~./bash_history历史文件中记录下的命令,当退出或登出shell时,会自动保存到历史命令文件,该命令单独使用时,仅显示历史命令
  • 历史命令默认只能存储1000条,可以通过/etc/profile文件修改
  • 命令格式:history [-选项] [参数]
  • 常用选项:
  • -a 追加本次新执行的命令至历史命令文件中
  • -d 删除历史命令中指定的命令
  • -c 清空历史命令列表
  • 快捷操作:
  • !# 调用命令历史中第N条命令
  • !string 调用命令历史中以strind开头的命令
  • !! 重复执行上一条命令
#获取命令帮助
[root@test ~]# help history

#查看历史命令
[root@test ~]# history

#查看记录历史命令文件
[root@test ~]# cat .bash_history

#将历史命令同步至历史命令配置文件中
[root@test ~]# history -a
[root@test ~]# cat .bash_history

#删除历史命令中655条命令历史
[root@test ~]# history -d 655
[root@test ~]# history -d 637

#清空缓存中所有历史命令
[root@test ~]# history -c
[root@test ~]# history
    1  history

#删除历史命令配置文件(该文件删除后系统会再次自动创建)
[root@test ~]# rm -rf .bash_history

#快速调用历史命令中第1条
[root@test ~]# !1
[root@test ~]# !3

#调用历史命令中以cat开头的命令(只调用最近使用的cat历史命令)
[root@test ~]# !cat

#重复执行上一条命令
[root@test ~]# !!

#历史命令默认只能记录1000条,可以通过/etc/profile文件修改
[root@test ~]# vim /etc/profile
...

46 HISTSIZE=100

date日期时间管理

  • date命令用于显示或设置系统日期与时间
  • 命令格式:date [-选项] [+格式符] #查看系统日期时间
  • 命令格式:date [-选项] #设置日期时间
  • 常用选项:-s 设置日期时间
  • 格式符:
  • +%Y 年份
  • +%B 月份
  • +%d 日
  • +%H 时
  • +%M 分
  • +%S 秒
  • +%F 年-月-日
  • +%X 时:分:秒
#显示系统日期与时间
[root@test ~]# date
2021年 03月 28日 星期日 17:08:34 CST

#只显示年分
[root@test ~]# date +%Y
2021

#只显示月份
[root@test ~]# date +%B
三月

#只显示几号
[root@test ~]# date +%d
28

#只显示小时
[root@test ~]# date +%H
17

#只显示分钟
[root@test ~]# date +%M
10

#只显示秒
[root@test ~]# date +%S
24

#显示年月日
[root@test ~]# date +%F
2021-03-28

#显示时分秒
[root@test ~]# date +%X
17时12分10秒

#显示年月日时分秒
[root@test ~]# date +%F%X
2021-03-2817时12分39秒

#可以自定义分隔符"-"
[root@test ~]# date +%F-%X
2021-03-28-17时13分38秒

[root@test ~]# date +%F:%X
2021-03-28:17时13分55秒

#修改系统年月日
[root@test ~]# date -s 2020-03-28
2020年 03月 28日 星期六 00:00:00 CST

#修改系统时分秒
[root@test ~]# date -s 17:16:00
2020年 03月 28日 星期六 17:16:00 CST

#修改年月日时分秒
[root@test ~]# date -s '2021-03-28 17:17:00'
2021年 03月 28日 星期日 17:17:00 CST
#解释:
''单引号:引用整体,屏蔽特殊符号的功能
""双引号:引用整体,不会屏蔽特殊符号的功能

#Linux的两种时钟
系统时钟:内核通过CPU的工作频率去计算的时间
硬件时钟:

#显示硬件时间
[root@test ~]# clock
2021年03月28日 星期日 17时23分42秒  -0.945549 秒

#显示并同步系统与硬件时钟
[root@test ~]# man hwclock
-s:把系统时间设置成与硬件时间相同
-w:把硬件时间设置成与系统时间相同
[root@test ~]# hwclock -w
[root@test ~]# date
2021年 03月 28日 星期日 17:27:18 CST

#cal显示日历
[root@test ~]# cal
      三月 2021
日 一 二 三 四 五 六
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

#显示指定的全年月份
[root@test ~]# cal 2021

wc统计命令

  • wc 用于统计文件的字节数、行数,并将统计的结果输出到屏幕
  • 命令格式:wc [-选项] 文件名
  • 常用选项:
  • -c #统计字节数
  • -l #统计行数
[root@test ~]# wc /etc/passwd
43   87 2259 /etc/passwd
行数 单词 字节  文件名

#统计文件字节数
[root@test ~]# wc -c /etc/passwd
2259 /etc/passwd

#统计文件行数
[root@test ~]# wc -l /etc/passwd
43 /etc/passwd

[root@test ~]# wc -l /etc/fstab
11 /etc/fstab

管道符

  • 管道符”|”:将命令的输出结果交给另外一条命令作为参数继续处理
[root@test ~]# head -10 /etc/passwd |tail -5

[root@test ~]# head -10 /etc/passwd |tail -5 |wc -l
5

root@test ~]# cat -n /etc/passwd |head -10|tail -5
     6  sync:x:5:0:sync:/sbin:/bin/sync
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8  halt:x:7:0:halt:/sbin:/sbin/halt
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10  operator:x:11:0:operator:/root:/sbin/nologin

[root@test ~]# ifconfig ens32 |head -2
ens32: flags=4163  mtu 1500
        inet 192.168.0.50  netmask 255.255.255.0  broadcast 192.168.0.255

重定向操作

  • 重定向操作:将上一条命令的输出写入其他文本文件
    [En]

    redirect operation: write the output of the previous command to other text files*

  • 重定向的表示符号
  • 重定向输出(覆盖)

  • 重定向输出(追加)

  • < #输入重定向(覆盖)
  • << #输入重定向(追加)
  • 只收集正确的输出结果

  • 2> 只收集错误的输出结果(覆盖)
  • 2>> 只收集错误的输出结果(追加)
  • &> 正确错误都收集(覆盖)
  • &>> 正确错误都收集(追加)
#将命令的输出结果以覆盖的方式重定向到文件中,(>附带创建文件功能)
[root@test ~]# ifconfig ens32 |head -2 > /opt/ens32.bak
ens32: flags=4163  mtu 1500
        inet 192.168.0.50  netmask 255.255.255.0  broadcast 192.168.0.255

[root@test ~]# cat /etc/hostname > /opt/ens32.bak
[root@test ~]# cat /opt/ens32.bak
test

[root@test ~]# free -h > /opt/free.bak
[root@test ~]# cat /opt/free.bak
              total        used        free      shared  buff/cache   available
Mem:           972M        414M        123M         15M        435M        336M
Swap:          2.0G          0B        2.0G

#将命令的输出结果以追加的方式重定向到文件中
[root@test ~]# cat /etc/hostname >> /opt/free.bak
[root@test ~]# cat /opt/free.bak

#">"只收集正确的输出结果,不收集错误的输出结果
[root@test ~]# ls xxooooxx > /opt/xx.txt
ls: 无法访问xxooooxx: 没有那个文件或目录

#"2>"只收集错误的输出结果,不收集正确的输出结果
[root@test ~]# ls xxooooxx 2> /opt/xx.txt
[root@test ~]# cat /opt/xx.txt
ls: 无法访问xxooooxx: 没有那个文件或目录

#"2>"以覆盖的方式将输出结果重定向到文件中
[root@test ~]# cat /etc/abc 2> /opt/ens32.bak
[root@test ~]# cat /opt/ens32.bak
cat: /etc/abc: 没有那个文件或目录

#"2>>"以追加的方式将输出结果重定向到文件中
[root@test ~]# ls /etc/abcd 2>> /opt/ens32.bak
[root@test ~]# cat /opt/ens32.bak
cat: /etc/abc: 没有那个文件或目录
ls: 无法访问/etc/abcd: 没有那个文件或目录

#"&>"以覆盖的方式将正确输出与错误输出重定向到文件中
[root@test ~]# lscat &> /opt/abc.txt
[root@test ~]# cat /opt/abc.txt

[root@test ~]# ls /etc/passwd &> /opt/pass.bak
[root@test ~]# cat /opt/pass.bak

[root@test ~]# free -h &> /opt/pass.bak
[root@test ~]# cat /opt/pass.bak

#"&>"以追加的方式将正确输出与错误输出重定向到文件中
[root@test ~]# ifconfig ens32 | head -2 &>> /opt/pass.bak
[root@test ~]# cat /opt/pass.bak

#以覆盖方式将正确输出与错误输出重定向到不同文件中
[root@test ~]# ll -d /root/  bcd >a.txt 2>b.txt
[root@test ~]# cat a.txt
dr-xr-x---. 24 root root 4096 3月  28 18:07 /root/
[root@test ~]# cat b.txt
ls: 无法访问bcd: 没有那个文件或目录

echo命令与sleep命令

  • echo命令用于输出指定的字符串和变量
  • 命令格式:echo [-选项] [参数]
[root@test ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@test ~]# echo xxoo
xxoo

[root@test ~]# echo abc
abc

[root@test ~]# echo 男人好难
男人好难

[root@test ~]# echo 123
123

[root@test ~]# cat /etc/hostname
test

[root@test ~]# echo localhost > /etc/hostname
[root@test ~]# cat /etc/hostname
localhost
  • sleep命令可以用来将目前动作延迟一段时间
  • 命令格式:sleep 时间
  • 常用选项: s 秒 m 分钟 h 小时 d 日
[root@test ~]# sleep 3

课后练习

1.查看当前系统内核名称及版本信息

uname -sr

2.请写系统存放cpu配置文件

/proc/cpuinfo

3.请写出查看cpu信息命令

cat /proc/cpuinfo

lscpu

4.请写出系统存放内存配置文件

/proc/meminfo

5.请写出查看内存命令(以人类易读方式显示)

free -h

6.请写出系统存放网卡配置文件路径

/etc/sysconfig/network-scripts/

7.请写出查看网卡配置信息命令

ifconfig(如果系统最小化安装,需要安装net-tools)

ip a (ip address)

8.请写出系统存放主机名配置文件

/etc/hostname

9.请写出查看主机名命令

cat /etc/hostname

hostname

10.将主机名修改为student(永久修改)

hostnamectl set-hostname student

vim /etc/hostname

echo student > /etc/hostname

11.请写出vim的三种模式

命令模式

输入模式

底线命令模式(最后一行模式)

[En]

Bottom line command mode (last line mode)

12.将/etc/passwd文件复制到/opt目录,使用vim打开文件并显示行号

cp /etc/passwd /opt

vim /opt/passwd

:set nu

13.使用vim在/opt/passwd文件中搜索包含root关键字的行

/root

14.使用vim在/opt/passwd文件中将光标快速跳转到第10行,并将光标跳转到行尾

:10 $

15.使用vim在/opt/passwd文件中快速跳转到文件最后一行并删除,在将光标跳转到文件第一行,将刚刚删除的行复制到文件第二行

G dd p

16.使用vim将/etc/hostname文件内容读入到/opt/passwd文件最后一行下

:r /etc/hostname

17.使用vim在/opt/passwd文件中复制前5行内容并粘贴到文件最后一行下

5yy p

18.将本次vim的修改恢复至初始状态,并保存退出

u

:wq

19.将本机IP地址修改为192.168.0.100,并重启动网卡

vim /etc/sysconfig/network-scripts/ifcfg-ens32

systemctl restart network

20.如何获取一个域名所对应的IP地址

host www.baidu.com

21.如何检测本机使用的DNS是否可用

nslookup www.jd.com

22.请将hostname命令设置别名为hn(临时设置)

alias hn=hostname

23.取消hostname命令别名

unalias hn

24.如何查看本机历史命令

history

25.执行命令历史中第20条命令

!20

26.删除命令历史中第5条命令

history -d 5

27.清空所有历史命令

history -c

rm -rf .bash_history

28.查看本机当前系统日期与时间

date

29.将本机日期时间设置与你当前时间一致

date -s ‘2021-04-10 14:32:00’

30.统计/etc/passwd文件行数,并将命令输出结果重定向至/opt/pass.bak文件中

wc -l /etc/passwd > /opt/pass.bak

31.显示/etc/passwd文件末尾10行的前5行内容,并将输出结果追加至/opt/pass.bak文件中

tail -10 /etc/passwd | head -5 > /opt/pass.bak

cat -n /etc/passwd | tail -10 | head -5 >> /opt/pass.bak

用户账号管理

  • 用户帐号功能:可以使用用户帐号登录系统,实现访问控制。
    [En]

    function of user account: user account can be used to log in to the system and access control can be realized.*

  • 用户模板目录:/etc/skel/
[root@localhost ~]# ls -a /etc/skel/.  ..  .bash_logout  .bash_profile  .bashrc  .mozilla[root@localhost ~]# cd /etc/skel/[root@localhost skel]# vim prompt

useradd创建用户

  • useradd 命令用于创建新的用户
  • 命令格式:useradd [-选项] 用户名
  • 常用选项:
  • -u 指定用户UID
  • -d 指定用户家目录(了解)
  • -c 用户描述信息
  • -g 指定用户基本组(了解)
  • -G 指定用户附加组
  • -s 指定用户的解释器程序
[root@localhost ~]# useradd user1#创建用户并指定用户的UID[root@localhost ~]# useradd -u 1100 user2#创建用户并指定用户的家目录root@localhost ~]# useradd -d /opt/user3 user3#创建用户并指定UID与用户描述信息[root@localhost ~]# useradd -u 1400  -c yunwei user4#创建test组[root@localhost ~]# groupadd test#创建用户指定用户UID、描述信息、基本组[root@localhost ~]# useradd -u 1500 -c xxoo@163.com -g test user5[root@localhost ~]# id user5#创建用户指定用户UID、描述信息、附加组[root@localhost ~]# useradd -u 1600 -c yunwei -G test xiaozhang[root@localhost ~]# id xiaozhanguid=1600(xiaozhang) gid=1600(xiaozhang) 组=1600(xiaozhang),1401(test)#/sbin/nologin :禁止用户登录系统[root@localhost ~]# useradd -u 1800 -c test -s /sbin/nologin user8user8:x:1800:1800:test:/home/user8:/sbin/nologin

id命令

  • id 命令用于查看系统用户和用户所在组的信息
  • 命令格式:id [-选项] [用户名]
[root@localhost ~]# id user1uid=1001(user1) gid=1001(user1) 组=1001(user1)

/etc/passwd用户信息文件

用户的基本信息存放在/etc/passwd文件

[root@localhost ~]# vim /etc/passwd
root:x:0:0:root:/root:/bin/bash
#每个字段含义解释:用户名:密码占位符:UID:基本组GID:用户描述信息:家目录:解释器程序
UID:0 超级用户
UID:1-999 系统伪用户,不能登录系统
UID:1000-65535 普通用户,管理员创建的用户

组:

基本组(初始组):一个用户只能有一个基本组

[En]

Basic group (initial group): only one basic group is allowed for a user

附加组(基本组之外):一个用户可以允许多个附加组

[En]

Additional groups (outside the basic group): a user can allow multiple additional groups

用户—>shell程序—>内核—>硬件

/etc/default/useradd文件

/etc/default/useradd 存放用户默认值信息

[root@localhost ~]# vim /etc/default/useradd
useradd defaults file
GROUP=100 #用户默认组
HOME=/home #用户家目录
INACTIVE=-1 #密码过期宽限天数(/etc/shadow文件第7个字段)
EXPIRE= #密码失效时间(/etc/shadow文件第8个字段)
SHELL=/bin/bash #默认使用的
SKEL=/etc/skel #模板目录
CREATE_MAIL_SPOOL=yes #是否建立邮箱

/var/spool/mail/用户邮件目录

[root@localhost ~]# ls /var/spool/mail/
laowang  lisi  rpc  user1  user2  user3  user4  user5  user8  xiaozhang

#查看邮件
[root@localhost ~]# mail

passwd设置用户密码

  • passwd命令用于设置用户密码
  • 命令格式:passwd [-选项] [用户名]
  • 密码规范:长度不能少于8个字符,复杂度(数字、字母区分大小写,特殊字符)
  • 密码规范:本次修改的密码不能与上次修改的密码太接近。
    [En]

    password specification: the password modified this time cannot be too close to the password modified last time.*

  • 常用选项
  • -S 查看密码信息
  • -l 锁定用户密码
  • -u 解锁用户密码
  • -d 删除密码
  • –stdin 通过管道方式设置用户密码
  • 非交互设置用户密码
  • 命令格式:echo ‘密码’ | passwd –stdin 用户名
#设置用户密码[root@localhost ~]# passwd user1更改用户 user1 的密码 。新的 密码:1无效的密码: 密码是一个回文重新输入新的 密码:1passwd:所有的身份验证令牌已经成功更新。#使用user1用户登录系统[user1@localhost ~]$ lsprompt[user1@localhost ~]$ cat prompt不允许随便修改系统xx文件!有问题可联系管理员邮箱:xxoo@163.com#查看用户密码信息[root@localhost ~]# passwd -S user1#锁定用户当密码[root@localhost ~]# passwd -l user2锁定用户 user2 的密码 。passwd: 操作成功[root@localhost ~]# passwd -S user2user2 LK 2021-04-10 0 99999 7 -1 (密码已被锁定。)#解锁用户密码[root@localhost ~]# passwd -u user2解锁用户 user2 的密码。passwd: 操作成功#删除用户密码[root@localhost ~]# passwd -d user2清除用户的密码 user2。passwd: 操作成功#非交互设置用户密码[root@localhost ~]# echo 1 | passwd --stdin laowang更改用户 laowang 的密码 。passwd:所有的身份验证令牌已经成功更新。

/etc/shadow用户密码文件

  • 用户的密码信息存放在/etc/shadow文件中,该文件默认任何人都没有任何权限(不包括root)
[root@localhost ~]# vim /etc/shadowroot:$6$1ji5e8yglrZWAcI6$FONKr3qebZufQ.u0Mf/MbipzGw/MVvxS.vgXcy/duc4b/GU0U7tfe37wPQ4XJEXstqBuwvaJqq2/kY/g/783u/::0:99999:7:::#每个字段含义解释:第一字段:用户名第二字段:密码加密字符串,加密算法为SHA512散列加密算法,如果密码位是"*"或者"!!"表示密码已过期第三个字段:上次修改密码的日期,从1970年1月1日开始,每经过一天的时间戳为1<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Third field: the date on which the password was last modified, starting from January 1, 1970, with a time stamp of 1 for each passing day</font>*</details>第四个字段:密码修改的持续时间。如果该字段为0,则可以随时更改密码。例如,如果该字段为10,则在10天内不能更改密码。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>The fourth field: the duration of password modification. If the field is 0, the password can be changed at any time. For example, if the field is 10, the password cannot be changed within 10 days.</font>*</details>第五个字段:密码有效期第六字段:密码到期前的警告时间(与第五字段相比)<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Sixth field: password warning time before expiration (compared to the fifth field)</font>*</details>第七个字段:密码过期后的宽限天数(与第五个字段相比)<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Seventh field: the number of days of grace after the password expires (compared to the fifth field)</font>*</details>第八个字段:帐户到期时间,日期为1970年1月1日<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Eighth field: account expiration time, date from January 1, 1970</font>*</details>第九个字段:保留#chage命令用于修改/etc/shadow文件信息,修改文件内容第三个字段(密码最后一次修改时间)[root@localhost ~]# chage -d 0 user8

su命令

  • su命令用于切换当前用户身份到其他用户身份
  • 命令格式:su [-选项] [用户名]
#只切换用户身份,环境没有改变[root@localhost ~]# su user1[user1@localhost root]$ lsls: 无法打开目录.: 权限不够[user1@localhost root]$ cd[user1@localhost ~]$ exitexit#切换用户身份,连同环境一起切换[root@localhost ~]# su - user1上一次登录:六 4月 10 16:54:40 CST 2021pts/1 上[user1@localhost ~]$ pwd/home/user1#普通用户切换为root(需要输入root用户的密码)[user1@localhost ~]$ su - root密码:上一次登录:六 4月 10 16:05:17 CST 2021从 192.168.0.1pts/2 上

usermod修改用户属性

  • usermod 命令用于修改已存在用户的基本信息
  • 命令格式:usermod [-选项] 用户名
  • 常用选项:
  • -u 修改用户UID
  • -d 修改用户家目录
  • -g 修改用户基本组
  • -c 修改用户描述信息
  • -G 添加用户附加组
  • -s 修改用户shell
#修改用户UID(用户如果以登录系统,不允许修改)[root@localhost ~]# usermod -u 1111 user1[root@localhost ~]# id user1uid=1111(user1) gid=1001(user1) 组=1001(user1)#修改用户描述信息[root@localhost ~]# usermod -c xxoo@163.com user8#修改用户的附加组[root@localhost ~]# usermod -G test user8[root@localhost ~]# id user8uid=1800(user8) gid=1800(user8) 组=1800(user8),1401(test)#修改用户的解释器[root@localhost ~]# usermod -s /bin/bash user8

userdel删除用户

  • userdel 用于删除给定的用户以及与用户相关的文件,该命令若不加选项仅删除用户账号,不删除用户的家目录
  • 命令格式:userdel [-选项] 用户名
  • 常用选项:
  • -r 删除用户同时,并删除用户的家目录
#删除用户,仅删除账号,不删除家目录[root@localhost ~]# userdel user8[root@localhost ~]# ls /homelaowang  lisi  user1  user2  user4  user5  user8  xiaozhang[root@localhost ~]# id user8id: user8: no such user#删除用户,连同用户家目录一并删掉[root@localhost ~]# userdel -r user4[root@localhost ~]# ls /homelaowang  lisi  user1  user2  user5  user8  xiaozhang[root@localhost ~]# id user4id: user4: no such user

groupadd添加新组

  • groupadd 用于创建一个新的工作组,新组的信息将被添加到/etc/group文件中
  • 命令格式:groupadd [-选项] 组名
  • 常用选项:
  • -g GID #指定组的GID
#创建组[root@localhost ~]# groupadd -g 1555 student[root@localhost ~]# cat /etc/group

/etc/group组信息文件

  • 组信息存放在/etc/group文件中
[root@localhost ~]# vim /etc/grouproot:x:0:#每个字段含义解释:组名:组密码占位符:GID:组中附加用户

/etc/gshadow组密码文件

  • 组密码信息存放在/etc/gshadow文件中
[root@localhost ~]# vim /etc/gshadowroot:::#每个字段含义解释:组名:组密码:组内管理员:组中附加用户

groupmod修改组属性

  • groupmod 用于修改指定工作组属性
  • 命令格式:groupmod [-选项] 组名
  • 常用选项:
  • -g GID #修改组的GID
  • -n 新组名 #修改组名
#修改组名[root@localhost ~]# groupmod -n stugrp student#修改组GIDroot@localhost ~]# groupmod -g 1666 stugrp

gpasswd组管理命令

  • gpasswd 是Linux工作组文件/etc/group和/etc/gshadow管理工具,用于将用户添加到组或从组中删除
  • 命令格式:gpasswd [-选项] 用户名 组名
  • 常用选项:
  • -a #将用户添加到工作组
  • -d #将用户从工作组中删除
#创建用户[root@localhost ~]# useradd hary[root@localhost ~]# useradd tom[root@localhost ~]# useradd natasha[root@localhost ~]# useradd kenji[root@localhost ~]# useradd jack#讲用户加入到组[root@localhost ~]# gpasswd -a hary stugrp正在将用户"hary"加入到"stugrp"组中[root@localhost ~]# gpasswd -a tom stugrp正在将用户"tom"加入到"stugrp"组中[root@localhost ~]# gpasswd -a kenji stugrp正在将用户"kenji"加入到"stugrp"组中[root@localhost ~]# gpasswd -a natasha stugrp正在将用户"natasha"加入到"stugrp"组中[root@localhost ~]# gpasswd -a jack stugrp正在将用户"jack"加入到"stugrp"组中[root@localhost ~]##查看组文件信息[root@localhost ~]# cat /etc/groupstugrp:x:1666:hary,tom,kenji,natasha,jack#将用户从组中删除root@localhost ~]# gpasswd -d tom stugrp[root@localhost ~]# gpasswd -d hary stugrp正在将用户"hary"从"stugrp"组中删除[root@localhost ~]# gpasswd -d jack stugrp正在将用户"jack"从"stugrp"组中删除[root@localhost ~]# gpasswd -d kenji stugrp正在将用户"kenji"从"stugrp"组中删除[root@localhost ~]# cat /etc/group

groupdel删除组

  • groupdel 用于删除指定工作组
  • 命令格式:groupdel 组名
[root@localhost ~]# groupdel stugrp

chmod权限管理

  • chmod(英文全拼:change mode)设置用户对文件的权限
  • 命令格式:chmod [-选项] 归属关系+-=权限类别 文件…

  • 常用选项:

  • -R 递归修改目录下所有的子文件与子目录的权限与父目录相同
  • 归属关系:u 所有者 g 所属组 o 其他人
  • 权限类别: r 读取 w 写入 x 执行 – 没有权限
  • 操作:+ 添加权限 – 去除权限 = 重新定义权限
  • 权限数字表示:r —- 4 w —- 2 x —- 1 0 没有权限

`shell

查看文件详细属性

[root@localhost ~]# ll hello
-rw-r–r–. 1 root root 426 3月 28 15:00 hello

为文件所有者添加执行权限

[root@localhost ~]# chmod u+x hello
[root@localhost ~]# ll hello
-rwxr–r–. 1 root root 426 3月 28 15:00 hello

为文件所属组添加写权限

[root@localhost ~]# chmod g+w hello
[root@localhost ~]# ll hello
-rwxrw-r–. 1 root root 426 3月 28 15:00 hello

为文件其他人添加写权限

[root@localhost ~]# chmod o+w hello
[root@localhost ~]# ll hello
-rwxrw-rw-. 1 root root 426 3月 28 15:00 hello

使用(逗号)可以同时为多个用户授权

[root@localhost ~]# chmod g+x,o+x hello
[root@localhost ~]# ll hello
-rwxrwxrwx. 1 root root 426 3月 28 15:00 hello

去除所有者执行权限

[root@localhost ~]# chmod u-x hello
[root@localhost ~]# ll hello
-rw-rwxrwx. 1 root root 426 3月 28 15:00 hello

去除所属组执行权限

[root@localhost ~]# chmod g-x hello
[root@localhost ~]# ll hello
-rw-rw-rwx. 1 root root 426 3月 28 15:00 hello

去除其他人执行权限

[root@localhost ~]# chmod o-x hello
[root@localhost ~]# ll hello
-rw-rw-rw-. 1 root root 426 3月 28 15:00 hello

同时去除ugo写权限

[root@localhost ~]# chmod u-w,g-w,o-w hello
[root@localhost ~]# ll hello
-r–r–r–. 1 root root 426 3月 28 15:00 hello

重新定义所有者权限

[root@localhost ~]# chmod u=rwx hello
[root@localhost ~]# ll hello
-rwxr–r–. 1 root root 426 3月 28 15:00 hello

重新定义所属组权限

[root@localhost ~]# chmod g=rwx hello
[root@localhost ~]# ll hello
-rwxrwxr–. 1 root root 426 3月 28 15:00 hello

重新定义其他人权限

[root@localhost ~]# chmod o=rwx hello
[root@localhost ~]# ll hello
-rwxrwxrwx. 1 root root 426 3月 28 15:00 hello

创建目录并设置目录权限

[root@localhost ~]# mkdir /test
[root@localhost ~]# ll -d /test
drwxr-xr-x. 2 root root 6 4月 11 14:30 /test

为目录所属组添加写权限

[root@localhost ~]# chmod g+w /test
[root@localhost ~]# ll -d /test
drwxrwxr-x. 2 root root 6 4月 11 14:30 /test

为目录其他人添加写权限

[root@localhost ~]# chmod o+w /test
[root@localhost ~]# ll -d /test
drwxrwxrwx. 2 root root 6 4月 11 14:30 /test
[root@localhost ~]#

重新定义所有用户权限

[root@localhost ~]# chmod u=rwx,g=rx,o=rx /test
[root@localhost ~]# ll -d /test
drwxr-xr-x. 2 root root 6 4月 11 14:30 /test

同时为所有用户定义相同权限

[root@localhost ~]# chmod ugo=rwx /test
[root@localhost ~]# ll -d /test
drwxrwxrwx. 2 root root 21 4月 11 14:37 /test

权限数字定义方式

[root@localhost ~]# ll hello
-rwxrwxrwx. 1 root root 426 3月 28 15:00 hello
所有者:rwx 4+2+1=7
所属组:r 4
其他人:r 4
[root@localhost ~]# chmod 744 hello
[root@localhost ~]# ll hello
-rwxr–r–. 1 root root 426 3月 28 15:00 hello

所有者:rw 4+2=6
所属组:rw 4+2=6
其他人:— 0
[root@localhost ~]# chmod 660 hello
[root@localhost ~]# ll hello
-rw-rw—-. 1 root root 426 3月 28 15:00 hello

所有者:rwx 4+2+1=7
所属组:wx 2+1=3
其他人:— 0
[root@localhost ~]# touch /hello.txt
[root@localhost ~]# ll /hello.txt
-rw-r–r–. 1 root root 0 4月 11 14:45 /hello.txt
[root@localhost ~]# chmod 730 /hello.txt
[root@localhost ~]# ll /hello.txt
-rwx-wx—. 1 root root 0 4月 11 14:45 /hello.txt

去除所有用户权限

[root@localhost ~]# chmod 000 /hello.txt
[root@localhost ~]# ll /hello.txt

Original: https://www.cnblogs.com/gaoziman/p/Linux-blog1.html
Author: 爱笑的Gao
Title: RHCSA阶段笔记

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/524118/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 【Example】C++ 标准库智能指针 unique_ptr 与 shared_ptr

    【概念直接搬运Docs】C 样式编程的一个主要 bug 类型是内存泄漏。 泄漏通常是由于为分配的内存的调用失败引起的 delete new 。 现代 C++ 强调”资源…

    Linux 2023年6月13日
    0134
  • Python的偏函数(Partial Function Application)

    这个概念说实话以前听过,但没深究过,也没用到过…但是被人问到了,就查来记录一下。 查下资料,发现这个东西说白了就是:部分参数有默认值的函数,Orz。再回过头来看他的英文…

    Linux 2023年6月8日
    097
  • 软件危机复习

    没有银弹的含义 软件危机:由于软件规模越来越大,软件复杂性越来越高,可靠性问题也越来越突出,传统的个人设计,个人实现的方式不再满足要求,迫切需要改变软件生产方式,提高软件开发效率,…

    Linux 2023年6月8日
    091
  • openssl 签署SAN证书

    openssl, x509, SAN 1、Prepare openssl config file. cat > my-openssl.cnf << EOF [ c…

    Linux 2023年6月6日
    083
  • centos快速搭建nfs共享

    一、nfs服务器端 01.安装nfs服务 yum -y install nfs-utils 02.创建存储目录 mkdir -p /data/2haohr_backup 03.设置…

    Linux 2023年6月6日
    0110
  • 在公司当上PD的心路历程

    前不久因为接了个新项目,我被选中当上PD也就是专门负责给客户演示,推进项目、录视频、写文档、做测试,因为我本来就需要测这些东西,熟悉算法、应用、固件,所以大部分人就觉得非我不可。 …

    Linux 2023年6月8日
    098
  • OpenStack 发放云主机

    登陆网址 具体安装步骤欢迎参照我的博客:https://www.cnblogs.com/kongshuo/p/16618008.html用户名 admin 密码 redhat 创建…

    Linux 2023年6月8日
    0119
  • 一文带你玩透结构体和方法

    package main import ( "fmt" ) //定义结构体类型User type User struct { username string &…

    Linux 2023年6月7日
    082
  • debugfs使用指南

    debugfs 概述 类似sysfs、procfs,debugfs 也是一种内存文件系统。不过不同于sysfs一个kobject对应一个文件,procfs和进程相关的特性,debu…

    Linux 2023年6月7日
    095
  • MIT6.824 Lab2调试过程

    2021-12-12 21:50 测试了5次,通过了并发用例。运行并发的用例的时候,会报第6个entry没有被三个节点中的任意一个apply。 看了看日志,发现第6个entry一直…

    Linux 2023年6月7日
    0101
  • PHP获取前一天,前一个月,前半年,前一年的时间戳

    // 获取前一秒 strtotime("-1 seconds"); // 获取前一分钟 strtotime("-1 minute"); //…

    Linux 2023年6月13日
    0170
  • 数据转换-位串字节数组

    一、任务详情 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务 1 参考《GMT 0009-2012 SM2密码算法使用规范》第6节”…

    Linux 2023年6月8日
    0108
  • wget命令8种实用用法

    大家好,我是良许。 wget 是一个可以从网络上下载文件的免费实用程序,它的工作原理是从 Internet 上获取数据,并将其保存到本地文件中或显示在你的终端上。 这实际上也是大家…

    Linux 2023年6月14日
    089
  • burp通过条件竞争上传文件

    一·何为条件竞争现代框架更能抵御此类攻击。他们通常不会将文件直接上传到文件系统上的预期目的地。相反,他们采取了预防措施,例如首先上传到临时的沙盒目录并随机命名以避免覆盖现有文件。然…

    Linux 2023年6月7日
    089
  • redis五种数据类型及使用场景

    一、Redis简介Redis 是一个开源的使用 C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库。它可以用作数据库、缓存和消息中间件。它支持的数据类…

    Linux 2023年5月28日
    089
  • zabbix

    1. zabbix介绍 2. zabbix特点 3. zabbix配置文件 4. 部署zabbix zabbix介绍 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监…

    Linux 2023年6月7日
    0161
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球