Skip to content

ixaeon/harden-ubuntu-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ubuntu Server Hardening with DevSec

Ansible playbook for hardening Ubuntu 24.04 servers using the dev-sec/ansible-collection-hardening collection.

Features

  • ✅ Creates admin user with SSH key authentication
  • ✅ Configures UFW firewall with custom SSH port
  • ✅ Applies comprehensive SSH hardening (dev-sec.hardening.ssh_hardening)
  • ✅ Applies OS-level hardening (dev-sec.hardening.os_hardening)
  • ✅ Disables password authentication
  • ✅ Removes weak cryptographic algorithms
  • ✅ Hardens system permissions and configurations

Requirements

  • Ansible: 2.16 or higher
  • Target System: Ubuntu 24.04 LTS (may work on other versions)
  • Initial Access: Root SSH access to target server

Quick Start

1. Install Ansible and Required Collections

# Install Ansible (if not already installed)
pip3 install ansible

# Install required collections
ansible-galaxy collection install devsec.hardening
ansible-galaxy collection install community.general
ansible-galaxy collection install ansible.posix

2. Clone and Configure

git clone https://github.com/yourusername/harden-ubuntu-server.git
cd harden-ubuntu-server

# Copy and customize configuration
cp group_vars/all.yml.example group_vars/all.yml
cp inventory/hosts.example inventory/hosts

# Edit with your values
vi group_vars/all.yml  # Configure admin user, SSH keys, port
vi inventory/hosts     # Add your server IP/hostname

3. Run the Playbook

# Test connectivity
ansible all -m ping

# Dry run (check mode)
ansible-playbook playbook.yml --check

# Execute hardening
ansible-playbook playbook.yml

Configuration

Key Settings (group_vars/all.yml)

# Admin user that will be created
admin_user: "yourname"
admin_home: "/home/{{ admin_user }}"
admin_shell: "/bin/bash"

# SSH keys (GitHub or local file)
ssh_keys:
  - "https://github.com/yourusername.keys"

# Custom SSH port (reduces bot attacks)
sshd_port: 2222

Inventory (inventory/hosts)

[ubuntu]
myserver ansible_host=203.0.113.10 ansible_user=root

[all:vars]
ansible_python_interpreter=/usr/bin/python3

What Gets Hardened?

Custom Role (roles/harden)

  • Creates admin user with sudo access
  • Installs SSH public keys
  • Configures UFW firewall
  • Sets custom SSH port

DevSec SSH Hardening

  • Disables password authentication
  • Removes weak ciphers and MACs
  • Configures secure SSH daemon settings
  • Hardens SSH client configuration

DevSec OS Hardening

  • Installs and configures auditd
  • Hardens file permissions (shadow, passwd, cron)
  • Configures sysctl for network security
  • Disables core dumps
  • Hardens filesystem mount options
  • And much more...

Post-Hardening

After running the playbook:

  1. Test SSH access with your admin user:

    ssh -p 2222 yourname@your-server-ip
  2. Verify sudo works:

    sudo whoami  # Should return 'root'
  3. Check firewall:

    sudo ufw status verbose
  4. Update your SSH config (~/.ssh/config):

    Host myserver
      HostName your-server-ip
      Port 2222
      User yourname
    

Troubleshooting

Can't connect after hardening?

  1. Check if you're using the correct port: ssh -p 2222 user@host
  2. Verify SSH keys are correct in group_vars/all.yml
  3. Check firewall allows your SSH port: sudo ufw status

Want to customize hardening?

The dev-sec roles are highly configurable. See their documentation:

Add overrides to group_vars/all.yml.

Testing

It's recommended to test this playbook on a disposable VM or VPS before applying to production servers.

License

MIT

Credits

This playbook uses the excellent dev-sec/ansible-collection-hardening collection.

About

Ansible playbook for hardening Ubuntu 24.04 servers using dev-sec/ansible-collection-hardening

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages