允许使用 ufw 向外连接到特定 IP

Lor*_*enc 4 linux ubuntu ufw cloudflare

我已经禁用了与 UFW 的整体传出连接,因为我不想在我的 vBulletin 论坛上冒险将我的服务器的 IP 地址暴露给攻击者(我使用 CloudFlare)。

完成此操作后,我注意到我无法查询通过代码在内部调用的特定 IP 地址。现在我的问题是,我怎样才能只允许这个特定的 IP(比如 255.255.255.255)地址进行传出连接?

谢谢!

小智 7

我使用以下命令成功允许传出流量到特定端口上的特定 ip:

ufw allow out from any to <the.target.ip> port <port_number>
Run Code Online (Sandbox Code Playgroud)


Lor*_*enc 1

非常简单的修复!对于任何想知道如何做到这一点的人来说:

1)打开并在最后一行/etc/ufw/before.rules插入此规则COMMIT

-A ufw-before-output -m owner --uid-owner {user} -p {protocol} --dport {port} -d {ip} -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

如下填写每个值:

{user}- 您想要允许此操作的用户(www-data)

{protocol}- UDP 或 TCP

{port}- 您想要允许的端口

{ip}- 您想要允许的 IP 地址。

一个例子:

# server
-A ufw-before-output -m owner --uid-owner www-data -p udp --dport 6666 -d 255.255.255.255 -j ACCEPT # gameserver
-A ufw-before-output -m owner --uid-owner www-data -p tcp --dport 3306 -d 255.255.255.255 -j ACCEPT # mysql

# don't delete the 'COMMIT' line or these rules won't be processed
COMMIT
Run Code Online (Sandbox Code Playgroud)

2)重启ufwservice ufw restart

之后应该一切正常工作!确保您不要将两个端口放在一个端口中,例如“-dport 6666,5555” - 它通常会出错!