Use UFW (Ubuntu) or Firewalld (CentOS/AlmaLinux) to control access.
Example Login server (Ubuntu)
Sudo ufw status
sudo ufw enable
sudo ufw allow 22/tcp # Allow SSH
sudo ufw allow 80/tcp # Allow HTTP
sudo ufw deny 3306 # Block MySQL from public access
sudo ufw reload
sudo ufw status
Example (CentOS/AlmaLinux):
sudo systemctl start firewalld
sudo firewall-cmd –permanent –add-service=ssh
sudo firewall-cmd –permanent –add-port=8080/tcp # Allow HTTPS
sudo firewall-cmd –reload
If VMs do not need internet access, use private/internal networks in VirtualBox, VMware, or Proxmox.
Example:
1 . In VirtualBox, set the network to “Internal Network” instead of “Bridged Adapter”.
2 . In AWS/Azure/GCP, assign private IPs and avoid direct internet exposure.
Disable root login
sudo nano /etc/ssh/sshd_config
Set : PermitRootLogin no
Restart SSH: sudo systemctl restart ssh
Use SSH keys instead of passwords:
ssh-keygen -t rsa -b 4096
ssh-copy-id user@your-vm-ip
How ssh-copy-id Works
Install Fail2Ban to block repeated unauthorized login attempts
sudo apt install fail2ban -y # Ubuntu
sudo yum install fail2ban -y # CentOS/AlmaLinux
sudo systemctl enable fail2ban –now
Allow only specific IPs to access SSH:
sudo iptables -A INPUT -p tcp –dport 22 -s YOUR_IP -j ACCEPT
sudo iptables -A INPUT -p tcp –dport 22 -j DROP
Regularly update your system to fix vulnerabilities:
sudo apt update && sudo apt upgrade -y # Ubuntu
sudo yum update -y # CentOS/AlmaLinux
Check logs for suspicious activity:
sudo cat /var/log/auth.log # Ubuntu
sudo cat /var/log/secure # CentOS