You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Install firewalld if not already installed
dnf install firewalld
# Start and enable firewalld
systemctl enable firewalld
systemctl start firewalld
# Check status
firewall-cmd --state
# View current configuration
firewall-cmd --list-all
# Add a service
firewall-cmd --permanent --add-service=http
# Add a port
firewall-cmd --permanent --add-port=8080/tcp
# Configure port forwarding
firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080
# Configure masquerading
firewall-cmd --permanent --add-masquerade
# Create a custom service
cat > /etc/firewalld/services/myapp.xml <<EOF<?xml version="1.0" encoding="utf-8"?><service> <short>MyApp</short> <description>My custom application</description> <port protocol="tcp" port="12345"/></service>EOF
firewall-cmd --permanent --add-service=myapp
# Reload firewall to apply changes
firewall-cmd --reload
Configure UFW (Ubuntu/Debian)
# Install UFW if not already installed
apt-get install ufw
# Set default policies
ufw default deny incoming
ufw default allow outgoing
# Allow specific services
ufw allow ssh
ufw allow http
ufw allow https
# Allow specific ports
ufw allow 8080/tcp
# Allow from specific IP address
ufw allow from 192.168.1.100
# Allow specific IP to access a specific port
ufw allow from 192.168.1.100 to any port 22
# Rate limiting (brute force protection)
ufw limit ssh
# Enable UFW
ufw enable# Check status
ufw status verbose
Configure iptables directly
# Flush existing rules
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
# Set default chain policies
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# Allow loopback traffic
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Allow established and related connections
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# Allow SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Allow HTTP and HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# Allow ICMP (ping)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# Log dropped packets
iptables -A INPUT -j LOG --log-prefix "IPTables-Dropped: " --log-level 4
# Save rules (Debian/Ubuntu)
iptables-save > /etc/iptables/rules.v4
# Save rules (RHEL/CentOS)
service iptables save
Securing SSH
Secure SSH configuration
# Backup the original configuration
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# Edit SSH configuration
cat > /etc/ssh/sshd_config.d/secure.conf <<EOF# Disable password authenticationPasswordAuthentication no# Allow only key-based authenticationPubkeyAuthentication yes# Disable root loginPermitRootLogin no# Allow only specific usersAllowUsers user1 user2# Allow only specific groupsAllowGroups sshusers wheel# Limit login attemptsMaxAuthTries 3# Change SSH port (optional)Port 2222# Disable empty passwordsPermitEmptyPasswords no# Disconnect when idleClientAliveInterval 300ClientAliveCountMax 2# Disable X11 forwardingX11Forwarding no# Use strong ciphers and MACsCiphers [email protected],[email protected]MACs [email protected],[email protected]EOF# Check configuration syntax
sshd -t
# Restart SSH service
systemctl restart sshd
Set up SSH key authentication
# Generate SSH key pair (on client)
ssh-keygen -t ed25519 -f ~/.ssh/mykey -C "my-ssh-key"# Copy public key to server (interactive method)
ssh-copy-id -i ~/.ssh/mykey.pub user@server
# Copy public key to server (manual method)
cat ~/.ssh/mykey.pub | ssh user@server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"# Configure the client to use the key
cat >>~/.ssh/config <<EOFHost myserver HostName server.example.com User myuser Port 22 IdentityFile ~/.ssh/mykey IdentitiesOnly yesEOF
chmod 600 ~/.ssh/config
Set up SSH with 2FA
# Install Google Authenticator
apt-get install libpam-google-authenticator
# Configure PAMecho"auth required pam_google_authenticator.so">> /etc/pam.d/sshd
# Update SSH config
sed -i 's/ChallengeResponseAuthentication no/ChallengeResponseAuthentication yes/' /etc/ssh/sshd_config
sed -i 's/UsePAM no/UsePAM yes/' /etc/ssh/sshd_config
echo"AuthenticationMethods publickey,keyboard-interactive">> /etc/ssh/sshd_config
# Restart SSH
systemctl restart sshd
# Setup for user (run as the user)
google-authenticator