iptables基本用法和linux网络相关
ifconfig 命令
ifconfig在centos6中是默认安装的,centos7版本默认是没有这个命令的,需要使用yum来进行安装: yum install net-tools
使用ifdown和ifup单独启动个别网卡
[root@localhost /]# ifdown enp0s3 && ifup enp0s3
成功断开设备 'enp0s3'。
成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
设置虚拟网卡,拷贝原网卡文件,修改网卡配置文件中的网卡名称再次重启后生成虚拟网卡,生成的虚拟网卡是一个有独立ip的设备 ,可以使用这个ip和其他主机进行通信
[root@localhost network-scripts]# cp ifcfg-enp0s3 ifcfg-enp0s3\:0
[root@localhost network-scripts]# vim ifcfg-enp0s3\:0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=enp0s3:0
UUID=bf8adc16-a9c3-494a-b47e-f572fad38639
DEVICE=enp0s3:0
[root@localhost network-scripts]# ifdown enp0s3 && ifup enp0s3
成功断开设备 'enp0s3'。
成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/3)
[root@localhost network-scripts]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.1.223 netmask 255.255.255.0 broadcast 192.168.1.255ether 08:00:27:a7:f9:c2 txqueuelen 1000 (Ethernet)RX packets 20698 bytes 21236657 (20.2 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 9509 bytes 924400 (902.7 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0enp0s3:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.1.233 netmask 255.255.255.0 broadcast 192.168.1.255ether 08:00:27:a7:f9:c2 txqueuelen 1000 (Ethernet)
临时修改主机名hostname 主机名 ; 永久修改linux主机名vim /etc/hostname ,重启或source /etc/profile 重新加载系统环境变量文件
DNS配置文件: /etc/resolv.conf这里的DNS文件优先级会低于网卡配置文件中指定的DNS,如果发生重启会被网卡中的DNS配置所覆盖
linux中简单解析文件:/etc/hosts ,修改的解析只在本机生效, 一行内一个ip可以配置多个对应域名
[root@localhost network-scripts]# vim /etc/hosts
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.233 www.bilibili.com www.abc.com www.123.com
[root@localhost network-scripts]# ping www.bilibili.com
PING www.bilibili.com (192.168.1.233) 56(84) bytes of data.
64 bytes from www.bilibili.com (192.168.1.233): icmp_seq=1 ttl=64 time=0.219 ms
firewalld和netfilter工具
关闭selinux工具,selinux默认开启状态,安装某些服务selinux会影响服务正常启动,临时关闭selinux:
[root@localhost network-scripts]# setenforce 0
[root@localhost network-scripts]# getenforce
Disabled
永久关闭selinux
[root@localhost ~]# vim /etc/selinux/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
停用firewalld改为使用netfilter工具,需要安装iptables-services的yum包文件
[root@localhost network-scripts]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@localhost network-scripts]# systemctl stop firewalld
[root@localhost network-scripts]# yum install -y iptables-services
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* epel: mirrors.ustc.edu.cn* extras: mirrors.aliyun.com* updates: mirrors.sohu.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 iptables-services.x86_64.0.1.4.21-24.1.el7_5 将被 安装
--> 正在处理依赖关系 iptables = 1.4.21-24.1.el7_5,它被软件包 iptables-services-1.4.21-24.1.el7_5.x86_64 需要
--> 正在检查事务
---> 软件包 iptables.x86_64.0.1.4.21-17.el7 将被 升级
---> 软件包 iptables.x86_64.0.1.4.21-24.1.el7_5 将被 更新
--> 解决依赖关系完成
已安装:iptables-services.x86_64 0:1.4.21-24.1.el7_5 作为依赖被升级:iptables.x86_64 0:1.4.21-24.1.el7_5 完毕!
[root@localhost network-scripts]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@localhost network-scripts]# systemctl start iptables
[root@localhost network-scripts]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination 51 3496 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED0 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:228 699 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibitedChain 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-prohibitedChain OUTPUT (policy ACCEPT 29 packets, 2876 bytes)pkts bytes target prot opt in out source destination
iptables只是netfilter使用的一个工具,只是在使用的时候我们通常称为iptables
netfilter5表5链介绍
filter表:默认的一张表,表中包含INPUT、OUTPUT和FORWARD链
INPUT表示进入本机流量的过滤链,检测端口或来源ip
FORWARD转发链,检查是否为本机流量,非本机流量将会被转发
OUTPUT本机出流量,可禁止到某ip的数据包
nat表:流量数据包转发
PREROUTING数据包进入时更改来源ip
POSTROUTING数据包转发出去是修改目的ip
实现内网服务器和公网服务器之间互相通信,需要在一台出口设备上做端口映射,也就是使用PREROUTING和POSTROUTING链
mangle表:给数据包做标记,如数据类型
raw表:可以实现不追踪某些数据包
security表:加强访问控制的(MAC)网络规则
iptables语法
iptables -nvL查看默认filter表的规则
[root@localhost network-scripts]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination 984 83612 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED0 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:223211 287K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibitedChain 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-prohibitedChain OUTPUT (policy ACCEPT 772 packets, 175K bytes)pkts bytes target prot opt in out source destination
iptables -F 清空规则
iptables -t nat -nvL 指定表查看规则
[root@localhost network-scripts]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination
iptables -Z 清零计数器
记录通过规则链的数据流累计信息,可以通过记录数判断数据量大小,可以根据数据量发送接收大小来做限制
iptables -A INPUT -s 192.168.1.112 -p tcp --sport 1234 -d 192.168.1.223 --dport 80 -j DROP/REJECT 禁止1.112通过1234端口向223发送数据
[root@localhost network-scripts]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination 1142 95952 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED0 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:224450 397K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited0 0 DROP tcp -- * * 192.168.1.112 192.168.1.223 tcp spt:1234 dpt:80Chain 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-prohibitedChain OUTPUT (policy ACCEPT 17 packets, 1676 bytes)pkts bytes target prot opt in out source destination
-A 在现有规则后新增一条新规则
-I 在现有规则最前面插入一条规则
-I和-A的区别:匹配规则时会优先从最前面规则进行匹配,如果匹配到规则后就不会再往下匹配了
删除iptables规则
iptables -D INPUT 规则序号
[root@localhost network-scripts]# iptables -D INPUT 6
[root@localhost network-scripts]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination 1200 100K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED0 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:224606 409K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibitedChain 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-prohibitedChain OUTPUT (policy ACCEPT 4 packets, 512 bytes)pkts bytes target prot opt in out source destination
修改链默认的匹配规则,注意如果写入拒绝语句之前,请放行需要使用的端口,否则会造成通信中断(如ssh服务的22端口)
iptables -P INPUT DROP
[root@localhost ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
[root@localhost ~]# iptables -A INPUT -p tcp --sport 22 -j ACCEPT
[root@localhost ~]# iptables -P INPUT DROP
[root@localhost ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
转载于:https://blog.51cto.com/8844414/2141117
iptables基本用法和linux网络相关相关推荐
- 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 ne
七周三次课 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法 10.11 Linux网络 ...
- Linux centos7 Linux网络相关、firewalld和netfilter、netfilter5表5链介绍、iptables语法
一. Linux网络相关 yum install net-tools ifconfig查看网卡ip ifup ens33开启网卡 ifdown ens33关闭网卡 设定虚拟网卡ens33:0 mii- ...
- 七周三次课(11月29日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法...
2019独角兽企业重金招聘Python工程师标准>>> 10.11 Linux网络相关 ifconfig 查看网卡ip (yum install net-tools) 安装 -a ...
- Linux网络相关、firewalld、netfilter及其5表5链、iptables语法
2019独角兽企业重金招聘Python工程师标准>>> 1.Linux 网络相关 ifconfig 查看网卡IP,见下图, net-tools 包之前安装过了,这边还可以执行这个命令 ...
- 七周三次课(1月24日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法...
七周三次课(1月24日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法 ====== ...
- Linux网络相关知识
2019独角兽企业重金招聘Python工程师标准>>> 10月25日任务 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfil ...
- linux网络相关配置
linux网络相关配置文件 linux系统一般来说分为两大类: 第一类:Debian系列:Debian.Ubuntu等: 第一类:RedHat系列:Redhat.Centos.Fedora等: lin ...
- linux netfilter路由表,linux网络相关,firewalld和netfilter,netfilter5表5链介绍,itptables语法...
Linux网络相关: 如果没有 ifconfig 命令 那么需要安装一个包(yum install net-tools) 如果 ifconfig 命令查看不到网卡的话,那么可以使用 ifconfig ...
- Linux日常运维管理技巧(一)监控系统状态、监控网卡流量、监控IO性能、查看系统进程、查看网络状态、Linux下抓包tcpdump、Linux网络相关、DNS配置
目录 监控系统状态 监控网卡流量 监控IO性能 查看系统进程 查看网络状态 Linux下抓包 Linux网络相关 监控系统状态 w/uptime查看系统负载 [root@zyshanlinux-01 ...
- Linux网络相关命令
Linux网络相关命令 1 ping 2 netstat 3 tranceroute 4 ARP 5 tcpdump 1 ping 作用:检测当前主机与目标主机之间的连通性(不是100%准确,有的服务 ...
最新文章
- mysql根据字段长度查询_SQL语句如何查询某一字符串字段长度等于某个值的所有记录...
- 【原创】HP 安装 depot (以mysql为例)
- c语言中菲薄拉次数列,在VB子函数如何写出菲薄拉起数列
- java应用:csv文件的读写
- 数据结构与算法---查找算法(Search Algorithm)
- 苹果将削减iPhone SE及AirPods产量 iPhone 13也要求减产
- Django Signal 代码布局
- 基于DVWA的CSRF
- centos下CMAKE源码安装MYSQL5.5.23
- 【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
- 网络创业者之家:普通人想要获得“第一桶金”,要记住这3点
- QMC5883L与msp430FG4618--IIC通信
- 别人的【计算机视觉算法岗面经】“吐血”整理:2019秋招面经
- SQL日期时间转为字符串
- 技术专有名词缩写整理
- Omni协议PHP开发包
- 142.如何个性化推荐系统设计-2
- 现代企业管理-管理概论
- Google Play ASO 系列 - 用户体验一直为王
- 【TeamViewer丨远程控制软件】上海道宁助您远程访问和即时远程支持,提高远程工作团队的生产力
热门文章
- 云主机搭建linux web,linux云主机web服务的搭建和配置
- java打印等腰三角形_为什么大家都说Java中只有值传递?
- PHP接口报错:Malformed UTF-8 characters, possibly incorrectly encoded
- mysql查询表字段的个数
- PHP正则匹配Html内容,并通过浏览器弹框下载Excel
- PHP将图片转换成base64编码,hash函数
- 《Yii2 By Example》第2章:创建一个简单的新闻阅读器
- oracle获取当前年上一年时间,Oracle获取一年中的所有日期和一个月中的所有日期...
- 六石管理学:半弹性工作时间
- JNI新旧两种方式不冲突,可以共存