IPtables --- TCPMSS target

本文介绍TCPMSS(最大段尺寸)的概念及其在网络通信中的作用。TCPMSS用于控制TCP SYN包的最大段尺寸,以解决ISP或服务器阻塞大型ICMP包导致的数据交换问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TCPMSS target

The TCPMSS target can be used to alter the MSS (Maximum Segment Size) value of TCP SYN packets that the firewall sees. The MSS value is used to control the maximum size of packets for specific connections. Under normal circumstances, this means the size of the MTU (Maximum Transfer Unit) value, minus 40 bytes. This is used to overcome some ISP's and servers that block ICMP fragmentation needed packets, which can result in really weird problems which can mainly be described such that everything works perfectly from your firewall/router, but your local hosts behind the firewall can't exchange large packets. This could mean such things as mail servers being able to send small mails, but not large ones, web browsers that connect but then hang with no data received, and ssh connecting properly, but scp hangs after the initial handshake. In other words, everything that uses any large packets will be unable to work.

TCPMSS 目标用来改变路由器防火墙能够识别的TCP syn 包中的MSS (最大分段长度)标示位值。MSS 值用来控制特定连接中包的长度。一般情况下,MSS 值等于MTU (最大传输单位)值减去40. TCPMSS 目标用来解决一些奇怪的问题:ISP (网络服务提供商)或服务器会阻塞大的ICMP 包,这样会导致路由器工作正常,而在路由器后面的本地主机工作异常,不能传输大的数据包。这可能意味着邮件服务器能够发送小邮件,不能发送大邮件;web 浏览器能够连接服务端却不能接收数据;ssh 能够连接上,但在初始化握手后的scp 却挂住了。换句话说,需要大数据包的操作都将不能正常操作。

The TCPMSS target is able to solve these problems, by changing the size of the packets going out through a connection. Please note that we only need to set the MSS on the SYN packet since the hosts take care of the MSS after that. The target takes two arguments.

TCPMSS 目标通过改变从一个连接出去的包的大小来剞劂这些问题。注意仅仅需要设置SYN 包中的MSS标记位,因为主机将会在收到SYN 包后检测MSS 。

Table 11-13. TCPMSS target options

Option

--set-mss

Example

iptables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o eth0 -j TCPMSS --set-mss 1460

Explanation

The --set-mss argument explicitly sets a specific MSS value of all outgoing packets. In the example above, we set the MSS of all SYN packets going out over the eth0 interface to 1460 bytes -- normal MTU for ethernet is 1500 bytes, minus 40 bytes is 1460 bytes. MSS only has to be set properly in the SYN packet, and then the peer hosts take care of the MSS automatically.

Option

--clamp-mss-to-pmtu

Example

iptables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o eth0 -j TCPMSS --clamp-mss-to-pmtu

Explanation

The --clamp-mss-to-pmtu automatically sets the MSS to the proper value, hence you don't need to explicitly set it. It is automatically set to PMTU (Path Maximum Transfer Unit) minus 40 bytes, which should be a reasonable value for most applications.

 

 

MSS ( Maxitum Segment Size )最大传输大小的缩写,是 TCP 协议里面的一个概念。 MSS 就是 TCP数据包每次能够传输的最大数据分段。为了达到最佳的传输效能 TCP 协议在建 立连接的时候通常要协商双方的 MSS 值,这个值 TCP 协议在实现的时候往往用 MTU 值代替(需要减去 IP 数据包包头的大小 20Bytes 和 TCP 数据段的 包头 20Bytes )所以往往 MSS 为 1460 。通讯双方会根据双方提供的MSS 值得最小值确定为这次连接的最大 MSS 值。

### 配置iptables规则以实现仅允许特定IP通过SSH访问 为了确保服务器的安全性,可以通过 `iptables` 设置规则来限制只有特定的 IP 地址能够通过 SSH(端口 22)访问服务器,并拒绝其他所有连接。以下是详细的配置过程: #### 默认策略设置 首先,需要定义默认的链策略,使输入和转发流量被丢弃,而输出流量则被接受。 ```bash iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT ``` 这一步骤确保任何未明确允许的流量都会被自动拒绝[^1]。 #### 允许已建立的连接继续通信 为了让已经存在的合法会话不受影响,需添加一条规则允许已建立的连接继续通信。 ```bash iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT ``` 这条规则对于维持现有连接非常重要,尤其是在动态环境中[^3]。 #### 允许来自特定IP地址的SSH连接 假设要授权的特定IP地址为 `192.168.1.100`,那么可以使用如下命令将其加入白名单: ```bash iptables -A INPUT -p tcp -s 192.168.1.100 --dport 22 -j ACCEPT ``` 此操作专门针对该IP开放了SSH端口访问权限[^2]。 #### 拒绝其余所有尝试访问SSH的行为 最后,应确保除了上述特别指明的情况外,再无其他途径可触及到SSH服务: ```bash iptables -A INPUT -p tcp --dport 22 -j DROP ``` 以上步骤共同构建了一个安全框架,在其中仅有预设好的单一源能成功发起SSH请求[^4]。 #### 将当前规则保存以便持久化应用 由于之前所作修改均为即时生效而非永久存储,因此还需要采取措施让这些改动在系统重启之后仍然有效。具体做法依据不同发行版有所不同;这里给出适用于Red Hat系的操作方式: ```bash service iptables save ``` 或者手动将现有的表项导出至相应位置: ```bash iptables-save > /etc/sysconfig/iptables ``` 如此一来,即使经历重新引导周期,先前定制化的防护机制依旧得以保留[^3]。 ```python # 示例Python脚本用于自动化部分流程(非必要) import os def setup_iptables(ip_address): commands = [ 'iptables -P INPUT DROP', 'iptables -P FORWARD DROP', 'iptables -P OUTPUT ACCEPT', f'iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT', f'iptables -A INPUT -p tcp -s {ip_address} --dport 22 -j ACCEPT', 'iptables -A INPUT -p tcp --dport 22 -j DROP' ] for cmd in commands: os.system(cmd) setup_iptables('192.168.1.100') os.system('service iptables save') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值