[命令] Linux 命令 tar (打包、解包、压缩和解压文件或目录)

内容一:压缩并创建 (压缩) 包 (以压缩内容的维度进行分类)

1.1 压缩并创建 (压缩) 包 (文件)

1.1.1 压缩并创建 (压缩) 包 (某 1 个文件)
# tar zcvf file.tar file.txt

(补充:这里以压缩后创建 file.tar (压缩) 包,而 file.txt 是被压缩的目录为例)

1.1.2 压缩并创建 (压缩) 包 (某几个文件)
# tar zcvf tmp.tar file1.txt file2.txt file3.txt

(补充:这里以压缩后创建 file.tar (压缩) 包,而 file1.txt file2.txt 和 file3.txt 是被压缩的目录为例)

1.2 压缩并创建 (压缩) 包 (目录)

1.2.1 压缩并创建 (压缩) 包 (某 1 个目录)
# tar zcvf tmp.tar /tmp

(补充:这里以压缩后创建 tmp.tar (压缩) 包,而 /tmp 是被压缩的目录为例)

1.2.2 压缩并创建 (压缩) 包 (某几个目录)
# tar zcvf tmp.tar /tmp /var/tmp

(补充:这里以压缩后创建 tmp.tar (压缩) 包,而 /tmp 和 /var/tmp 是被压缩的目录为例)

内容二:压缩并创建 (压缩) 包 (以压缩格式的维度进行分类)

2.1 以 gzip 格式压缩并创建 (压缩) 包

# tar zcvf tmp.tar.gz /tmp

(补充:这里的 tmp.tar.gz 是创建的压缩包,而 /tmp 是被压缩的目录)

2.2 以 bzip2 格式压缩并创建 (压缩) 包

# tar jcvf test.tar.bz2 /tmp

(补充:这里的 tmp.tar.bz2 是创建的压缩包,而 /tmp 是被压缩的目录)

(注意:压缩后 bz2 后缀的文件比 gz 后缀的文件更小,但是花费的时间更长)

2.3 以 xz 格式压缩并创建 (压缩) 包

# tar Jcvf test.tar.xz /tmp

(补充:这里的 tmp.tar.xz 是创建的包,而 /tmp 是被压缩的目录)

(注意:压缩后 xz 后缀的文件比 bz2 后缀的文件更小,但是花费的时间更长)

内容三:解压 (压缩) 包

3.1 将 (压缩) 包解压到当前目录

# tar zxvf tmp.tar

或者:

# tar zxvf tmp.tar.gz

或者:

# tar jxvf tmp.tar.bz2

或者:

# tar Jxvf tmp.tar.xz

(补充:这里的 tmp.tar、tmp.tar.gz、tmp.tar.bz2、tmp.tar.xz 是要被解压的 (压缩) 包,它们分别是 gzip 格式的 (压缩) 包、gzip 格式的 (压缩) 包、bzip2 格式的 (压缩) 包、xz 格式的 (压缩) 包)

3.2 将 (压缩) 包解压到指定目录

# tar zxvf tmp.tar -C /tmp

或者:

# tar zxvf tmp.tar.gz -C /tmp

或者:

# tar jxvf tmp.tar.bz2 -C /tmp

或者:

# tar Jxvf tmp.tar.xz -C /tmp


补充:
1) 这里的 tmp.tar、tmp.tar.gz、tmp.tar.bz2、tmp.tar.xz 是要被解压的 (压缩) 包,它们分别是 gzip 格式的(压缩)包、gzip 格式的 (压缩) 包、bzip2 格式的 (压缩) 包、xz 格式的 (压缩) 包
2) 这里的 /tmp 是 (压缩) 包里的内容要被解压到目录

内容四:tar 命令的常用选项

1) –remove-files 压缩完成后删除原来的文件
2) –exclude= 排除某些文件不压缩

[步骤] Linux 图形桌面的安装 (GNOME 版) (openSUSE & SLES 版)

步骤一:系统环境要求

服务器系统要配置好可用的软件源

步骤二:显示 openSUSE & SLES 可安装的软件包组

# zypper se -t pattern

步骤三:安装 GNOME 图形桌面

3.1 安装 GNOME 图形桌面

# zypper in -t pattern gnome

3.2 将系统默认的图形桌面设置为 GNOME 图形桌面

# vim /etc/sysconfig/displaymanager

将以下内容:

......
DISPLAYMANAGER_XSERVER="Xorg"

修改为:

......
DISPLAYMANAGER_XSERVER="gdm"

步骤四:进入图形桌面

4.1 设置系统开机进入图形桌面

# systemctl set-default graphical.target

4.2 立刻进入图形桌面

# startx

或者:

# init 5

[步骤] bond 网卡捆绑组的添加(CentOS Linux & RHEL 版)

步骤一:显示现有的网卡有哪些

# nmcli connection show

步骤二:添加网卡捆绑组

2.1 添加网卡捆绑组

2.1.1 添加网卡捆绑组的格式
# nmcli connection add type bond con-name <connection name of network card binding group> ifname <name of network card binding group> mode <network card binding group type> miimon <delay time>
2.1.2 添加网卡捆绑组的案例
# nmcli connection add type bond con-name bond0 ifname bond0 mode active-backup miimon 100

(补充:这里以创建连接名为 bond0 设备名为 bond0 延迟为 100 毫秒的网卡捆绑组为例)

2.2 添加网卡捆绑组的子网卡

2.2.1 添加网卡捆绑组子网卡的格式
# nmcli connection add type bond-slave con-name <network card connection name> ifname <subnet card name> master <network card binding group type>
2.2.2 添加网卡捆绑组子网卡的案例
# nmcli connection add type bond-slave con-name bond0-eth0 ifname eth0 master bond0
# nmcli connection add type bond-slave con-name bond0-eth1 ifname eth1 master bond0

(补充:这里以将设备名为 eth0 和 eth1 的网卡添加到 bond0 网卡捆绑组,同时指定名称为 bond0-eth0 和 bond1-eth1 为例)

(注意:这里的 con-name 和 ifname 最好不要设置成一样的,否则后面不好管理)

步骤三:给网卡捆绑组配置 IP 地址

# nmcli connection modify bond0 ipv4.addresses 192.168.100.5/24 ipv4.gateway 192.168.100.1 autoconnect yes ipv4.method manual

(补充:这里给 bon0 网卡捆绑组添加 192.168.100.5/24 IP 地址,192.168.100.1 网关 IP 地址,开机自动启动并且把网卡设置成静态 IP 地址为例)

步骤四:启动网卡捆绑组

4.1 显示现有的网卡捆绑组和对应的子网卡有哪些

# nmcli connection show

4.2 启动网卡捆绑组里的子网卡

4.2.1 启动网卡捆绑组里子网卡的格式
# nmcli connection up <subnet card name>
4.2.2 启动网卡捆绑组里的子网卡
# nmcli connection up bond0-eth0
# nmcli connection up bond0-eth1

(补充:这里以重启 eth0 或者 eth1 为例)

或者:

# nmcli connection reload

4.3 启动网卡捆绑组

4.3.1 启动网卡捆绑组的格式
# nmcli connection up <connection name of network card binding group>
4.3.2 启动网卡捆绑组的案例
# nmcli connection up bond0

(补充:这里以启动 bond0 网卡组为例)

步骤五:确认网卡捆绑组的 IP 地址配置成功

# ip address show 

(补充:如果网卡组里出现了我们配置的 IP 地址,则代表 IP 地址配置成功)

步骤六:显示网卡捆绑组的子网卡

6.1 显示网卡捆绑组的成员格式

# cat /proc/net/bonding/<bond name>

6.2 显示网卡捆绑组的成员的案例

# cat /proc/net/bonding/bond0

(补充:这里以显示网卡组 bond0 的子网卡为例)

步骤七:测试 bond 网卡捆包组实现了主从互备

7.1 检查第 2 张子网卡是否工作正常

7.1.1 停用第 1 张子网卡
# ifconfig eth0 down

(补充:这里以停用子网卡 bond0-eth0 为例)

7.1.2 检查现在网络是否正常

(步骤略)

7.1.3 检查现在 bond 网卡捆绑组活跃的子网卡是不是第 2 张子网卡
# cat /proc/net/bonding/bond0

(补充:这里以检查现在 bond 网卡捆绑组 bond0 活跃的子网卡是不是第 2 张子网卡为例)

7.1.4 启动第 1 张子网卡
# ifconfig eth0 up

(补充:这里以启用子网卡 bond0-eth0 为例)

7.2 检查第 1 张子网卡是否工作正常

7.2.1 停用第 2 张子网卡
# ifconfig eth1 down

(补充:这里以停用子网卡 bond0-eth1 为例)

7.2.2 检查现在网络是否正常

(步骤略)

7.2.3 检查现在 bond 网卡捆绑组活跃的子网卡是不是第 2 张子网卡
# cat /proc/net/bonding/bond0

(补充:这里以检查现在 bond 网卡捆绑组 bond0 活跃的子网卡是不是第 2 张子网卡为例)

7.2.4 启动第 2 张子网卡
# ifconfig eth1 up

(补充:这里以启用子网卡 bond0-eth1 为例)

[步骤] Linux 网络的设置 (禁用 KVM 虚拟 IP)

步骤一:现象分析

安装 REHL 和 CentOS 系统时,系统可能会自动附带安装 libvirtd,并且启动其中的虚拟 IP

步骤二:显示是否有虚拟 IP

# ip -4 addr

(如果在输出的结果中包含的有以 virbr 开头的网卡信息,则代表虚拟网卡是启动的)

步骤三:删除 KVM 虚拟 IP

3.1 禁用虚拟网卡

# ifconfig virbr0 down

3.2 删除虚拟网桥

# brctl delbr virbro

步骤四:禁止 libvirtd 开机自启

# systemctl disable libvirtd

[命令] Linux 命令 systemctl (程序单元启动和管理)

内容一:受 systemctl 管理的单元列表

1.1 受 systemctl 管理的所有单元列表

1.1.1 列出所有 systemctl 可用单元以及是否开机自启
# systemctl list-unit-files

(补充:所有受 systemctl 管理单元的文件都是放在 /usr/lib/systemd/system 目录下的)

1.1.2 列出所有 systemctl 正在运行的单元
# systemctl list-units

(补充:所有受 systemctl 管理单元的文件都是放在 /usr/lib/systemd/system 目录下的)

1.1.3 列出所有 systemctl 运行失败的单元
# systemctl --failed

(补充:所有受 systemctl 管理单元的文件都是放在 /usr/lib/systemd/system 目录下的)

1.2 受 systemctl 管理的目标单元列表

1.2.1 列出所有受 systemctl 管理的目标单元
# systemctl list-units --all --type target


补充:
1) 所有受 systemctl 目标管理单元的文件都是放在 /usr/lib/systemd/system 目录下的
2) service 是一个个单独的服务
3) target 是包含很多个单独服务(service)的一组服务

1.2.2 列出所有正在运行的受 SYSTEMCTL 管理的目标单元
# systemctl list-units --typer=target --state=running


补充:
1) 所有受 systemctl 目标管理单元的文件都是放在 /usr/lib/systemd/system 目录下的
2) service 是一个个单独的服务
3) target 是包含很多个单独服务(service)的一组服务

1.2.3 列出所有开机自启的受 SYSTEMCTL 管理的目标单元
# systemctl list-units --all --type=target --state=active


补充:
1) 所有受 systemctl 目标管理单元的文件都是放在 /usr/lib/systemd/system 目录下的
2) service 是一个个单独的服务
3) target 是包含很多个单独服务(service)的一组服务

1.2.4 列出所有开机不自启的受 SYSTEMCTL 管理的目标单元
# systemctl list-units --all --type=target --state=inactive


补充:
1) 所有受 systemctl 目标管理单元的文件都是放在 /usr/lib/systemd/system 目录下的
2) service 是一个个单独的服务
3) target 是包含很多个单独服务(service)的一组服务

1.3 受 systemctl 管理的目标单元列表

1.3.1 列出所有受 systemctl 管理的服务单元
# systemctl list-units --all --type service


补充:
1) 所有受 systemctl 管理单元的文件都是放在 /usr/lib/systemd/system 目录下的
2) service 是一个个单独的服务
3) target 是包含很多个单独服务(service)的一组服务

1.3.2 列出所有正在运行的受 systemctl 管理的服务单元
# systemctl list-units --typer=service --state=running


补充:
1) 所有受 systemctl 管理单元的文件都是放在 /usr/lib/systemd/system 目录下的
2) service 是一个个单独的服务
3) target 是包含很多个单独服务(service)的一组服务

1.3.3 列出所有开机自启的受 systemctl 管理的服务单元
# systemctl list-units --all --type=service --state=active


补充:
1) 所有受 systemctl 管理单元的文件都是放在 /usr/lib/systemd/system 目录下的
2) service 是一个个单独的服务
3) target 是包含很多个单独服务(service)的一组服务

1.3.4 列出所有开机不自启的受 systemctl 管理的服务单元
# systemctl list-units --all --type=service --state=inactive


补充:
1) 所有受 systemctl 管理单元的文件都是放在 /usr/lib/systemd/system 目录下的
2) service 是一个个单独的服务
3) target 是包含很多个单独服务(service)的一组服务

内容二:单元的状态显示

2.1 单元的状态显示 (按标准格式显示)

# systemctl status <unit>


补充:
1) systemctl 的主要状态项有:
Loaded 单元是否已被内存加载
Active 单元是否已成功运行
Main PID 单元主进程的 PID 号
Status 单元的其他相关信息

2) Active 状态项的参数有:
loaded 单元文件已经处理
active(running) 单元正在运行
active(exited)已完成单元的一次性配置
active(waiting)正在等待启动
inactive 没有启动
enabled 将在系统启动时自动启动
disabled 不会在系统启动时自动启动
static 可以被其他单元启动

3) 单元既可以是服务单元也可以是目标单元

2.2 单元的状态显示 (显示所有内容)

# systemctl status -l <unit>


补充:
1) systemctl 的主要状态项有:
Loaded 单元是否已被内存加载
Active 单元是否已成功运行
Main PID 单元主进程的 PID 号
Status 单元的其他相关信息

2) Active 状态项的参数有:
loaded 单元文件已经处理
active(running) 单元正在运行
active(exited)已完成单元的一次性配置
active(waiting)正在等待启动
inactive 没有启动
enabled 将在系统启动时自动启动
disabled 不会在系统启动时自动启动
static 可以被其他单元启动

3) 单元既可以是服务单元也可以是目标单元

内容三:单元的启动管理

3.1 启动某 1 个单元

# systemctl start <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

3.2 停止某 1 个单元

# systemctl stop <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

3.3 重启某 1 个单元

# systemctl restart <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

3.3 显示某 1 个单元是否正在运行

3.3.1 使用 is-active 参数显示某个单元是否正在运行
# systemctl is-active <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

3.3.2 使用 list-unit-files 参数显示某个单元是否正在运行
3.3.2.1 使用 list-unit-files 参数显示某个单元是否正在运行的格式
3.3.2.1.1 显示某个 systemctl 的服务在 systemctl 中的文件名的格式
# ll /usr/lib/systemd/system/<unit>
3.3.2.1.2 显示某个 systemctl 文件名的状态的格式
# systemctl list-unit-files | grep ^<file name>
3.3.2.2 使用 list-unit-files 参数显示某个单元是否正在运行的案例
3.3.2.2.1 显示某个 systemctl 的服务在 systemctl 中的文件名的案例
# ll /usr/lib/systemd/system/ctrl-alt-del.target
lrwxrwxrwx 1 root root 13 May 19 06:56 /usr/lib/systemd/system/ctrl-alt-del.target -> reboot.target


补充:
1) 这里以显示 systemctl 的 ctrl-alt-del.target 服务在 systemctl 中的文件名为例
2) 从这里的输出结果可以看出 systemctl 的 ctrl-alt-del.target 服务在 systemctl 中的文件名为 reboot.target

3.3.2.2.2 显示某个 systemctl 文件名的状态的案例
# systemctl list-unit-files | grep ^reboot.target
reboot.target                                                             disabled        disabled

(补充:这里以显示 systemctl 的 reboot.target 文件名为例)

3.4 启动并将某 1 个单元设置为开机自启

# systemctl enable --now <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

3.5 停止并将某 1 个单元取消开机自启

# systemctl disable --now <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

内容四:单元的开机自启管理

4.1 让某一个单元开机自启

# systemctl enable <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

4.2 取消某一个单元的开机自启

# systemctl disable <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

4.3 显示某一个单元是否是开机自启

# systemctl is-enabled <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

内容五:单元的锁定管理

5.1 锁定某一个单元(禁止其启动)

# systemctl mask <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

5.2 取消某一个单元的锁定(让它可以启动)

# systemctl umask <unit>


补充:
1) service 是一个个单独的服务
2) target 是包含很多个单独服务(service)的一组服务
3) 单元既可以是服务单元(service)也可以是目标单元(target)

内容六:重新加载某 systemctl 的配置文件

# systemctl daemon-reload