[内容] CentOS&RHEL 静态路由表的添加

内容目录:

内容一:添加临时路由器表
1.1 案例一:添加某一个 IP 地址的路由表
1.2 案例二:删除某一个 IP 地址的路由表
1.3 案例三:添加某一个网段的路由表
1.4 案例四:删除某一个网段的路由表

内容二:添加永久路由表
2.1 添加永久路由表
2.1.1 案例一
2.1.2 案例二
2.2 重启要添加路由表的网卡
2.2.1 重启要添加路由表的网卡的格式
2.2.2 重启要添加路由表的网卡的案例

具体的内容:

内容一:临时路由器表到管理
1.1 案例一:添加某一个 IP 地址的路由表

# route add -host 192.168.2.1 netmask 255.255.255.255 gw 192.168.1.1 dev eth0

(补充:这里以指向 192.168.2.1 IP 地址的流量需要通过 eth0 网卡和 192.168.1.1 网关为例)

1.2 案例二:删除某一个 IP 地址的路由表

# route del -host 192.168.2.1

(补充:这里删除 192.168.2.1 IP 地址的路由表为例)

1.3 案例三:添加某一个网段的路由表

# route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1

(补充:这里以指向 192.168.2.0/24 网段的流量需要通过 192.168.1.1 网关为例)

1.4 案例四:删除某一个网段的路由表

# route del -net 192.168.2.0/24

(补充:这里删除 192.168.2.0/24 网段的路由表为例)

内容二:添加永久路由表
2.1 添加永久路由表
2.1.1 案例一

# nmcli con mod eth0 ipv4.routes "192.168.2.0/24 192.168.3.1"

(补充:这里以给 eth0 网卡添加 192.168.2.0/24 通过 192.168.3.1 的网关为例)

2.1.2 案例二

# vim /etc/sysconfig/network-scripts/route-enp1s0

创建以下内容:

192.168.100.3/32 via 192.168.101.101 dev enp1s0

(补充:这里以添加 192.168.100.3 通向 192.168.101.101 网关的永久路由为例)

2.2 重启要添加路由表的网卡
2.2.1 重启要添加路由表的网卡的格式

# nmcli connection down <the name of the network card to add the routing table to>
# nmcli connection up <the name of the network card to add the routing table to>

或者:

# reboot

2.2.2 重启要添加路由表的网卡的案例

# nmcli connection down enp1s0
# nmcli connection up enp1s0

或者:

# reboot

(补充:这里以重启名为 enp1s0 网卡为例)

[实验] CentOS&RHEL 自动挂载服务的搭建 (通过 Autofs 和 NFS 实现) (CentOS 8 版)

纪念:站主于 2020 年 6 月完成了此开源实验,并将过程中的所有命令经过整理和注释以后,形成以下教程

步骤目录:

步骤一:规划拓扑
1.1 服务器列表
1.2 服务器列表简介

步骤二:系统环境要求

步骤三:所有服务器安装 NFS 服务
3.1 所有服务器安装 NFS 服务
3.2 设置所有服务器开机自启 NFS 服务
3.3 所有服务器启动 NFS 服务

步骤四:配置 NFS 服务
4.1 创建用于 NFS 服务的目录
4.1.1 创建被 NFS 服务共享的目录
4.1.2 创建用于自动挂载 NFS 服务分享目录的目录
4.2 配置服务端的 NFS 服务配置文件
4.2.1 在服务端上添加可被 NFS 服务挂载的选项
4.2.2 让刚刚修改的 NFS 服务配置文件生效
4.3 部署客户端的 Autofs 自动挂载服务
4.3.1 安装 Autofs 服务
4.3.2 设置客户端开机自启 Autofs 服务
4.3.3 在客户端上设置 Autofs 自动挂载服务
4.3.3.1 在客户端上设置 Autofs 自动挂载的主配置文件
4.3.3.2 在客户端上设置 Autofs 的从配置文件
4.3.4 让刚刚修改的 Autofs 自动挂载服务配置文件生效

步骤五:查看 Autofs 自动挂载服务是否设置成功
5.1 查看客户端当前的目录挂载情况
5.2 进入到 Autofs 自动挂载的目录
5.3 再次查看客户端当前的目录挂载情况

具体的操作步骤:

步骤一:规划拓扑
1.1 服务器列表

服务端 192.168.101.10
客户端 192.168.101.11

1.2 服务器列表简介

1) 服务器提供 NFS 服务将自己的目录分享
2) 客户端挂载和使用 NFS 服务将服务端分享的目录挂载在自己的目录上

步骤二:系统环境要求

1) 所有服务器的系统都需要是 CentOS 8 版本
2) 所有服务器都要关闭防火墙
3) 所有服务器系统都要配置好可用的软件源
4) 需要按照拓扑图给对应的服务器配置好 IP 地址和主机名
5) 所有服务器都要可以相互 ping 通自己和对方的 IP 地址和主机名

步骤三:所有服务器安装 NFS 服务
3.1 所有服务器安装 NFS 服务

(分别在服务端和客户端上执行以下步骤)

# yum -y install rpcbind nfs-utils

3.2 设置所有服务器开机自启 NFS 服务

(分别在服务端和客户端上执行以下步骤)

# systemctl enable nfs-server

3.3 所有服务器启动 NFS 服务

(分别在服务端和客户端上执行以下步骤)

# systemctl start nfs-server

步骤四:配置 NFS 服务
4.1 创建用于 NFS 服务的目录
4.1.1 创建被 NFS 服务共享的目录

(只在服务端上执行以下步骤)

# mkdir /nfsshare

4.1.2 创建用于自动挂载 NFS 服务分享目录的目录

(只在客户端上执行以下步骤)

# mkdir /autofs

4.2 配置服务端的 NFS 服务配置文件
4.2.1 在服务端上添加可被 NFS 服务挂载的选项

(只在服务端上执行以下步骤)

# vim /etc/exports

添加以下内容:

......
/nfsshare 192.168.101.0/24(rw,sync,no_root_squash,no_subtree_check)

(补充:这里的 192.168.101.0.24 是客户端的 IP 地址所在的网段)

4.2.2 让刚刚修改的 NFS 服务配置文件生效

(只在服务端上执行以下步骤)

# exportfs -a

4.3 部署客户端的 Autofs 自动挂载服务
4.3.1 安装 Autofs 服务

(只在客户端上执行以下步骤)

# yum -y install autofs

4.3.2 设置客户端开机自启 Autofs 服务

(只在客户端上执行以下步骤)

# systemctl enable autofs

4.3.3 在客户端上设置 Autofs 自动挂载服务
4.3.3.1 在客户端上设置 Autofs 自动挂载的主配置文件

(只在客户端上执行以下步骤)

# vim /etc/auto.master

将以下内容:

......
#
/misc   /etc/auto.misc
#
......

修改为:

......
/misc   /etc/auto.misc
/autofs /etc/auto.autofs
......

(补充:在这里指定了 /etc/auto.autofs 为 Autofs 的从配置文件,并且将 autofs 的主目录设置为 /autofs)

4.3.3.2 在客户端上设置 Autofs 的从配置文件

(只在客户端上执行以下步骤)

# cp /etc/auto.misc /etc/auto.autofs
# vim /etc/auto.autofs

将以下内容:

......
cd              -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
......

修改为:

......
cd              -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
directory01             -fstype=nfs,rw 192.168.101.10:/nfsshare
......

(补充:在这里指定了 Autofs 的次级目录为 directory01,即:/autofs/directory01)

4.3.4 让刚刚修改的 Autofs 自动挂载服务配置文件生效

(只在客户端上执行以下步骤)

# systemctl restart autofs

步骤五:查看 Autofs 自动挂载服务是否设置成功
5.1 查看客户端当前的目录挂载情况

(只在客户端上执行以下步骤)

# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        957M     0  957M   0% /dev
tmpfs           971M     0  971M   0% /dev/shm
tmpfs           971M   17M  954M   2% /run
tmpfs           971M     0  971M   0% /sys/fs/cgroup
/dev/vda1        10G  1.6G  8.5G  16% /
tmpfs           195M     0  195M   0% /run/user/0

5.2 进入到 Autofs 自动挂载的目录

(只在客户端上执行以下步骤)

# cd /autofs/directory01

5.3 再次查看客户端当前的目录挂载情况

(只在客户端上执行以下步骤)

# df -h
Filesystem                Size  Used Avail Use% Mounted on
devtmpfs                  957M     0  957M   0% /dev
tmpfs                     971M     0  971M   0% /dev/shm
tmpfs                     971M   17M  955M   2% /run
tmpfs                     971M     0  971M   0% /sys/fs/cgroup
/dev/vda1                  10G  1.6G  8.5G  16% /
tmpfs                     195M     0  195M   0% /run/user/0
192.168.101.10:/nfsshare   10G  1.6G  8.5G  16% /autofs/directory01

(补充:在进入到 Autofs 自动挂载的目录后,自动挂载就在系统中自动出现了)

[工具] Shell 取消所有已开放的端口策略 (firewalld 版)

介绍:

作者:朱明宇
名称:取消所有已开放的端口策略
作用:取消所有已开放的端口策略

使用方法:
1. 给此脚本添加执行权限
2. 执行此脚本

脚本:

#!/bin/bash

systemctl start firewalld
systemctl enable firewalld

for i in `firewall-cmd --list-all | grep ports | egrep [0-9] | awk -F':' '{print $2}'`
do
        pports=`echo $i | awk -F'/' '{print $1}'`
        ptus=`echo $i | awk -F'/' '{print $2}'`

        firewall-cmd --remove-port=$pports/$ptus --permanent

done

firewall-cmd --add-service=ssh --permanent
firewall-cmd --reload

[工具] Shell 取消所有已设置的复杂端口策略 (firewalld 版)

介绍:

作者:朱明宇
名称:取消所有已设置的复杂端口策略
作用:取消所有已设置的复杂端口策略

使用方法:
1. 给此脚本添加执行权限
2. 执行此脚本

脚本:

#!/bin/bash

systemctl start firewalld
systemctl enable firewalld

max=`firewall-cmd --list-all | grep "rule family" | wc -l`

for i in `seq 1 $max`
do
        echo $i
        lines=`firewall-cmd --list-all | grep "rule family" | sed -n $[i]p`
        ipvs=`echo $lines | awk -F'"' '{print $2}'`
        ips=`echo $lines | awk -F'"' '{print $4}'`
        ports=`echo $lines | awk -F'"' '{print $6}'`
        tus=`echo $lines | awk -F'"' '{print $8}'`
        acts=`echo $lines | awk -F'"' '{print $9}'`

        echo $ipvs $ips $ports $tus $acts
        firewall-cmd --remove-rich-rule="rule family="$ipvs" source address="$ips" port protocol="$tus" port="$ports" $acts" --permanent
done

firewall-cmd --add-service=ssh --permanent
firewall-cmd --reload

[工具] Shell 开放所有正处于监听状态的端口策略 (firewalld 版)

介绍:

作者:朱明宇
名称:开放所有正处于监听状态的端口策略
作用:开放所有正处于监听状态的端口策略

使用方法:
1. 给此脚本添加执行权限
2. 执行此脚本

脚本:

#!/bin/bash

systemctl start firewalld
systemctl enable firewalld

for k in `ss -ntulap |grep 0.0.0.0 | grep LISTEN | awk '{print $5}' | awk -F':' '{print $2}'`
do 
        firewall-cmd --add-port=$k/tcp --permanent
done

firewall-cmd --reload