分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

linux网络相关 |防火墙 |netfilter5表5链

发布时间:2023-09-06 01:38责任编辑:彭小芳关键词:暂无标签
10.11 linux网络相关

ifconfig 如果没有ifconfig,需要安装包 ???

[root@centos-01 ~]# yum install net-tools

如果需要显示所有的网卡信息,包括down掉的或者没有IP地址的网络,使用-a命令

[root@centos-01 ~]# ifconfig -a

有时候会单独针对一个网卡做一些更改(如改网关,或者增加DNS),但是不想把所有的网卡都重启,只需要重启指定的网卡,为了避免down掉后无法启动,我们需要2个命令一起执行

[root@centos-01 ~]# ifdown ens33 && ifup ens33 成功断开设备 ‘ens33‘。 成功激活的连接(D-Bus 激活路 径:/org/freedesktop/NetworkManager/ActiveConnection/1)

设定虚拟网卡

1、切换至网卡配置文件

[root@centos-01 ~]# cd /etc/sysconfig/network-scripts

2、复制网卡

[root@centos-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0

3、编辑配置文件

[root@centos-01 network-scripts]# vi !$TYPE=EthernetBOOTPROTO=dhcpDEFROUTE=yesPEERDNS=yesPEERROUTES=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_PEERDNS=yesIPV6_PEERROUTES=yesIPV6_FAILURE_FATAL=noIPV6_ADDR_GEN_MODE=stable-privacyNAME=ens33:0UUID=584a55df-e887-4d4e-a11a-2ea0b5f6f5edDEVICE=ens33:0ONBOOT=YESIPADDR=192.168.187.150NETMASK=255.255.255.0

说明:更改了NAME、DEVICE、IPADDR,删除了DNS和网关,之前已经设定过。

4、重启系统网卡

[root@centos-01 network-scripts]# ifdown ens33 && ifup ens33成功断开设备 ‘ens33‘。成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/2)

5、查看网卡信息

[root@centos-01 network-scripts]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> ?mtu 1500inet 192.168.187.130 ?netmask 255.255.255.0 ?broadcast 192.168.187.255inet6 fe80::9b87:5d89:4bd9:8e53 ?prefixlen 64 ?scopeid 0x20<link>ether 00:0c:29:02:6a:c4 ?txqueuelen 1000 ?(Ethernet)RX packets 3124 ?bytes 244651 (238.9 KiB)RX errors 0 ?dropped 0 ?overruns 0 ?frame 0TX packets 689 ?bytes 85729 (83.7 KiB)TX errors 0 ?dropped 0 overruns 0 ?carrier 0 ?collisions 0ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> ?mtu 1500inet 192.168.187.150 ?netmask 255.255.255.0 ?broadcast 192.168.187.255ether 00:0c:29:02:6a:c4 ?txqueuelen 1000 ?(Ethernet)lo: flags=73<UP,LOOPBACK,RUNNING> ?mtu 65536inet 127.0.0.1 ?netmask 255.0.0.0inet6 ::1 ?prefixlen 128 ?scopeid 0x10<host>loop ?txqueuelen 1 ?(Local Loopback)RX packets 20 ?bytes 1700 (1.6 KiB)RX errors 0 ?dropped 0 ?overruns 0 ?frame 0TX packets 20 ?bytes 1700 (1.6 KiB)TX errors 0 ?dropped 0 overruns 0 ?carrier 0 ?collisions 0

6、检测虚拟网卡IP是否能ping通 在cmd中ping一下就可以

查看网卡是否连接

方法1:mii-tool [网卡名]

[root@centos-01 ~]# mii-tool ens33ens33: negotiated 1000baseT-FD flow-control, link ok查看:link ok说明网卡连接OK!

方法2:ethtool [网卡名]

[root@centos-01 ~]# ethtool ens33Settings for ens33:Supported ports: [ TP ]Supported link modes: ??10baseT/Half 10baseT/Full ????????????????????100baseT/Half 100baseT/Full ?…… ???????????????????????????Wake-on: dCurrent message level: 0x00000007 (7) ??????????drv probe linkLink detected: yes查看:Link detected: yes说明网卡连接OK!

更改主机名

 更改:[root@centos-01 ~]# hostnamectl set-hostname aminglinux-001查看主机名:[root@centos-01 ~]# hostnameaminglinux-001不重启的时候更改主机名[root@centos-01 ~]# bash[root@aminglinux-001 ~]# exit exit

DNS配置

DNS配置文件:/etc/resolv.conf

 [root@centos-01 ~]# cat /etc/resolv.conf# Generated by NetworkManagersearch localdomainnameserver 192.168.187.2

注: 更改网卡配置即可更改DNS配置文件,更改后需要重启网卡(ifdown/ifup)后生效,也可编辑‘/etc/resolv.conf’临时更改DNS配置,该办法在重启网卡后会被网卡配置文件中的DNS覆盖!

本地域名配置文件文件:/etc/hosts

[root@centos-01 ~]# cat /etc/hosts127.0.0.1 ??localhost localhost.localdomain localhost4 ???localhost4.localdomain4 ::1 ????????localhost localhost.localdomain localhost6 localhost6.localdomain6

说明: 使用vi命令可在该配置文件下可以自定义IP所对应的域名(一个IP对应多个域名或一个域名对应多个IP,用空格隔开,当一个域名对应多个IP时,以配置文件中靠后面的配置为准),但是该域名配置只在本机生效!

10.12 firewalld和netfilter

永久关闭防火墙

[root@aminglinux-001 ~]# vi etc/setlinux/config # This file controls the state of SELinux on the system.# ?SELINUX= can take one of these three values:# ??enforcing - SELinux security policy is enforced.# ??permissive - SELinux prints warnings instead of enforcing.# ???disabled - No SELinux policy is loaded.SELINUX=disabled# ?SELINUXTYPE= can take one of three two values:# ??targeted - Targeted processes are protected,# ??minimum - Modification of targeted policy. Only selected processes are protected.# ??mls - Multi Level Security protection. SELINUXTYPE=targeted

将SELINUX=enforcing改为disabled保存,重启系统即可!

查看防火墙状态

[root@aminglinux-001 ~]# getenforce

Enforcing

临时关闭防火墙

 [root@aminglinux-001 ~]# setenforce 0 ?[root@aminglinux-001 ~]# getenforce Permissive

Centos7中的防火墙叫firewalld,而7之前的防火墙叫netfilter.内部的工具iptables是一样的(可以打开或者关闭端口)

停用firewalld

关闭防火墙 [root@aminglinux-001 ~]# systemctl disable firewalldRemoved symlink /etc/systemd/system/dbus- ?org.fedoraproject.FirewallD1.service. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.停止服务 [root@aminglinux-001 ~]# systemctl stop firewalld

开启netfilter

开启前先安装iptables工具包:[root@aminglinux-001 ~]# yum install -y iptables-services开启iptables服务:[root@aminglinux-001 ~]# systemctl enable iptables Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. [root@aminglinux-001 ~]# systemctl start iptables

查看iptables规则:iptables -nvL

 [root@aminglinux-001 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target ????prot opt in ????out ????source ??????????????destination ??????????37 ?2432 ACCEPT ????all ?-- ?* ?????* ??????0.0.0.0/0 ???????????0.0.0.0/0 ?????????????state RELATED,ESTABLISHED 0 ????0 ACCEPT ????icmp -- ?* ?????* ??????0.0.0.0/0 ???????????0.0.0.0/0 ???????????0 ????0 ACCEPT ????all ?-- ?lo ????* ??????0.0.0.0/0 ???????????0.0.0.0/0 ???????????0 ????0 ACCEPT ????tcp ?-- ?* ?????* ??????0.0.0.0/0 ???????????0.0.0.0/0 ???????????state NEW tcp dpt:22 ?25 ?1450 REJECT ????all ?-- ?* ?????* ??????0.0.0.0/0 ???????????0.0.0.0/0 ???????????reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target ????prot opt in ????out ????source ??????????????destination ??????????0 ????0 REJECT ????all ?-- ?* ?????* ??????0.0.0.0/0 ???????????0.0.0.0/0 ???????????reject-with icmp-host-prohibited ?Chain OUTPUT (policy ACCEPT 21 packets, 2204 bytes) pkts bytes target ????prot opt in ????out ????source ??????????????destination

10.13 netfilter5表5链介绍

表名

filter:包过滤,用于防火墙规则。 nat:地址转换,用于网关路由器。 mangle:用于给数据包打标记,然后根据标记去操作那些表。(不常用) 还有两个不常用的表:raw和security,在此不多讲述。 规则链名

filter的三种链:

INPUT链:作用于输入本机的数据包。 OUTPUT链:作用于本机输出的数据包。 FORWARD链:作用于与本机无关的包。 nat的三种链:

PREROUTING链:作用是在包刚刚到达防火墙时改变它的目的地址,如果需要的话。 OUTPUT链:改变本地产生的包的目的地址。 POSTROUTING链:在包离开防火墙之前改变其源地址。 nat表应用(了解内容)

环境: 假设有三台机器(A,B,C),A机器上有一块网卡连接公网IP1;B机器上有两块网卡b1、b2,b1、b2分别连接公网和私网IP2,、IP3;C机器上有一块网卡连接私网IP4。很明显此时A&B,B&C之间都分别能互通,但是A和C之间是不能直接连通的,只能借助B做媒介才能连通。那么如何设置才能使得A和C之间互通呢?

方法:

先打开路由转发功能: ??[root@aminglinux-001 ~]# echo "1" > /proc/sys/net/ipv4_forward然后对nat表做IP转发配置: ?????[root@aminglinux-001 ~]# iptables -t nat -A POSTROUTING -s IP2(此处IP在同一个网段即可) -o ensA(A机器的网卡名称) -j MASQUERADE

说明: -o 选项后面跟设备名称,表示出口网卡,MASQUERADE是伪装、冒充的意思。

10.14 iptables语法

iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分。可以直接配置,也可以通过许多前端和图形界面配置。

语法: iptables [options] [参数]
Options:
-n:不针对IP反解析主机名
-v:显示更详细的信息
-t:指定表(iptables命令默认作用于filter表)
-L:显示信息
-F:清空所有规则
-A/D:=add/delete,添加/删除一条规则
-I:插入一条规则
-p:指定协议,可以是tcp,udp或icmp --sport:跟-p一起使用,指定源端口
--dport:跟-p一起使用,指定目标端口
-s:指定源IP(可以是一个IP段)
-d:指定目的IP(可以是一个IP段)
-j:后面跟动作(ACCEPT表示允许包;DROP表示丢掉包;REJECT表示拒绝包)
-i:指定网卡
-Z:把包以及流量计数器清零
-P:=pre,预设策略

  • iptables命令选项输入顺序:

    iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作

  • 查看规则

    [root@aminglinux-001 ~]# iptables -nvL
    iptables规则配置文件:/etc/sysconfig/iptables

  • 清空规则

    [root@aminglinux-001 ~]# iptables -F
    注: 该命令不会清除配置文件内的规则!当更改规则后需要执行命令‘service iptables save’将其保存到配置文件。

  • 增加一条规则(-A/I)

    [root@aminglinux-001 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
    说明: 增加一条规则,当IP192.168.188.1、协议为tcp、端口为‘1234’的向IP192.168.188.128、端口为80的机器发送包时执行操作:drop(丢掉包)。 注: 该命令也可以把-A换成-I,两者的区别类似于排队和插队,两种方法插入的规则优先级不同。

  • 删除一条规则(-D)

  • 方法1: 知道规则的内容

    [root@aminglinux-001 ~]# iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
    注: 要删除一条规则时,必须和插入的规则一致,也就是说,两条iptables命令除了-A/I和-D不一样外,其他地方都一样。

  • 方法2: 忘记规则内容
    首先所以用以下命令查看规则序号:

    [root@aminglinux-001 ~]# iptables -nvL --line-number
    然后再执行删除命令:

    [root@aminglinux-001 ~]# iptables -D INPUT [序号]
    更改预设策略(-P)

执行命令:

 ?#iptables -P OUTPUT DROP

注意: 尽量不要随意更改该配置,尤其是在进行远程登录时,一旦执行该命令后将会断开连接。这个策略设定后只能用命令:‘iptables -P OUTPUT ACCEPT’来恢复成原始状态,不能使用-F参数。

linux网络相关 |防火墙 |netfilter5表5链

原文地址:http://blog.51cto.com/13242922/2064436

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved