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登录到数据库,关闭数据 ...
随机推荐
- android的GPS代码分析JNI如何HAL之间如何设置回调函数【转】
本文转载自:http://blog.csdn.net/kmesg/article/details/6531577 本文只关注JNI和HAL的接口部分 在jni的android_location_Gps ...
- Apache CGI 配置
在/etc/apache2/apache2.conf末尾添加 ServerName lacalhost:80 然后启动CGI模块: sudo a2enmod cgi 3.重启Apache: syste ...
- 自用的弹出窗口jquery插件
现有网上的弹出窗口插件很多, 但发现在项目应用中总会有些功能不能适用, 最后只好自己写一个:插件主要参考了ymPrompt弹窗代码, ymPrompt是JS的弹窗,本插件相当于是ymPrompt的jq ...
- 时尚创意VI矢量设计模板
时尚创意VI矢量设计模板 创意VI VI设计 企业VI 时尚背景 信封设计 封面设计 杯子 桌旗 帽子 EPS矢量素材下载 http://www.huiyi8.com/vi/
- LoadRunner监控图表与配置(二)监控运行状况和交易状况
1.在左侧Available Graphs视图中展开Runtime Graphs节点,选择其中一种类型添加至控制器运行标签的界面. 2.在图中显示的空白区域点击右键,在弹出的快捷菜单中选择config ...
- base64编码方式
一.编码的两大方式: 在python3.x中,字符串编码分为unicode和bytes两大类编码方式. 直接书写s='中国人',这种方式定义的编码方式为unicode,是通用的方式. 另一种是byte ...
- less语言特性
作为CSS的一种扩展,LESSCSS不仅向下兼容CSS的语法,而且连新增的特性也是使用CSS的语法.这样的设计使得学习LESS很轻松,而且你可以在任何时候回退到CSS. 变量 很容易理解: @nice ...
- MySQL业务-发放的优惠券 用户使用情况_20161028
运营部门给用户发放优惠券,如果想监控优惠券的使用效果 优惠券使用率是个反映效果的很好指标 下面sql就是针对某天对特定用户发放的优惠券在发放日期以后每天的使用情况 SELECT e.城市,e.用户ID ...
- Node初学者入门,一本全面的NodeJS教程
作者: Manuel Kiessling 翻译: goddyzhao & GrayZhang & MondayChen 关于 本书致力于教会你如何用Node.js来开发应用,过程中会 ...
- 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)
前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...