大网数据广告活动
服务器运维问答

Allowing outgoing connections to a particular IP with ufw

· 52CE 我爱测

I have disabled outgoing connections as a whole with UFW seeing that I didn't want to take chances with my vBulletin forum exposing my server's IP address to attackers (I use CloudFlare). As I've done this, I've noticed I am unab…

问题原文

原帖链接 · Lorenc · CC BY-SA 3.0

I have disabled outgoing connections as a whole with UFW seeing that I didn't want to take chances with my vBulletin forum exposing my server's IP address to attackers (I use CloudFlare).

As I've done this, I've noticed I am unable to query one particular IP address which is called internally through code. Now my question is, how can I allow only this specific IP (let's say 255.255.255.255) address to make outgoing connections?

Thanks!

已采纳解答

原帖链接 · Lorenc · CC BY-SA 3.0

Very simple fix! Here's to anyone wondering how this can be done:

1) Open /etc/ufw/before.rules and insert this rule above COMMIT on the last line

-A ufw-before-output -m owner --uid-owner {user} -p {protocol} --dport {port} -d {ip} -j ACCEPT

Fill in the values for each as so:

{user} - user you want to allow this (www-data)

{protocol} - udp or tcp

{port} - on what port you want to allow it

{ip} - IP Address you want to allow.

An example:

# 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

2) Restart ufw service ufw restart

Should all correctly work after! Ensure you don't put two ports in one like like "-dport 6666,5555" - it usually errors!

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-12-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。