Linux对外提供服务 网络操作 端口操作 1.开启服务监听端口 2.设置防火墙,放行访问端口的包 iptables&netfilter 四表五链和通堵策略
主题:
Linux服务器上软件提供服务 1.网络操作 2.端口操作
1.网络操作 本机必须能够ping通目标主机(本地虚拟机或者远程主机)
2.端口操作 1.开启服务监听端口 2.设置防火墙,放行访问该端口的数据包
iptables&netfilter 四表五链和堵通策略 应用举例:
Linux上安装Tomcat和MySQL,客户端要能够访问服务器上的Tomcat服务和MySQL服务 操作:
1.网络操作 本机必须能够ping通目标主机(本地虚拟机或者远程主机)
2.端口操作 1.开启服务监听端口 2.设置防火墙,放行访问该端口的数据包
关键iptables和netfilter:
iptables&netfilter的四表五链和堵通策略 演示的Linux操作系统版本CentOS release 6.7:
[root@heima01 ~]# uname -a
Linux heima01 2.6.32-573.el6.i686 #1 SMP Thu Jul 23 12:37:35 UTC 2015 i686 i686 i386 GNU/Linux [root@heima01 ~]# lsb_release -a
LSB Version: :base-4.0-ia32:base-4.0-noarch:core-4.0-ia32:core-4.0-noarch:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: CentOS
Description: CentOS release 6.7 (Final)
Release: 6.7
Codename: Final CentOS6.7端口操作最佳实践:
查看iptables命令的帮助:
iptables --help 不详细
man iptables 一般详细 手册页
info iptables 最详细 1.查看当前包过滤规则
示例:# service iptables status
2.根据需求添加或删除相应的规则。配置文件或者指令
示例:# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
3.iptables指令修改规则,立即生效,但不会持久化,所以根据需要手动进行持久化操作
示例:# service iptables save
4.直接修改/etc/sysconfig/iptables文件,规则不会立即生效,通过重启iptables,使其生效。
示例:# service iptables restart 1.网络操作:
1.1 使用ifconfig查看虚拟机网络地址
示例:# ifconfig
[root@heima01 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:71:C4:BB
inet addr:192.168.211.130 Bcast:192.168.211.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe71:c4bb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:420 errors:0 dropped:0 overruns:0 frame:0
TX packets:229 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:35784 (34.9 KiB) TX bytes:28445 (27.7 KiB)
Interrupt:19 Base address:0x2000 lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:480 (480.0 b) TX bytes:480 (480.0 b) 1.2 在本地ping虚拟机网络地址,必须保证ping通
示例:ping 192.168.211.130 本机与虚拟机
网络不通:
C:\Users\jie>ping 192.168.211.130
正在 Ping 192.168.211.130 具有 32 字节的数据:
来自 192.168.211.1 的回复: 无法访问目标主机。 网络联通:
C:\Users\jie>ping 192.168.211.130
正在 Ping 192.168.211.130 具有 32 字节的数据:
来自 192.168.211.130 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.211.130 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.211.130 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.211.130 的回复: 字节=32 时间<1ms TTL=64 192.168.211.130 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms 2.端口操作:
2.1.启动服务,监听某个端口
查看某个端口是否已经被监听:(即相应的服务已经启动)
示例:# netstat -ntlp
2.2设置防火墙,放行访问这个端口的包 查看某个端口是否已经被监听:(即相应的服务已经启动)
[root@heima01 ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1588/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1835/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1684/cupsd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2797/sshd
tcp 0 0 0.0.0.0:53754 0.0.0.0:* LISTEN 1645/rpc.statd
tcp 0 0 :::43942 :::* LISTEN 1645/rpc.statd
tcp 0 0 :::3306 :::* LISTEN 1976/mysqld
tcp 0 0 :::111 :::* LISTEN 1588/rpcbind
tcp 0 0 :::22 :::* LISTEN 1835/sshd
tcp 0 0 ::1:631 :::* LISTEN 1684/cupsd
tcp 0 0 ::1:6010 :::* LISTEN 2797/sshd CentOS6.7中设置防火墙,放行访问端口的数据包:
查看防火墙的包过滤规则:(正在生效)
示例:# service iptables status
查看包过滤规则文件:(不一定正在生效)
示例:# cat /etc/sysconfig/iptables
查看链中的规则:(正在生效)
示例:# iptables -L 注意:
修改包过滤规则,必须重启iptables服务,使新的规则生效。
链中规则有顺序,请把规则放首位。 参数解释:
通堵策略: ACCEPT接收 DROP丢弃 REJECT拒绝
-I 插入规则
-D 删除规则 方式1:修改/etc/sysconfig/iptables文件
步骤:
1.为/etc/sysconfig/iptables文件添加一条规则
示例:-A INPUT -p tcp -m state --state NEW -m tcp --dport 端口号 -j ACCEPT
注意:规则有顺序,所以把规则添加到上面,而不是下面。 2.重启iptables服务,新加规则才会生效
示例:service iptables restart
方式2:
步骤:
1.使用iptables动态添加规则
添加接收访问某端口的包的规则
示例:# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
添加丢弃访问某端口的包的规则
示例:# iptables -I INPUT -p tcp --dport=3306 -j DROP
注意:立即生效,只对本次有效,规则不会添加到iptables文件,服务重启后失效 2.iptables指令删除规则:
1.查看规则,获取规则编号:
示例:# service iptables status
2.删除规则:
示例:# iptables -D INPUT 规则编号
3.再次查看规则,删除成功:
示例:# service iptables status 3.将本次的规则保存到iptables文件中
示例:# service iptables save 最佳实践:
查看iptables命令的帮助:
iptables --help 不详细
man iptables 一般详细 手册页
info iptables 最详细 1.查看当前包过滤规则
示例:# service iptables status
2.根据需求添加或删除相应的规则。配置文件或者指令
示例:# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
3.iptables指令修改,立即生效,可能需要进行持久化操作
示例:# service iptables save
4.直接修改/etc/sysconfig/iptables文件,规则不会立即生效,通过重启iptables,使其生效。
示例:# service iptables restart 关键iptables:
iptables中的四表五链和堵通策略 直接修改/etc/sysconfig/iptables文件,添加开放端口的规则:
[root@heima01 ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.7 on Mon May 27 22:42:05 2019
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4:560]
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
# Completed on Mon May 27 22:42:05 2019 重启iptables服务,让规则生效:
[root@heima01 ~]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ] 查看链中的规则:(链中规则有顺序,请把规则放首位)
[root@heima01 ~]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT)
num target prot opt source destination 查看链中的规则:
[root@heima01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:mysql
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT)
target prot opt source destination 查看/etc/sysconfig/iptables文件:
[root@heima01 ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.7 on Mon May 27 22:42:05 2019
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4:560]
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited iptables命令动态添加规则:
示例:# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT [root@heima01 ~]# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
[root@heima01 ~]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
3 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
6 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT)
num target prot opt source destination 将当前生效的规则保存到iptables文件:
[root@heima01 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] [root@heima01 ~]# iptables -I INPUT -p tcp --dport=3306 -j DROP
[root@heima01 ~]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
2 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
4 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
5 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
6 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
7 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT)
num target prot opt source destination [root@heima01 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@heima01 ~]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
[root@heima01 ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.7 on Tue May 28 18:23:29 2019
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [32:4416]
-A INPUT -p tcp -m tcp --dport 3306 -j DROP
-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Tue May 28 18:23:29 2019 iptables指令删除规则:
1.查看规则,获取规则编号:
示例:# service iptables status
2.删除规则:
示例:# iptables -D INPUT 规则编号
3.再次查看规则,删除成功:
示例:# service iptables status [root@heima01 ~]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
2 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
4 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
5 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
6 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
7 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT)
num target prot opt source destination [root@heima01 ~]# iptables -D INPUT 1 [root@heima01 ~]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
3 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
6 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
Linux对外提供服务 网络操作 端口操作 1.开启服务监听端口 2.设置防火墙,放行访问端口的包 iptables&netfilter 四表五链和通堵策略的更多相关文章
- linux防火墙放行了端口,但是仍然访问不到
我们的防火墙默认规则如下: 如果防火墙放行了端口,但是仍然访问不到的话,可能是因为添加规则的时候,用的是iptables -A 选项,这样,增加的规则会排列在 第6条 规则后面,虽然service i ...
- Win10如何设置防火墙开放特定端口 windows10防火墙设置对特定端口开放的方法
Win10防火墙虽然能够很好地保护我们的系统,但同时也会因限制了某些端口,而给我们的操作带了一些不便.对于既想使用某些端口,又不愿关闭防火墙的用户而言,在Win10系统中设置防火墙开放特定端口就非常必 ...
- SUSE Enterprise Server 12 SP3 64 设置防火墙开放8080端口
SUSE Enterprise Server 12 SP3 64 设置防火墙开放8080端口 第一种方式: 1.sudo chmod a+w /etc/sysconfig/SuSEfirewall2 ...
- SUSE Enterprise Server 12 SP3 64 设置防火墙开放8080端口,出现Unsafe permissions for file /etc/sysconfig/SuSEfirewall2 to be sourced
SUSE Enterprise Server 12 SP3 64 设置防火墙开放8080端口时出现 Unsafe permissions for file /etc/sysconfig/SuSEf ...
- windows 开启 nginx 监听80 端口 以及 禁用 http 服务后,无法重启 HTTP 服务,提示 系统错误 123,文件目录、卷标出错
1. 正常情况直接运行 start nginx.exe 不能开启成功,因为 80 端口被占用.提示: bind() to 0.0.0.0:80 failed (10013: An attempt w ...
- SVN开启端口监听,并设置开机启动
svnserve -d -r /home/svn/repo --listen-port=3690,svn仓库地址,及监听端口 vi svn_startup.sh,位置在/root下面编辑一个启动脚本, ...
- 160524、Linux下如何启动、关闭Oracle以及打开关闭监听
1. linux下启动oraclesu - oraclesqlplus /nologconn /as sysdbastartupexitlsnrctl startexit2. linux下关闭orac ...
- Nginx服务监听端口修改启动bug
监听的端口从80 修改到其他端口出现启动不起来问题. 解决方案如下: yum install policycoreutils-python sudo cat /var/log/audit/audit. ...
- linux 开启oracle监听
secureCRT连接到数据库所在的linux机器,切换到oracle用户模式下 [root@nstlbeta ~]# su - oracle 步骤阅读 2 然后用sqlplus登录到数据库,关闭数据 ...
随机推荐
- PS图片透明处理方法
// http://www.3lian.com/edu/2014/06-18/149890.html 今天想把图片背景做成透明效果,以前叫美工的妹纸帮忙做过,自己没留意学. 今天需要做这个东西,特别记 ...
- 简单通俗解释内外网IP与端口映射
IP:分为外网IP和内网IP 也就是我们说的外网IP属于实体IP 实体IP,它是独一无二的,在网络的世界里,每一部计算机的都有他的位置,一个 IP 就好似一个门牌!例如,你要去百度的网站的话,就要去『 ...
- 五年java工作应具备的技能
具有一到五年开发经验 需要学习内容很多 JVM/分布式/高并发/性能优化/Spring MVC/Spring Boot/Spring Cloud/MyBatis/Netty源码分析等等等 01.透彻理 ...
- 淘宝双十一页面(Flexible)demo
下面的代码是看了大漠 使用Flexible实现手淘H5页面的终端适配 做的一个demo. <!DOCTYPE html> <html lang="en" ng-a ...
- linux cpu占用100%排查
某服务器上部署了若干tomcat实例,即若干垂直切分的Java站点服务,以及若干Java微服务,突然收到运维的CPU异常告警. 问:如何定位是哪个服务进程导致CPU过载,哪个线程导致CPU过载,哪段代 ...
- CodeForces Div1: 995 D. Game(数学期望)
Allen and Bessie are playing a simple number game. They both know a function f:{0,1}n→Rf:{0,1}n→R, i ...
- LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
原题链接在这里:https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ 题目: C ...
- mysql数据库表分区详解(数量过大的数据库表通过分区提高查询速度)
这篇文章主要介绍了MySQL的表分区,例如什么是表分区.为什么要对表进行分区.表分区的4种类型详解等,需要的朋友可以参考下 一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysq ...
- [原创]如何解决IE10下CkEditor报 --- SCRIPT5007: 无法获取未定义或 null 引用的属性“toLowerCase”
如何解决IE10下CkEditor报 --- SCRIPT5007: 无法获取未定义或 null 引用的属性“toLowerCase” 错误 如果你的IE是IE10,且不是运行在IE的兼容模式你也许会 ...
- 湖南程序设计竞赛赛题总结 XTU 1237 Magic Triangle(计算几何)
这个月月初我们一行三人去湖南参加了ccpc湖南程序设计比赛,虽然路途遥远,六月的湘潭天气燥热,不过在一起的努力之下,拿到了一块铜牌,也算没空手而归啦.不过通过比赛,还是发现我们的差距,希望这几个月自己 ...