如何阻止,拒绝或重定向IP地址或网站域

这个问题在几天前就出现了,它是:如何在不使用路由器或配置浏览器的情况下阻止,拒绝或重定向IP地址或域名到另一个IP或Web域。 基本上只使用Ubuntu来做到这一点。

例如,用户使用计算机,他/她无法访问Facebook或Twitter(可能是永久拒绝访问或仅在下午2点到晚上8点之间)。 如果用户试图进入Facebook或Twitter,它将被重定向到另一个地方或刚刚被取消。

该程序在GUI中更可取,但如果没有终端就行。

我已经检查了例如ufw和gufw,但它只适用于程序和端口。 关于域名的案例不存在。 这样可以更轻松地选择或取消选择域。

如果它只有几个ip /域名,iptables很有用。

使用iptables,您可以根据用户,组和/或时间进行限制,但这样做需要使用OUTPUT表。 因此,要允许root,以及组“web”,请使用

# this allows root for things such as apt-get sudo iptables -A OUTPUT -m owner --uid-owner root -j ACCEPT # this allows users of the group web # create a group, web, and add users to it to allow access sudo iptables -A OUTPUT -m owner --gid-owner web -j ACCEPT # These two rules allow access to port 80 and 443 over the lunch hour sudo iptables -A OUTPUT -o eth0 -p tcp -m multiport --dports 80,443 -m time --timestart 12:00 --timestop 13:00 -j ACCEPT sudo iptables -A OUTPUT -o eth0 -p tcp -m multiport --dports 80,443 -j DROP 

但随着您的需求变得越来越复杂,使用代理会很有帮助。 例如,您可以将privoxy(和其他)用于adblock。 Squid增加了过滤和更复杂的规则(acl或访问控制列表),但可能会过度杀死家庭用户。

然后使用iptables使代理透明

 # This allows root sudo iptables -A OUTPUT -m owner --uid-owner root -j ACCEPT # This allows privoxy, which serves as adblock sudo iptables -A OUTPUT -p tcp --dport 80 -m owner --uid-owner privoxy -j ACCEPT # this blocks direct access to ports 80 to all other users sudo iptables -A OUTPUT -p tcp --dport 80 -j DROP # This allows squid to access privoxy (I think squid runs as "proxy") #sudo iptables -A OUTPUT -o lo -p tcp --dport 8118 -m owner --uid-owner proxy -j ACCEPT # this rule blocks other users from direct access to privoxy sudo iptables -A OUTPUT -o lo -p tcp --dport 8118 -j DROP # Redirect all outgoing traffic on port 80 to squid listening on port 3128 sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -m owner ! --uid-owner privoxy -j REDIRECT --to-port 3128 

现在你安装并配置privoxy和squid

Ubuntu服务器指南鱿鱼

Ubuntu wiki privoxy

这种方法的问题是你需要安装squidguard,配置squid等,这将是一个很长的post,更适合更大的局域网。

我找到了一个更好的解决方案,moblock,它附带了一个每日更新的黑名单和一个图形配置工具。

要安装它运行

 sudo add-apt-repository ppa:jre-phoenix/ppa sudo apt-get update sudo apt-get install moblock blockcontrol mobloquer 

然后运行Mobloquer

有关其他信息,请参阅Moblock主页(sourceforge)

拍摄1

在此处输入图像描述

您可以使用IPTABLES阻止IP和域名

传出的例子:

 iptables -A OUTPUT -p tcp -m string --string "xxx.com" --algo kmp -j DROP 

然后使用cron作业,您可以在所需的特定时间阻止所需的所有域,然后取消阻止它们

由于您可能需要阻止或限制任何特定IP地址或域访问您的服务器的几种情况,可能会出现这种情况。 这是一个简单的过程,按照这些说明,您可以阻止Ubuntu中的网站域。 我们知道Ubuntu是一个基于开源命令的操作系统,这就是为什么你必须通过MASQUERADE规则的输入链并触发下面的命令:

 iptables -I INPUT –s  -j DROP 

例如,您要阻止160.10.11.01而不是类型:

 iptables -I Input –s <160.10.11.01> -j DROP 

如果要阻止从160 … *开始的整个IP地址

 iptables -I Input –s <160.0.0.0> -j DROP 

如果仍有任何问题,那么最好联系任何网站托管服务商或专家域名注册商 。

我发现firestarter是一个非常好的GUI界面来管理iptables条目。