由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. ES6模块化及优点,简单案例让你秒懂

    模块化:模块是一个文件     好处:         1.减少命名冲突         2.避免引入时的层层依赖         3.可以提升执行效率     **第一种方法************ ...

  2. LLVM新建全局变量

    在LLVM中,有原生的AST Clone,却没有一个比较好的Stmt copy功能,基于Scout在LLVM上进行的修改,我们实现了自己的Stmt Clone功能. 要进行Stmt Clone,肯定需 ...

  3. linux下搭建redis内网端口映射工具-rinetd

    最近在工作中使用到了redis缓存,用来提某些业务场景的计算速度和某些情况下的操作频率限制,客户端工具使用了redisDesktopmanager来管理,在本机操作测试的时候,是没有问题的.但是将系统 ...

  4. java线程基础巩固---如何捕获线程运行期间的异常

    对于友盟统计我想搞程序的应该无人不晓,其中对于里面用得最多的功能就是对线上的崩溃进行修复,而这些异常都是运行期的,如: 其实也就是可以对线程中出现了这种运行期异常是提供有一种捕获机制对其进行统一处理, ...

  5. git远程分支创建,本地分支关联远程分支,第一次发布、更新分支

    git远程分支创建,本地分支关联远程分支,第一次发布.更新分支 github托管服务器地址为https://github.com git提交更新代码示意图: 本地与远程进行免密码配置(本地与远程关联) ...

  6. Python单例模式(Singleton)的N种实现

    很多初学者喜欢用全局变量,因为这比函数的参数传来传去更容易让人理解.确实在很多场景下用全局变量很方便.不过如果代码规模增大,并且有多个文件的时候,全局变量就会变得比较混乱.你可能不知道在哪个文件中定义 ...

  7. string::crbegin string::crend

    const_reverse_iterator crbegin() const noexcept;功能:crbegin是最后一个字符,crend第一个字符的前一个.迭代器向左移动是“+”,向右移动是“- ...

  8. Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化

    引言 在数据库存在外键的其情况下,使用select_related()和prefetch_related()很大程度上减少对数据库的请求次数以提高性能 1.实例准备 模型: from django.d ...

  9. Sklearn分类树在合成数集上的表现

    小伙伴们大家好~o( ̄▽ ̄)ブ,今天我们开始来看一下Sklearn分类树的表现,我的开发环境是Jupyter lab,所用的库和版本大家参考: Python 3.7.1(你的版本至少要3.4以上) S ...

  10. Java实现文件的上传下载(含源代码和jar包)

    1.需要使用的jar包 链接:https://pan.baidu.com/s/1IaxQRSwfzxDpe4w4JiaEKw 提取码:xwtz 2.如果想实现文件的下载,需要创建一张表,表的结构为 i ...