目录
第一章
shell:命令解释器
#解读
[root@GBL ~]#
root:登录用户 GBL:主机名称 ~:现在所在工作目录为登录用户的家目录 #:登录用户为超级用户root
#查看主机名
[root@GBL ~]# cat /etc/hostname
GBL
#获取IP
方法一
[root@GBL ~]#dhclient
方法二:
[root@GBL ~]# nmcli connection up 网卡名称
#查看当前LINUX的发行版信息
[root@GBL ~]# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.1 (Ootpa)
#查看内核版本
[root@GBL ~]# uname -r
4.18.0-147.el8.x86_64
#查看当前登录用户
[root@GBL ~]# whoami
root
[root@GBL ~]# who am i
root pts/1 2022-08-22 08:22 (192.168.222.1)
#临时修改主机名
[root@GBL ~]# hostname 新名称
#永久修改主机名
[root@GBL ~]# hostnamectl set-hostname 新名称
1.3.3 cd ls命令
#切换目录
[root@GBL ~]# cd / -----工作目录切换到/目录
[root@GBL /]# cd -----工作目录切换到~目录
[root@GBL ~]# cd ~
[root@GBL ~]#
[root@GBL ~]# cd .. ------切换上一级
[root@GBL /]#
[root@GBL /]# cd . ------切换到当前(没啥用)
[root@GBL /]#
[root@GBL ~]# cd /
[root@GBL /]# cd /test
[root@GBL test]# cd - -----切换到之前的工作目录
/
#linux 中一切皆文件
**ls**
1.不加后缀 查看当前目录
2.ls -l (ll) 列表形式显示信息
3.ls -a 显示隐藏文件
4.ls -hl 以长列表显示出来,以人类可读方式显示文件大小
5.ls -dl ~ 长格式查看目录本身信息
6.ls -R 查看本身有哪些内容,有颜色表示文件夹 .cfg表示配置文件
7.ls -i 显示文件索引节点,一个索引节点表示一个文件
#ls 查看当前目录文件里有什么
[root@GBL /]# ls
bin dev home lib64 mnt proc run srv test usr
boot etc lib media opt root sbin sys tmp var
[root@GBL /]# cd dev
[root@GBL dev]# cd /home -----同级切换,使用绝对路径
[root@GBL /]# cd lib
[root@GBL lib]# pwd
/lib
#重置root密码
第二章 linux中执行命令
2.1命令格式
#命令后不跟操作对象,默认为当前
[root@GBL ~]# pwd
/root
[root@GBL ~]# ls
a Music
a2 Pictures
anaconda-ks.cfg Public
Desktop sl-5.02-1.el8.x86_64.rpm
Documents Templates
Downloads test
initial-setup-ks.cfg Videos
[root@GBL ~]# ls /root
a Music
a2 Pictures
anaconda-ks.cfg Public
Desktop sl-5.02-1.el8.x86_64.rpm
Documents Templates
Downloads test
initial-setup-ks.cfg Videos
[root@GBL ~]#
#命令分两类 ①内置builtin ②外部命令
[root@GBL ~]# type cd
cd is a shell builtin ----内置命令
[root@GBL ~]# type man
man is hashed (/usr/bin/man) ----man命令的可执行文件
[root@GBL ~]# type cat
cat is hashed (/usr/bin/cat) -----cat命令的可执行文件
2.2 查看命令帮助
- help 命令
- 命令 --help
- man 命令
- info page
2.3 date 命令
[root@GBL ~]# date
Sun Aug 21 20:49:01 CST 2022
[root@GBL ~]# date -R
Sun, 21 Aug 2022 20:58:30 +0800
[root@GBL ~]# date -s "20201101 8:30:00"----空格分隔符后面一般跟下一个命令,这用“”
Sun Nov 1 08:30:00 CST 2020
[root@GBL ~]# date 110108302020
Sun Nov 1 08:30:00 CST 2020
[root@GBL ~]# date "+%j"
306
[root@GBL ~]# date "+%Y-%m-%d %H:%M:%S"
2020-11-01 08:34:17
2.4timedate命令
2.5 ls命令
[root@GBL ~]# ls ------ls命令一般查看目录下的文件
a Documents
a2 Downloads
aa1 initial-setup-ks.cfg
aa{1-3} Music
aa2 Pictures
aa3 Public
anaconda-ks.cfg sl-5.02-1.el8.x86_64.rpm
b Templates
c test
Desktop Videos
[root@GBL ~]# ls -d ~ -----选项d表示查看目录本身内容
/root
[root@GBL ~]# ls -dl ~
dr-xr-x---. 16 root root 4096 Aug 22 15:23 /root
第三章 Linux的文件类型和根目录结构
软链接与硬链接
文件类型:
d目录文件
l 软链接文件 箭头指向链接到的文件
_ 普通文件 类似Windows记事本 图片等
c,b,p,s 字符设备 块设备 管道设备 套接字文件
3.2linux的目录结构
3.2.2 路径以及工作目录
第四章 从命令行管理文件
4.1创建链接文件
[root@GBL ~]# mkdir /test
[root@GBL ~]# cd /test
[root@GBL test]# ll
total 0
[root@GBL test]# touch a
[root@GBL test]# ll -i
total 0
17395869 -rw-r--r--. 1 root root 0 Aug 22 21:14 a
[root@GBL test]# ln a a.hard
[root@GBL test]# ll -i
total 0
17395869 -rw-r--r--. 2 root root 0 Aug 22 21:14 a
17395869 -rw-r--r--. 2 root root 0 Aug 22 21:14 a.hard
[root@GBL test]# ln -s a a.soft
[root@GBL test]# ll -i
total 0
17395869 -rw-r--r--. 2 root root 0 Aug 22 21:14 a
17395869 -rw-r--r--. 2 root root 0 Aug 22 21:14 a.hard
17395870 lrwxrwxrwx. 1 root root 1 Aug 22 21:15 a.soft -> a
[root@GBL test]#
4.2目录操作命令
#创建上下级目录 选项-p
[root@GBL test]# cd /
[root@GBL /]# mkdir -p /test/test2/test1
#创建同级目录
法1
[root@GBL /]# mkdir /test/{hi,hihi,hih}
[root@GBL /]# ll /test
total 0
-rw-r--r--. 2 root root 0 Aug 22 21:14 a
-rw-r--r--. 2 root root 0 Aug 22 21:14 a.hard
lrwxrwxrwx. 1 root root 1 Aug 22 21:15 a.soft -> a
drwxr-xr-x. 2 root root 6 Aug 22 21:28 hi
drwxr-xr-x. 2 root root 6 Aug 22 21:28 hih
drwxr-xr-x. 2 root root 6 Aug 22 21:28 hihi
drwxr-xr-x. 3 root root 19 Aug 22 21:22 test2
法2
[root@GBL /]# mkdir /test/test3 /test/test4
[root@GBL /]# ll /test
total 0
-rw-r--r--. 2 root root 0 Aug 22 21:14 a
-rw-r--r--. 2 root root 0 Aug 22 21:14 a.hard
lrwxrwxrwx. 1 root root 1 Aug 22 21:15 a.soft -> a
drwxr-xr-x. 2 root root 6 Aug 22 21:28 hi
drwxr-xr-x. 2 root root 6 Aug 22 21:28 hih
drwxr-xr-x. 2 root root 6 Aug 22 21:28 hihi
drwxr-xr-x. 3 root root 19 Aug 22 21:22 test2
drwxr-xr-x. 2 root root 6 Aug 22 21:32 test3
drwxr-xr-x. 2 root root 6 Aug 22 21:32 test4
4.3普通文件的创建和删除
#创建普通文件
[root@GBL /]# touch a b c
[root@GBL /]# touch {1..3} #用法2:touch m{1..2}
[root@GBL /]# touch {4,6}
[root@GBL /]# ll
total 30
-rw-r--r--. 1 root root 0 Aug 22 21:34 1
-rw-r--r--. 1 root root 0 Aug 22 21:34 2
-rw-r--r--. 1 root root 0 Aug 22 21:34 3
-rw-r--r--. 1 root root 0 Aug 22 21:35 4
-rw-r--r--. 1 root root 0 Aug 22 21:35 6
-rw-r--r--. 1 root root 0 Aug 22 21:34 a
-rw-r--r--. 1 root root 0 Aug 22 21:34 b
-rw-r--r--. 1 root root 0 Aug 22 21:34 c
[root@GBL test]# touch {1,3}{a,b}
[root@GBL test]# ls
1a 1b 3a 3b
#删除普通文件
/t1/t2/t3
t2:a b c d e
[root@GBL /]# mkdir -p /m1/m2/m3
[root@GBL /]# mkdir m1/{a,b}
[root@GBL /]# ls
bin dev home lib64 media opt root sbin sys usr
boot etc lib m1 mnt proc run srv tmp var
[root@GBL /]# cd m1
[root@GBL m1]# ls
a b m2
[root@GBL m1]# touch q w
[root@GBL m1]# ls
a b m2 q w
[root@GBL m1]# rm -rf m2
[root@GBL m1]# ls
a b q w
[root@GBL m1]# cd m2
-bash: cd: m2: No such file or directory
[root@GBL m1]#
[root@GBL /]# rm -rf t1 ------选项rf可联用,rm可以用来删除目录
#
删除前会询问:[root@GBL test]# rm 文件名
强制删除:[root@GBL test]# rm -f 文件名
rm -rf可联用
#
删除目录 rmdir 目录
软链接和硬链接
4.4数据流和重定向
[root@GBL test]# date
Tue Aug 23 09:19:54 CST 2022
[root@GBL test]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 23 08:53 1a
-rw-r--r--. 1 root root 0 Aug 23 08:53 1b
-rw-r--r--. 1 root root 0 Aug 23 08:53 3a
-rw-r--r--. 1 root root 0 Aug 23 08:53 3b
[root@GBL test]# date
Tue Aug 23 09:20:14 CST 2022
[root@GBL test]# date >> 1a
[root@GBL test]# cat 1a
Tue Aug 23 09:20:22 CST 2022
[root@GBL test]# ls >> 1a
[root@GBL test]# cat 1a
Tue Aug 23 09:20:22 CST 2022
1a
1b
3a
3b
[root@GBL test]# echo 123 > c
[root@GBL test]# ls
1a 1b 3a 3b c
[root@GBL test]# cat c
123
[root@GBL test]#
[root@GBL test]# ls /home /asasss > list.txt
ls: cannot access '/asasss': No such file or directory
[root@GBL test]# cat list.txt
/home:
redhat
[root@GBL test]# ls /home /asasss > list.txt 2>list.txt
[root@GBL test]# cat list.txt
/home:
redhat
ess '/asasss': No such file or directory
[root@GBL test]# cat > q
q
we
sd
^C
[root@GBL test]# ll
total 16
-rw-r--r--. 1 root root 41 Aug 23 09:20 1a
-rw-r--r--. 1 root root 0 Aug 23 08:53 1b
-rw-r--r--. 1 root root 0 Aug 23 08:53 3a
-rw-r--r--. 1 root root 0 Aug 23 08:53 3b
-rw-r--r--. 1 root root 4 Aug 23 09:21 c
-rw-r--r--. 1 root root 55 Aug 23 09:33 list.txt
-rw-r--r--. 1 root root 9 Aug 23 09:39 q
[root@GBL test]# cat q
q
we
sd
[root@GBL test]#
4.5将标准输出重定向到文件
[root@GBL test]# echo $HOME
/root
[root@GBL test]# csa=1
[root@GBL test]# echo $csa
1
[root@GBL test]# echo test > file11
[root@GBL test]# echo test2 >> file11
[root@GBL test]# cat file11
test
test2
[root@GBL test]#
4.6 tee命令读取数据输出到文件
[root@GBL test]# date | tee 1a -----date命令的输出,1a文件和屏幕上都有输出
Tue Aug 23 10:02:11 CST 2022
[root@GBL test]# cat 1a
Tue Aug 23 10:02:11 CST 2022
[root@GBL test]# date | tee -a 1a -----" -a "追加
Tue Aug 23 10:03:33 CST 2022
[root@GBL test]# cat 1a
Tue Aug 23 10:02:11 CST 2022
Tue Aug 23 10:03:33 CST 2022
[root@GBL test]# date | tee x --------创建原目录下没有的文件
Tue Aug 23 10:03:54 CST 2022
[root@GBL test]# cat x
Tue Aug 23 10:03:54 CST 2022
[root@GBL test]#
[root@GBL test]# echo 123 >> x1
[root@GBL test]#
[root@GBL test]# touch x2 < x1 ----这也可以创建文件x2,但x1的内容无法输入到x2
[root@GBL test]# cat x2
[root@GBL test]# cat x1
123
[root@GBL test]#
4.7 vim编辑器
4.8 cp mv
[root@GBL test]# mkdir dir
[root@GBL test]# touch a
[root@GBL test]# cp a dir ------cp:复制
[root@GBL test]# ll dir
total 0
-rw-r--r--. 1 root root 0 Aug 23 10:23 a
[root@GBL test]# ls
a dir
[root@GBL test]# mv a dir ------mv:移动
mv: overwrite 'dir/a'? y
[root@GBL test]# ls dir
a
[root@GBL test]# ls
dir
[root@GBL test]# touch vb
[root@GBL test]# ls
dir vb
[root@GBL test]# cp vb vb.1
[root@GBL test]# ls
dir vb vb.1
[root@GBL test]# mv vb ./vb2
[root@GBL test]# ls
dir vb.1 vb2
[root@GBL test]#
第五章 文件内容显示
5.1 浏览普通文件内容
5.2 grep 过滤文件内容显示
5.3 cut 切割显示
5.4 sort 排序显示
5.5 uniq 去重显示
5.6 tr 替换文件中的字符显示
#查看文件内容
cat---查看文件内容
tac---倒序查看文件内容
more 和 less 区别: more自动退出 less按q手动退出
more -行号 文件名------- 指定一次有几行去翻页,less和cat无此用法
#只看开头/结尾 的几行
head 文件名 ------默认查看开头前10行
head/tail -行号 文件名 ------ 查看前几行/后几行
head -7 文件名 | tail -1-------只查看某一文件的第七行
tail 文件名------查看结尾后10行
tail -f 文件名-----不停读取和显示文件最新内容
#grep 过滤文件内容显示
命令格式:grep 选项 关键字符串 文件名称
grep -n xxx 文件名-----显示行号
grep ^xxx 文件名-----显示以xxx开始的行
grep xxx$ 文件名-----显示以xxx结尾的行
#空行 显示文件中空行 并标记行号 ?
grep r.*t 文件名---过滤以r开头d结尾
grep 'r . . t' 文件名 -----只过滤四个字母
grep r . . t 文件名---过滤四个字母
grep [123] 文件名----匹配[]里面任意一个
grep ^[^qf] 文件名----不匹配以q/f开头的行
#切割显示
选项:-d间隔符号 -f设置需要看的列数 -c
用法
1. cut -d : -f 列数 文件名
2. cut -d : -f 1,2 文件名-----选不同多列,用逗号隔开
3. cut -d : -f 1-4 文件名----- 选1-4连续的几列
#排序显示SORT
sort 文件名------按每行首字符大小排序---先排符号,数字,字母
sort -n 文件名------按数字大小排序
sort -u 文件名-----排序后去重(消除文件中重复内容,保留一个)
#去重显示uniq
uniq 文件名------消除连续行重复内容,保留1个
#替换文件中的字符 tr
*tr 将原来内容替换新的内容 再输出
[root@GBL test]# cat a
hi
hello
my name
is GBL
my phone
1092839
as
1324
[root@GBL test]# cat a | tr as 1 -------文件a中的a和s都替换为1
hi
hello
my n1me
i1 GBL
my phone
1092839
11
1324
[root@GBL test]# cat a | tr hi o ------文件a中的h和i都替换为o
oo
oello
my name
os GBL
my poone
1092839
as
1324
[root@GBL test]# cat a | tr "is GBL" " " -----文件a中的 "is GBL" 替换为空(相当于把内容删除)
h
hello
my name
my phone
1092839
a
1324
第六章 文件的其他操作命令
6.1 wc 文本内容统计
[root@GBL test]# wc -l a -------行数
8 a
[root@GBL test]# wc -w a -------单词数
11 a
[root@GBL test]# wc -c a -------字节数
51 a
[root@GBL test]# cat -n a
1 hi
2 hello
3 my name
4 is GBL
5 my phone
6 1092839
7 as
8 1324
6.2 cp mv 复制移动文件
-a 等同于-d -p -r
#方法一
[root@GBL test]# cat /etc/passwd | cut -d : -f 1 | sort -r
[root@GBL test]# cut -d : -f 1 passwd | sort -r
#方法二
[root@GBL test]# hostname -I
192.168.222.128 192.168.122.1
6.3 查找文件路径
[root@GBL ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@GBL ~]# /usr/bin/ls
anaconda-ks.cfg Pictures
Desktop Public
Documents sl-5.02-1.el8.x86_64.rpm
Downloads Templates
initial-setup-ks.cfg Videos
Music
[root@GBL ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@GBL ~]# find /test -name a
/test/dir/a
/test/a
[root@GBL ~]# find /etc -name "host*"
/etc/host.conf
/etc/hosts
/etc/hostname
/etc/avahi/hosts
[root@GBL ~]# find . -name "a*"
./anaconda-ks.cfg
./.cache/evolution/addressbook
./.local/share/gnome-shell/application_state
./.local/share/evolution/addressbook
./.local/share/applications
[root@GBL ~]# cd /test
[root@GBL test]# find . -name "a*"
./dir/a
./a
[root@GBL test]# find ./* -name '*a'
./a
./dir/a
[root@GBL test]#
[root@GBL test]# find ./* -name '*.txt' -exec cp {} ./cp.txt \;
#find 不和 管道符联用
[root@GBL test]# find ./* -name '*.txt' | cp ./cp.txt
cp: missing destination file operand after './cp.txt'
Try 'cp --help' for more information.
6.4 压缩和解压缩
6.5 tar 归档命令
[root@GBL test]# tar cvf passwd.tar passwd
passwd
[root@GBL test]# ls
a dir passwd passwd.tar vb.1 vb2
[root@GBL test]# tar tf passwd.tar
passwd
[root@GBL test]# touch e r t
[root@GBL test]# tar rvf passwd.tar e
e
[root@GBL test]# tar tf passwd.tar
passwd
e
[root@GBL test]# tar czf passwd.tar.gz passwd
[root@GBL test]# ls
a e passwd.tar r vb.1
dir passwd passwd.tar.gz t vb2
[root@GBL test]# tar rvf passwd.tar.gz e
tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now
[root@GBL test]#
[root@GBL test]# tar xvf passwd.tar.gz -C ./dir
passwd
[root@GBL test]# ll dir
total 0
-rw-r--r--. 1 root root 0 Aug 23 10:23 a
drwxr-xr-x. 2 root root 6 Aug 23 21:19 c
drwxr-xr-x. 2 root root 6 Aug 23 21:19 d
-rw-r--r--. 1 root root 0 Aug 23 21:21 m1
-rw-r--r--. 1 root root 0 Aug 23 21:21 m2
-rw-r--r--. 1 root root 0 Aug 23 21:31 passwd
[root@GBL test]#
第七章 命令解释器 SHELL
7.1 shell中的变量
- 普通变量(局部变量)
- 环境变量(全局变量)
-
#普通变量的赋值 [root@GBL ~]# name=123 [root@GBL ~]# echo $name 123 [root@GBL ~]# name='123' [root@GBL ~]# echo $name 123 [root@GBL ~]# name="123" [root@GBL ~]# echo $name 123 [root@GBL ~]# [root@GBL ~]# name=`pwd` [root@GBL ~]# echo $name /root [root@GBL ~]# name=$(pwd) [root@GBL ~]# echo $name /root [root@GBL ~]# [root@GBL test]# vim var.sh [root@GBL test]# chmod +x var.sh [root@GBL test]# ./var.sh 1
环境变量
7.2命令历史 history
7.3命令别名 alias
1.命令格式:alias 别名=’ 原命令‘
2.linux中提供alias永久化的方法
#暂时性修改
[root@GBL /]# alias cdt='cd /test'
[root@GBL /]# cdt
[root@GBL test]# su redhat
[redhat@GBL test]$ cdt
bash: cdt: command not found...
Failed to search for file: Cannot update read-only repo
#永久性修改(修改配置文件)
[root@GBL ~]# cdt
[root@GBL test]# su redhat
[redhat@GBL test]$ cdt
[redhat@GBL test]$
7.4 linux中的特殊符号
#符号 ;使用分号分隔多条命令
[root@GBL ~]# pwd ;cat /test/a
/root
hi
hello
my name
is GBL
my phone
1092839
as
1324
#符号 \ 使\后面的特殊字符失效,换行后系统自动显示提示符>
[root@GBL ~]# cat a \
>
#
?:匹配任意单个字符
` :反引号一般用于引用命令
'':单引号里所有特殊字符失去含义
" " :双引号里部分特殊字符保留原有含义
7.5linux中的快捷键
ctrl+a | 光标移动到当前行开头 |
ctrl+e | 光标移动到当前行结尾 |
ctrl+u | 删除光标前的内容 ,光标所在内容不会删除 |
ctrl+k | 删除光标后的内容,光标所在内容也会删除 |
第八章 用户和组管理
8.1用户和组的分类
1.linux下的用户分为三类:超级用户root 系统用户 普通用户
linux下的组:基本组,附加组
2.用户和组的配置文件
用户账号文件 | /etc/passwd,该文件中,系统用户uid 1-999 普通用户uid >=1000 超级用户uid 0,/bin/bash 和 /sbin/nologin 是否可登录 |
用户密码文件 | /etc/shadow |
组账号文件 | /etc/group |
组密码文件 | /etc/gshadow |
8.2 用户管理
#1.添加新用户
#选项 -g:为用户指定基本组 -G指定附加组 -s设置可登录
[root@GBL ~]# useradd test1
[root@GBL ~]# tail -1 /etc/shadow
test1:!!:19229:0:99999:7::: ------感叹号表示未给test1用户设置密码
[root@GBL ~]# passwd test1 --------设置密码123
New password:
Retype new password:
[root@GBL ~]# tail -1 /etc/shadow
test1:$6$PGsAXhnDv7Gf8Bwu$Ype3.3AIcxxbfCl9l3Dpr93D4Ajb9mxDjmAQEHZbGdJKjTlfMEtP6o.G7BW0NRtJTYq7KXKbAYe8K.7LNnm2k0:19229:0:99999:7:::
[root@GBL ~]# useradd test2 -p 123 ------在创建test2用户时去设置密码,注意:-p后跟加密的密码
[root@GBL ~]# tail -1 /etc/shadow
test2:123:19229:0:99999:7:::
#2.修改用户信息
命令格式: usermod [选项] username
选项: -i 新用户名 :更改账户名称,必须在用户未登录情况下才用
#3.为用户账号设置密码
方式1:passwd
修改密码:方式2:echo 密码 | passwd --stdin 用户名
#4.删除用户账号 userdel
userdel [-r] 账号名 -----r选项,删除该账号时,一并删除账号对应主目录,此时主目录对应的所属者和所属组会发生变化
[root@GBL ~]# ll /home
total 4
drwx------. 15 redhat redhat 4096 Aug 24 16:36 redhat
drwx------. 3 test1 test1 78 Aug 25 08:21 test1
drwx------. 3 test2 test2 78 Aug 25 08:34 test2
[root@GBL ~]# userdel test1 -----删除账号,账号对应主目录保留
[root@GBL ~]# ll /home
total 4
drwx------. 15 redhat redhat 4096 Aug 24 16:36 redhat
drwx------. 3 1001 1001 78 Aug 25 08:21 test1
drwx------. 3 test2 test2 78 Aug 25 08:34 test2
[root@GBL ~]# userdel -r test2 ----账号和主目录一并删除
#5.用户间切换 su命令
su 和 su - 命令的区别在于加载的配置文件不同
#6.控制用户对系统命令的使用权限 sudo
[root@GBL ~]# su redhat
[redhat@GBL root]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[redhat@GBL root]$ sudo cat /etc/shadow
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for redhat:
root:$6$EOcEges3dkzDaRrz$nurkYwv.YYHr8YdNp8CRiRuyLAX7hT1tDs28WWoD2Q0cNsb22QGODwRGfrC1a9lEG3L3WeBiu.b5ssTX4Cdyx1:19227:0:99999:7:::
tcpdump:!!:19219::::::
redhat:$6$TbqE2nL1ki3qo4er$adoKII34NF4reMyOvWlrYlgxeRWDZTx2w5UQ8ImCuq2.NtsIgYtVqwxoukgJbhPW.m4BZCM6f4PpZIM.tRLHh.::0:99999:7:::
apache:!!:19225::::::
test2:123:19229:0:99999:7:::
[redhat@GBL root]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[redhat@GBL root]$
详细说sudo
sudo命令:提高普通用户的操作权限,这个权限需要root用户进行配置/etc/sudoers文件才可使用。配置/etc/sudoers文件的两种方式:1.vim /etc/sudoers 2.visudo
8.3 用户组管理
1.创建用户组
命令格式:groupadd [-r] 用户组名称
2.修改用户组属性
groupmod 选项 用户组
3.添加/删除组成员(只有root用户和组管理员才可使用该命令)
gpasswd [选项] [用户] [组]
4.删除组账号
groupdel 用户组名
注意:删除一个基本组,1.用户和其基本组一起删除 2给用户换个基本组,原先基本组中没有任何用户
8.4查询用户和组信息
显示某个用户的详细信息 :id 用户名
显示用户的所属组信息:groups 用户名
8.5查看用户登录系统的情况
1.查看当前登录系统的用户 users
此处三个登录用户root,有一个是虚拟机上登陆的root用户,其余两个都是在xshell上登陆的root用户。
2.列出目前与过去登入系统的用户相关信息 last,此命令默认读取/var/log/wtmp文件
tty : VM上的终端
pts:虚拟终端,XSHELL上的终端
3.查看每个账号的最近登陆时间,lastlog,该命令会读取/var/log/lastlog文件
4.显示登录到系统的用户信息 w
5.显示目前登录到系统的用户 who,who 通常通过/var/run/utmp文件来获取信息
[root@GBL ~]# whoami #当前所登陆的用户
root
[root@GBL ~]# who am i
root pts/1 2022-08-25 08:15 (192.168.222.1)
[root@GBL ~]#
[root@GBL ~]# su redhat
[redhat@GBL root]$ whoami #当前所登陆的用户
redhat
[redhat@GBL root]$ who am i #会话登录的用户
root pts/1 2022-08-25 08:15 (192.168.222.1)
[redhat@GBL root]$
第九章 Linux文件系统权限
root账号不受文件权限的r,w权限限制,x权限受限制
1.修改文件权限方法
文件 | 原权限 | 更改后的权限 | 方法 |
c | r-x r-x r-x | r-x rwx rwx | 1.g,o+w 2.g=rwx,o=rwx |
a | rwx r-x r-x | rwx r-- --- | 1.g-x,o-r,x 2.g=r--,o=- |
b | r-x r-x r-x | rwx rwx rwx | 1.777 2.a+w 3.a=rwx |
[root@GBL ~]# touch a
[root@GBL ~]# ls a
a
[root@GBL ~]# ll a
-rw-r--r--. 1 root root 0 Aug 25 14:00 a
[root@GBL ~]# chmod g-r,o-r a
[root@GBL ~]# ll a
-rw-------. 1 root root 0 Aug 25 14:00 a
[root@GBL ~]# chmod u=r--,o=r-- a
[root@GBL ~]# ll a
-r-----r--. 1 root root 0 Aug 25 14:00 a
[root@GBL ~]# touch b
[root@GBL ~]# ll b
-rw-r--r--. 1 root root 0 Aug 25 14:02 b
[root@GBL ~]# chmod g=r+w,o=- b
[root@GBL ~]# ll b
-rw-rw----. 1 root root 0 Aug 25 14:02 b
#修改文件a的所属者和所属组,前提是更换的所属者和所属组都存在
[root@localhost ~]# ll a
-rw-r--r--. 1 root root 0 Sep 6 10:42 a
[root@localhost ~]# chown redhat:redhat a-----同时更换所属者和所属组
[root@localhost ~]# ll a
-rw-r--r--. 1 redhat redhat 0 Sep 6 10:42 a
[root@localhost ~]#
[root@localhost ~]# chown :root a----更换所属组
[root@localhost ~]# ll a
-rw-r--r--. 1 redhat root 0 Sep 6 10:42 a
[root@localhost ~]# chown root a----更换所属者
[root@localhost ~]# ll a
-rw-r--r--. 1 root root 0 Sep 6 10:42 a
[root@localhost ~]#
[root@localhost ~]# chown :xixi a -----更换的所属组名称不存在时,无法更换
chown: invalid group: ‘:xixi’
[root@localhost ~]# chown hah a
chown: invalid user: ‘hah’
[root@localhost ~]#
对于文件和目录来说,r w x有着不同的含义
文件:--x可以用,尽量不用
目录:r--是不可能出现的,必须是r-x
9.2文件和目录的特殊权限
查找命令的可执行文件
whereis which locate find
1.SUID权限,让一般用户在执行某些程序的时候,在程序运行期间,暂时获得该程序文件所属者权限。u+s
[root@localhost ~]# which passwd
/usr/bin/passwd
[root@localhost ~]# passwd
Changing password for user root.
New password:
[root@localhost ~]# su redhat
[redhat@localhost root]$ passwd
Current password:
New password:
[root@localhost ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 33544 Dec 14 2019 /usr/bin/passwd
[root@localhost ~]# ll /etc/shadow
----------. 1 root root 1346 Sep 6 14:26 /etc/shadow
[root@localhost ~]#
2.SGID权限(g+s)
SGID是设置在A目录上,则A目录内建立的文件或目录的属组,将会是此A目录的所属组。
3.ACL权限,给指定的用户指定目录分配指定的权限
查看ACL权限 : getfacle 文件名
设定ACL权限:setfacl 选项 文件名
4.Sticky Bit 权限,当前只针对目录有效,对文件没有效果。(o+t)
5.权限掩码
第十章 管理linux的联网
10.1三种模式
桥接模式和NAT模式都可以连外网
仅主机模式:只能和主机相连
仅主机模式图示
NAT模式图示
桥接模式图示
10.2配置网络
网络接口这里是指计算机的网络接口即网卡设备,一个网络接口,可以有多个网络连接,但同一时间只能有一个网络连接处于活动状态。会话和网卡的关系:一个网卡可以有多个会话,但一个时刻只能连接到一个会话。
1.使用ip命令配置临时生效的网络连接
2.修改配置文件,前提需要有network服务
3.nmcli命令行工具
查看网卡设备
nmcli device 简写 nmcli d
nmcli device show
查看相应会话
nmcli connection 简写 nmcli c
nmcli connection show
配置已存在会话的网卡的IP地址
#非交互式
nmcli c mo ens160 ipv4.method manual ipv4.addresses 192.168.150.200/24 ipv4.gateway 192.168.150.2 ipv4.dns 223.5.5.5 autoconnect yes
连接,断开,删除会话
nmcli c up ethx
nmcli c down ethx
nmcli c delete ethx
10.3 从网站下载文件
1.wget命令用于在终端命令行下载网络文件,语法:wget [选项] 网址
2.curl命令是一个网络工具,作用:通过http,https,ftp等方式下载或上传文件
第十一章 管理LINUX软件包和进程
管理工具:
rpm 和 yum/dnf
[root@localhost ~]# rpm -qf /usr/bin/tree
tree-1.7.0-15.el8.x86_64
[root@localhost ~]# rpm -qi tree
Name : tree
Version : 1.7.0
Release : 15.el8
Architecture: x86_64
Install Date: Sun 28 Aug 2022 08:11:12 PM CST
Group : Unspecified
Size : 111611
License : GPLv2+
Signature : RSA/SHA256, Thu 08 Nov 2018 01:20:51 AM CST, Key ID 199e2f91fd431d51
Source RPM : tree-1.7.0-15.el8.src.rpm
Build Date : Thu 08 Nov 2018 12:49:35 AM CST
Build Host : x86-vm-10.build.eng.bos.redhat.com
Relocations : (not relocatable)
Packager : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Vendor : Red Hat, Inc.
URL : http://mama.indstate.edu/users/ice/tree/
Summary : File system tree viewer
Description :
The tree utility recursively displays the contents of directories in a
tree-like format. Tree is basically a UNIX port of the DOS tree
utility.
[root@localhost ~]# rpm -ql tree
/usr/bin/tree
/usr/lib/.build-id
/usr/lib/.build-id/50
/usr/lib/.build-id/50/ae300877a586b9e7b87f19dfe58d303a887fb9
/usr/share/doc/tree
/usr/share/doc/tree/LICENSE
/usr/share/doc/tree/README
/usr/share/man/man1/tree.1.gz
[root@localhost ~]# rpm -qa tree
tree-1.7.0-15.el8.x86_64
[root@localhost ~]# which httpd
/usr/sbin/httpd
[root@localhost ~]# rpm -qf /usr/sbin/httpd
httpd-2.4.37-41.module+el8.5.0+11772+c8e0c271.x86_64
[root@localhost ~]# rpm -ql httpd
/etc/httpd/conf
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
......
....
...
[root@localhost ~]# rpm -qa httpd
httpd-2.4.37-41.module+el8.5.0+11772+c8e0c271.x86_64
[root@localhost ~]#
[root@localhost ~]# rpm -qi httpd
Name : httpd
Version : 2.4.37
Release : 41.module+el8.5.0+11772+c8e0c271
Architecture: x86_64
Install Date: Fri 02 Sep 2022 11:56:11 AM CST
Group : System Environment/Daemons
Size : 4494146
License : ASL 2.0
Signature : RSA/SHA256, Sat 17 Jul 2021 04:10:19 PM CST, Key ID 199e2f91fd431d51
Source RPM : httpd-2.4.37-41.module+el8.5.0+11772+c8e0c271.src.rpm
Build Date : Tue 13 Jul 2021 05:40:41 AM CST
Build Host : x86-vm-07.build.eng.bos.redhat.com
Relocations : (not relocatable)
Packager : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Vendor : Red Hat, Inc.
URL : https://httpd.apache.org/
Summary : Apache HTTP Server
Description :
The Apache HTTP Server is a powerful, efficient, and extensible
web server.
[root@localhost ~]#
查看进程
静态查看进程:ps
pstree(进程树)
动态查看进程:top
信号控制进程
kill -9 pid(进程号)
killall vim
killall httpd
十二章磁盘:存储设备
拿到一个磁盘,要做如下三步骤:
1.分区
管理,划分分区三工具:
- fdisk:默认化为MBR格式的分区, MBR分区:4个主分区或3主分区带1扩展分区(在扩展分区里划分逻辑分区) 一个分区最大2.2TB
- gdisk:默认将磁盘化为GPT格式的分区, GPT格式:默认情况下,支持128个分区
- parted
2.对每块分区进行格式化
格式化:在存储设备上组织文件方法
#命令
mkfs.格式 分区的设备名
#格式
格式有:xfs ext2(无日志) ext3(有日志) ext4
3.挂载
什么是挂载?挂载就是将分区或设备挂载至挂载点目录,挂载后可以对磁盘上的文件进行正常的读写
#挂载命令
mount 设备名 挂载点目录
mount -u uuid 挂载点目录
#查看挂载信息
mount | grep /dev/sda
#卸载分区
umount 挂载点目录/存储设备名
#永久挂载
在/etc/fstab 配置文件下更改
管理交换分区
什么是交换分区?swap交换分区,类似于windows系统中的“虚拟内存”
查看磁盘空间使用量 df du
df:列出磁盘空间的占用情况
df [参数] [目录或文件名]
du:显示磁盘空间的使用量
du [参数] 目录或文件名
磁盘最小存储单元:扇区
操作系统对磁盘最小读取单位:簇/块,多扇区组成簇或块
查看磁盘信息:lsblk
查看格式化后的磁盘分区信息:blkid