Opening Ports in a Linux Firewall

This guide applies only to users of Linux based operating systems.

The procedure for opening ports in the Linux firewall "iptables" is relatively simple.

First, open a command-line terminal.  In most systems, you can usually find this in your 'Applications' menu under the 'System Tools' section.

Once you have a terminal open, you have to obtain root access to change firewall settings.  Do this by typing:
 

su -


You will be prompted for your root password.

In the following examples, substitute the port # you want to open for the 12345 in the command.

If you want to open an incoming TCP port, type the following:
 

iptables -I INPUT -p tcp --dport 12345 --syn -j ACCEPT


If you want to open a UDP port (perhaps for DHT in Tixati), type the following:
 

iptables -I INPUT -p udp --dport 12345 -j ACCEPT



After you are done opening ports in your firewall, you can save your changes so they will be applied when you restart your computer by typing the following command:
 

service iptables save



That's it!  If you have any questions or comments about this guide, please feel free to drop us a note on the Support and Feedback page.

If you also have a broadband NAT router for your internet connection and want to configure it to forward incoming TCP connections or UDP packets, read the Forwarding Ports In a Broadband Router guide.
 

原文链接 :  https://www.tixati.com/optimize/open-ports-linux.html

### 在 Linux 中开放指定端口的配置方法 在 Linux 系统中,开放指定端口通常涉及以下几个关键步骤:确保服务正在监听该端口、配置防火墙规则以允许流量通过该端口。以下是具体实现方法: #### 1. 确保服务正在监听目标端口 服务需要明确绑定到目标端口并处于监听状态。例如,如果希望开放 `8080` 端口供 HTTP 流量使用,可以通过以下方式检查服务是否已绑定到该端口: ```bash ss -tuln | grep ':8080' ``` 如果服务未绑定到目标端口,则需要配置服务以监听该端口。例如,对于 Nginx 或 Apache,可以在配置文件中设置监听端口[^6]。 #### 2. 配置防火墙规则以允许流量通过目标端口 Linux 系统通常使用 `iptables` 或 `firewalld` 来管理防火墙规则。以下是两种工具的具体配置方法: ##### 使用 `firewalld` 如果系统使用 `firewalld`,可以通过以下命令开放指定端口(如 `8080`): ```bash sudo firewall-cmd --add-port=8080/tcp --permanent sudo firewall-cmd --reload ``` 第一行命令将端口添加到防火墙规则中,并设置为永久生效;第二行命令重新加载防火墙配置以应用更改[^7]。 ##### 使用 `iptables` 如果系统使用 `iptables`,可以通过以下命令开放指定端口(如 `8080`): ```bash sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT sudo service iptables save ``` 第一行命令将规则添加到 `iptables` 的输入链中,允许 TCP 流量通过 `8080` 端口;第二行命令保存规则以确保其在系统重启后仍然有效[^8]。 #### 3. 检查 SELinux 是否限制了端口访问 如果系统启用了 SELinux,可能需要调整策略以允许特定服务使用目标端口。例如,要允许 HTTP 服务使用非标准端口(如 `8080`),可以运行以下命令: ```bash sudo semanage port -a -t http_port_t -p tcp 8080 ``` 如果没有安装 `semanage` 工具,可以通过以下命令安装: ```bash sudo yum install policycoreutils-python-utils ``` 此命令将为目标端口分配适当的 SELinux 类型,从而解除限制[^9]。 #### 4. 验证端口是否成功开放 完成上述配置后,可以使用以下方法验证端口是否成功开放: - 使用 `ss` 或 `netstat` 命令检查本地监听端口。 - 使用 `nmap` 或 `telnet` 测试远程连接是否成功。 --- ### 示例代码 以下是一个使用 `firewalld` 开放多个端口的脚本示例: ```bash #!/bin/bash PORTS=(8080 9090) for PORT in "${PORTS[@]}"; do echo "Opening port $PORT..." sudo firewall-cmd --add-port="$PORT"/tcp --permanent done sudo firewall-cmd --reload ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值