【linux】iptables和ufw禁用IP和端口

这篇具有很好参考价值的文章主要介绍了【linux】iptables和ufw禁用IP和端口。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

引言

有时候服务器,为了安全需要,会限制某些ip和端口对服务器的访问。那具体该如何配置呢?本文带你解决。
ubuntu 禁用端口,计算机技术,linux,ubuntu,tcp/ip,linux

禁用IP和端口

1、方法1:iptables

iptables v1.8.4

Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

Commands:
Either long or short options are allowed.
  --append  -A chain            Append to chain
  --check   -C chain            Check for the existence of a rule
  --delete  -D chain            Delete matching rule from chain
  --delete  -D chain rulenum
                                Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
                                Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
                                Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
                                List the rules in a chain or all chains
  --list-rules -S [chain [rulenum]]
                                Print the rules in a chain or all chains
  --flush   -F [chain]          Delete all rules in  chain or all chains
  --zero    -Z [chain [rulenum]]
                                Zero counters in chain or all chains
  --new     -N chain            Create a new user-defined chain
  --delete-chain
            -X [chain]          Delete a user-defined chain
  --policy  -P chain target
                                Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
                                Change chain name, (moving any references)
Options:
    --ipv4      -4              Nothing (line is ignored by ip6tables-restore)
    --ipv6      -6              Error (line is ignored by iptables-restore)
[!] --protocol  -p proto        protocol: by number or name, eg. `tcp'
[!] --source    -s address[/mask][...]
                                source specification
[!] --destination -d address[/mask][...]
                                destination specification
[!] --in-interface -i input name[+]
                                network interface name ([+] for wildcard)
 --jump -j target
                                target for rule (may load target extension)
  --goto      -g chain
                              jump to chain with no return
  --match       -m match
                                extended match (may load extension)
  --numeric     -n              numeric output of addresses and ports
[!] --out-interface -o output name[+]
                                network interface name ([+] for wildcard)
  --table       -t table        table to manipulate (default: `filter')
  --verbose     -v              verbose mode
  --wait        -w [seconds]    maximum wait to acquire xtables lock before give up
  --wait-interval -W [usecs]    wait time to try to acquire xtables lock
                                default is 1 second
  --line-numbers                print line numbers when listing
  --exact       -x              expand numbers (display exact values)
[!] --fragment  -f              match second or further fragments only
  --modprobe=<command>          try to insert modules using this command
  --set-counters PKTS BYTES     set the counter during insert/append
[!] --version   -V              print package version.

  • 🈲端口
iptables -A INPUT -p tcp --dport 80 -j DROP
iptables -A INPUT -p tcp --dport 5053 -j DROP
  • 🈲IP
iptables -I INPUT -s 10.115.10.129 -j DROP
  • 保存配置
    sudo service iptables save
    实测这条命令行不通,报错:iptables: unrecognized service
    下面这条可以
sudo iptables-save

2、方法2:ufw

UFW (Uncomplicated Firewall)是Ubuntu 自带的防火墙配置工具 。UFW 是一个用来管理 iptables 防火墙规则的用户友好的前端工具。它的主要目的就是为了使得管理 iptables 更简单。

Usage: ufw COMMAND

Commands:
 enable                          enables the firewall
 disable                         disables the firewall
 default ARG                     set default policy
 logging LEVEL                   set logging to LEVEL
 allow ARGS                      add allow rule
 deny ARGS                       add deny rule
 reject ARGS                     add reject rule
 limit ARGS                      add limit rule
 delete RULE|NUM                 delete RULE
 insert NUM RULE                 insert RULE at NUM
 route RULE                      add route RULE
 route delete RULE|NUM           delete route RULE
 route insert NUM RULE           insert route RULE at NUM
 reload                          reload firewall
 reset                           reset firewall
 status                          show firewall status
 status numbered                 show firewall status as numbered list of RULES
 status verbose                  show verbose firewall status
 show ARG                        show firewall report
 version                         display version information

Application profile commands:
 app list                        list application profiles
 app info PROFILE                show information on PROFILE
 app update PROFILE              update PROFILE
 app default ARG                 set default application policy

Ubuntu安装完毕,默认没有启动ufw。所以如果直接运行上述语句,那么它启动后就会使用默认规则,禁止一切流量,包括SSH的22端口,如果本来就是在SSH上远程操作,那么悲剧了,所以要先启用SSH的端口(默认是22,如果你设置了其他端口就加上去)

sudo ufw allow 22
sudo ufw reject 80 #(拒绝,直接返回:Connection refused)
sudo ufw deny 5053 #(否认,过段时间后返回:Connection timed out)
sudo ufw enable
sudo ufw status
  • 查看ufw防火墙是否在工作,查看使用中的规则
ufw status
  • 启动/关闭/重置ufw防火墙
ufw enable
ufw disable
ufw reset
  • 允许其它主机访问本机21端口,协议包含tcp和udp
ufw allow 21

-允许其它主机使用tcp协议访问本机80端口

ufw allow 80/tcp
  • 可以使用in或out来指定向内还是向外。如果未指定,默认是in
    允许访问本机http端口
ufw allow in http
  • 禁止其它主机访问本机80端口,
    • reject,直接告诉拒绝,返回的提示信息更多,比如ssh该主机时,直接返回:Connection refused

    • deny,否认,返回的提示信息少,比如ssh该主机时,过段时间后才返回:Connection timed out

ufw reject 80
ufw deny 80
  • 禁止本机对外访问192.168.1.1
    (实测有用)
ufw deny out to 192.168.1.1
  • 禁止192.168.1.1对内访问本机
    (实测没用)
ufw deny from 192.168.1.1
  • 删除规则,只要在命令中加入delete就行了
ufw delete deny 80/tcp
  • 打开/关闭log
ufw logging on
ufw logging off

logging on后 默认是low 等级。ufw支持多个等级: ‘low’, ‘medium’, ‘high’ and ‘full’。简单说low记录事件做少,其他等级记录逐级增加。使用默认的low level就够了。log文件保存在/var/log/ufw.log 文件内,可以用tail -n COUNT file的形式显示最后几行

参考文档

Linux下iptables屏蔽IP和端口号
Ubuntu 18.04 防火墙设置ufw详解
如何在 Ubuntu 20.04 上使用 UFW 来设置防火墙
Ubuntu自带防火墙ufw配置和用法文章来源地址https://www.toymoban.com/news/detail-655492.html

到了这里,关于【linux】iptables和ufw禁用IP和端口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • iptables -nvL查看linux系统的所有ip和端口情况

    1、查看规则 对规则的查看需要使用如下命令: 各参数的含义为: -L 表示查看当前表的所有规则,默认查看的是 filter 表,如果要查看 nat 表,可以加上 -t nat 参数。 -n 表示不对 IP 地址进行反查,加上这个参数显示速度将会加快。 -v 表示输出详细信息,包含通过该规则的数据

    2024年02月12日
    浏览(66)
  • Linux【安全 01】云服务器主机安全加固(修改SSHD端口、禁用登陆失败的IP地址、使用密钥登录)

    修改SSHD的默认端口,它可以抵御一些简单的密码暴力破解脚本。 查看登录失败的IP地址 通过下面的命令将这些登陆失败的IP加入服务器访问限制名单【失败次数最多的10个IP】 使用SSH密钥,并禁用密码登录,以MobaXterm为例进行说明。 以下命令在本机上执行(Windows) 使用Mob

    2024年02月05日
    浏览(48)
  • iptables修改目的IP和端口

    iptables NAT表的OUTPUT链用于对由本机发起的数据包进行目标IP地址和端口号的修改。DNAT指令可以用于将数据包的目标IP地址和端口号替换为指定的IP地址和端口号。在OUTPUT链中使用DNAT指令可以实现对本机发送的数据包进行目标地址的转换,将数据包发送至指定的目标地址。用法

    2024年02月05日
    浏览(33)
  • iptables指定ip访问指定端口

    2024年02月08日
    浏览(40)
  • iptables只允许指定IP调用所有端口

            首先需要设置一下允许所有ip访问22端口,要不然ssh就连不上了         当本机允许程序时,可能会访问到本机mysql、redis等数据库,所以需要允许本机ip访问

    2024年02月10日
    浏览(316)
  • [运维] iptables限制指定ip访问指定端口和只允许指定ip访问指定端口

    要使用 iptables 限制特定IP地址访问特定端口,您可以使用以下命令: 请将 IP地址 替换为要限制的IP地址,将 端口号 替换为要限制的端口号。 这个命令将添加一条规则到 iptables 的 INPUT 链,该规则匹配特定的 IP 地址和端口号,并将数据包的动作设置为 DROP ,这意味着数据包将

    2024年02月12日
    浏览(28)
  • iptables防火墙屏蔽指定ip的端口

    因为需要测试客户端程序与hadoop服务器之间正常通信需要开通的端口, 所以在hadoop各服务器上使用iptables防火墙屏蔽了测试客户端程序的ip和所有端口。然后,根据报错信息提示的端口号来逐步放开直到能正常通信下载文件。 在服务器端屏蔽指定ip访问所有端口 测试客户端程序

    2024年02月04日
    浏览(57)
  • iptables 限制所有ip访问22端口,仅开放个别ip访问 持续更新

    查看当前iptables 规则 添加已经建立tcp连接,就开放网络访问 添加允许访问22端口的ip 拒绝所有ip访问22端口 新加一个ip访问该主机22端口 保存iptables规则 参数详解 效果 本文参考:https://blog.csdn.net/zhougubei/article/details/120350467 本文参考:https://blog.csdn.net/qq_44273583/article/details/116661

    2024年02月07日
    浏览(35)
  • iptables拒绝所有端口放开特定端口方法流程,iptables允许ping和拒绝ping、hosts阻止所有ip指定放开ip方法流程、脚本检测日志异常并自动执行封堵

    收到下面一封邮件,简单来说就是需要一个虚拟机,外网能ping同,但端口全封,给他们做渗透用。 问了需求 不需要登录,封死所有端口。 所以 就用下面2个方式双重封死。 命令: iptables -P INPUT DROP 【我这就执行了这个进的】 iptables -P FORWARD DROP iptables -P OUTPUT DROP 注:上面执

    2024年02月05日
    浏览(29)
  • Ceph入门到精通-iptables 限制多个ip 的多个端口段访问

    要使用iptables限制多个IP的多个端口范围的访问,可以使用以下命令: 上面的命令将添加一条规则到INPUT链中,该规则将禁止指定IP范围访问指定的端口段。其中, 端口段 是指你要限制的端口范围,例如:80:100表示限制80到100的端口范围; 起始IP-结束IP 是指你要限制的IP范围,

    2024年02月04日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包