Server Initialization Script
Environment Deployment
Source: 16idc
A complete server initialization script covering security hardening, user creation, firewall, and essential software installation.
One-Command Init Script
#!/bin/bash
# server-init.sh — Fresh server initialization
# Ubuntu 22.04 / 24.04
set -euo pipefail
NEW_USER="deploy"
SSH_PORT="22" # Change to non-standard port for security
# 1. System update
apt update && apt upgrade -y && apt autoremove -y
# 2. Create deploy user
useradd -m -s /bin/bash "$NEW_USER"
usermod -aG sudo "$NEW_USER"
# 3. Harden SSH
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
systemctl restart sshd
# 4. Configure firewall
ufw allow $SSH_PORT/tcp && ufw allow 80/tcp && ufw allow 443/tcp
ufw --force enable
# 5. Install essentials
apt install -y curl wget git ufw fail2ban htop nginx docker.io docker-compose-plugin certbot
# 6. Enable fail2ban
systemctl enable --now fail2ban
echo "Server initialization complete!"
How to Use
scp server-init.sh root@your-server:~/
ssh root@your-server
chmod +x server-init.sh && sudo ./server-init.sh
What It Does
| Step |
Purpose |
| System update |
Latest packages |
| User creation |
SSH-only deploy user |
| SSH hardening |
No root, no passwords |
| Firewall |
UFW, ports 22/80/443 |
| Fail2ban |
Brute force protection |
Security Tips
- Change SSH to a non-standard port (e.g., 2222)
- Use your own SSH keys, not server-generated ones
- Enable unattended-upgrades for automatic security patches
- Monitor logs regularly with
journalctl -xe