🤖 Ai回答: To ban an IP address, follow these steps based on your environment:
---
### 1. Using `iptables` (Linux Command Line)
Block an IP:
```bash
sudo iptables -A INPUT -s <IP_ADDRESS> -j DROP
```
Save Rules (Debian/Ubuntu):
```bash
sudo apt-get install iptables-persistent
sudo netfilter-persistent save
```
Save Rules (CentOS/RHEL):
```bash
sudo service iptables save
```
---
### 2. Using `ufw` (Uncomplicated Firewall)
Block an IP:
```bash
sudo ufw deny from <IP_ADDRESS>
```
Check Status:
```bash
sudo ufw status numbered
```
Remove Rule:
```bash
sudo ufw delete [RULE_NUMBER]
```
#### Nginx
Use `fail2ban` or create a custom script. Example `nginx.conf`:
```nginx
server {
...
location / {
if ($bad_client) { return 444; }
}
}
```
Add blocked IPs to `/etc/nginx/blocked_ips.conf`:
```nginx
set $bad_client 0;
if ($remote_addr ~* "123\.45\.67\.89") { set $bad_client 1; }
```
---
### 4. Cloud Providers
#### AWS EC2
- Go to Security Groups > Select your instance’s security group.
- Edit Inbound Rules > Add a new rule with `Custom TCP`, port `ALL`, and source `<IP_ADDRESS>`.
#### DigitalOcean
- Go to Firewalls > Create a firewall.
- Add an inbound rule: Protocol `TCP`, Port `All`, Source `IPv4 Address`.
#### Google Cloud
- Go to VPC Network > Firewall rules.
- Create a rule with priority `1000`, target tags, and source IP `<IP_ADDRESS>`.