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

2024-03-27 01:59
文章标签 linux ip 禁用 端口 iptables ufw

本文主要是介绍【linux】iptables和ufw禁用IP和端口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

引言

有时候服务器,为了安全需要,会限制某些ip和端口对服务器的访问。那具体该如何配置呢?本文带你解决。
在这里插入图片描述

禁用IP和端口

1、方法1:iptables

iptables v1.8.4Usage: 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] chainiptables -E old-chain-name new-chain-nameiptables -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 rulenumDelete rule rulenum (1 = first) from chain--insert  -I chain [rulenum]Insert in chain as rulenum (default 1=first)--replace -R chain rulenumReplace 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 targetChange policy on chain to target--rename-chain-E old-chain new-chainChange 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 targettarget for rule (may load target extension)--goto      -g chainjump to chain with no return--match       -m matchextended 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 lockdefault 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 COMMANDCommands:enable                          enables the firewalldisable                         disables the firewalldefault ARG                     set default policylogging LEVEL                   set logging to LEVELallow ARGS                      add allow ruledeny ARGS                       add deny rulereject ARGS                     add reject rulelimit ARGS                      add limit ruledelete RULE|NUM                 delete RULEinsert NUM RULE                 insert RULE at NUMroute RULE                      add route RULEroute delete RULE|NUM           delete route RULEroute insert NUM RULE           insert route RULE at NUMreload                          reload firewallreset                           reset firewallstatus                          show firewall statusstatus numbered                 show firewall status as numbered list of RULESstatus verbose                  show verbose firewall statusshow ARG                        show firewall reportversion                         display version informationApplication profile commands:app list                        list application profilesapp info PROFILE                show information on PROFILEapp update PROFILE              update PROFILEapp 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配置和用法

这篇关于【linux】iptables和ufw禁用IP和端口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/850589

相关文章

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

zookeeper端口说明及介绍

《zookeeper端口说明及介绍》:本文主要介绍zookeeper端口说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、zookeeper有三个端口(可以修改)aVNMqvZ二、3个端口的作用三、部署时注意总China编程结一、zookeeper有三个端口(可以

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

linux hostname设置全过程

《linuxhostname设置全过程》:本文主要介绍linuxhostname设置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录查询hostname设置步骤其它相关点hostid/etc/hostsEDChina编程A工具license破解注意事项总结以RHE

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素