由Internet和其他网络协议识别端口号,使计算机能够与其他人进行交互。每个Linux服务器都有一个端口号(参见/ etc / services文件)

Block Incoming Port

The syntax is as follows to block incoming port using IPtables:

/sbin/iptables -A INPUT -p tcp --destination-port {PORT-NUMBER-HERE} -j DROP

### interface section use eth1 ###

/sbin/iptables -A INPUT -i eth1 -p tcp --destination-port {PORT-NUMBER-HERE} -j DROP

### only drop port for given IP or Subnet ##

/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP-ADDRESS-HERE} -j DROP /sbin/iptables -A INPUT -i eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP/SUBNET-HERE} -j DROP

To block port 80 (HTTP server), enter (or add to your iptables shell script):
# /sbin/iptables -A INPUT -p tcp --destination-port 80 -j DROP
# /sbin/service iptables save

Block Incomming Port 80 except for IP Address 1.2.3.4

#/sbin/iptables -A INPUT -p tcp -i eth1 -s ! 1.2.3.4 --dport 80 -j DROP

Block Outgoing Port

The syntax is as follows:

/sbin/iptables -A OUTPUT -p tcp --dport {PORT-NUMBER-HERE} -j DROP

### interface section use eth1 ###

/sbin/iptables -A OUTPUT -o eth1 -p tcp --dport {PORT-NUMBER-HERE} -j DROP

### only drop port for given IP or Subnet ##

/sbin/iptables -A OUTPUT -o eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP-ADDRESS-HERE} -j DROP /sbin/iptables -A OUTPUT -o eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP/SUBNET-HERE} -j DROP

To block outgoing port # 25, enter:
# /sbin/iptables -A OUTPUT -p tcp --dport 25 -j DROP
# /sbin/service iptables save

You can block port # 1234 for IP address 192.168.1.2 only:
# /sbin/iptables -A OUTPUT -p tcp -d 192.168.1.2 --dport 1234 -j DROP
# /sbin/service iptables save

How Do I Log Dropped Port Details?

Use the following syntax:

# Logging #

### If you would like to log dropped packets to syslog, first log it ###

/sbin/iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "PORT 80 DROP: " --log-level 7

### now drop it ###

/sbin/iptables -A INPUT -p tcp --destination-port 80 -j DROP

How Do I Block Cracker (IP: 123.1.2.3) Access To UDP Port # 161?

/sbin/iptables -A INPUT -s 123.1.2.3 -i eth1 -p udp -m state --state NEW -m udp --dport 161 -j DROP

# drop students 192.168.1.0/24 subnet to port 80

/sbin/iptables -A INPUT -s 192.168.1.0/24 -i eth1 -p tcp -m state --state NEW -m tcp --dport 80 -j DROP

How do I view blocked ports rules?

Use the iptables command:
# /sbin/iptables -L -n -v
# /sbin/iptables -L -n -v | grep port
# /sbin/iptables -L -n -v | grep -i DROP
# /sbin/iptables -L OUTPUT -n -v
# /sbin/iptables -L INPUT -n -v


check whether port is open or block

iptables -nL | grep <port number>

netstat :

netstat -plnt | grep ':25'

ss :

 ss -lntu | grep ':25'

nmap :

nmap -sT -O localhost | grep 25

lsof:

lsof -i:25

Refer to: https://www.cyberciti.biz/faq/iptables-block-port/

Refer to: https://unix.stackexchange.com/questions/306195/how-to-check-whether-port-25-is-open-or-blocked

Linux: Block Port With IPtables的更多相关文章

  1. linux平台下防火墙iptables原理

    iptables简单介绍 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它能够取代昂贵的商业 ...

  2. linux平台下防火墙iptables原理(转)

    原文地址:http://www.cnblogs.com/ggjucheng/archive/2012/08/19/2646466.html iptables简介 netfilter/iptables( ...

  3. Linux中的堡垒--iptables

    iptables的构成(四表五链)     表         filter:过滤数据包         nat :转换数据包的源或目标地址         mangle:用来mangle包,改变包的 ...

  4. linux下如何修改iptables开启80端口

    linux下如何修改iptables开启80端口   最近在做本地服务器的环境,发现网站localhost能正常访问,用ip访问就访问不了,经常使用CentOS的朋友,可能会遇到和我一样的问题.开启了 ...

  5. Linux基础命令---防火墙iptables

    iptables iptables指令用来设置Linux内核的ip过滤规则以及管理nat功能.iptables用于在Linux内核中设置.维护和检查IPv4数据包过滤规则表.可以定义几个不同的表.每个 ...

  6. Linux时间设置与iptables命令

    日期与时间设置 timedatectl:显示目前时区与时间等信息 [root@localhost zhang]# timedatectl Local time: Thu 2018-01-18 10:1 ...

  7. linux中firewall与iptables防火墙服务

    火墙firewall-cmd --state 查看火墙的状态firewall-cmd --get-active-zones 目前所处的域firewall-cmd --get-default-zone ...

  8. Linux中级之netfilter/iptables应用及补充

    一.iptables介绍 Netfilter/Iptables(以下简称Iptables)是unix/linux自带的一款优秀且开放源代码的完全自由的基于包过滤的防火墙工具,它的功能十分强大,使用非常 ...

  9. (一)洞悉linux下的Netfilter&iptables:什么是Netfilter?

    转自:http://blog.chinaunix.net/uid-23069658-id-3160506.html 本人研究linux的防火墙系统也有一段时间了,由于近来涉及到的工作比较纷杂,久而久之 ...

随机推荐

  1. 重拾MVC——第二天:Vue学习与即时密码格式验证

    今天是复习MVC的第二天,准备自己写一个后台管理,然后慢慢写大,做全. 个人感觉做 Web 的,前端知识是必备的,所有今天学习了一下 Vue,很多人用这个,我以前没有用过,今天把它补起来. 比较了各个 ...

  2. 解决设置了display:none的元素,会先展示再隐藏

    问题:元素明明设置了display:none,但是在刷新页面的时候却会先显示了出来,然后才会隐藏,实现display:none 原因:由于元素渲染的时候,样式还没有应用上去,导致的 解决办法:使用内联 ...

  3. django form 和modelform样式设置

      目录 1.form通过attr设置属性 2.输入框设置表单状态 3.modelform的使用 4.结合modelform 使用for循环生成输入框 5.基于init构造方法设置样式 6.基本增删改 ...

  4. swagger 的使用

    最近在用 .Net Core 做项目 了解到swagger 是一个不错的工具 简单介绍一下 在使用asp.net core 进行api开发完成后,书写api说明文档对于程序员来说想必是件很痛苦的事情吧 ...

  5. Python与C/C++相互调用(转)

    原文链接 作者 一.问题 Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结. 二.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库 ...

  6. Notepad++快捷键及使用技巧

    常用快捷键: CTRL+Q 注释/取消注释 用Notepad++写代码,要是有一些重复的代码想copy一下,还真不容易,又得动用鼠标,巨烦人....有木有简单的方法呢,确实还是有的不过也不算太好用.主 ...

  7. 下划线文字,鼠标hover小样式

    CSS样式 //不只是a标签,其他有下划线的字体也可以 a:hover{ color: #ff3100; //这里的颜色是指字体颜色不是波浪下划线效果的svg图颜色     text-decorati ...

  8. python_推导式

    列表推导式 目的:方便的生成一个列表 格式: v1 = [i for i in 可迭代对象 ] v2 = [i for i in 可迭代对象 if条件]#条件为True菜进行append v1 = [ ...

  9. python pip 出现locations that require TLS/SSL异常处理方法

    python pip 出现locations that require TLS/SSL异常处理方法 转载 郑才华 发布于2018-03-24 21:41:16 阅读数 51844 收藏 展开 最近在r ...

  10. adreno的tbdr

    看完apple的(&&powervr) 再来撸高通的.. FlexRender 能自己选三种模式 Direct Binning 这个只是分块 HWVizBinning 这个估计就是tb ...