- Added short link to down - Removed Dockerfile: can't test the script with them. Use KVM instead. - AI word salads removed
5.6 KiB
Linux Server Hardener
POSIX-compliant shell script that automates server security hardening on a new Linux/FreeBSD server. The script is intended to be executed immediately after you have access to a new Linux/FreeBSD server (most likely a VPS) as root.
Usage
-
WARNING: Make sure you:
- Have root privilege to the server
- Have 2 ssh sessions active to the server:
- 1st for running the script
- 2nd for viewing script's logs and to recover from it's failure
- SAVE ALL CREDENTIALS SHOWN POST EXECUTION: THEY AREN'T SAVED ANYWHERE AND WON'T BE DISPLAYED AGAIN
-
Options:
-r: Reset root password-u USERNAME: Create a new user with sudo privileges-h: Display help message
curl -L -o harden.sh https://sot.li/hardensh
cat harden.sh # review content
chmod +x harden.sh
# Harden server (SSH, Fail2ban, Firewalld/pf)
./harden.sh
# Create new privileged (sudo) user & harden server
./harden.sh -u jay
# Create new privileged user, reset root password & harden server
./harden.sh -r -u jay
-
Quick & dirty execute:
curl -sL https://sot.li/hardensh | sh -s -- -r -u jayThere are risks involved with running scripts directly from web, as done above. Everyone does it anyways; you have been warned.
Post Installation
-
Linux:
# Firewalld: Check firewall status sudo firewall-cmd --status && sudo firewall-cmd --list-services # Firewalld: Allow a port/service (dhcp) sudo firewall-cmd --add-service=dhcp --permanent # Firewalld: Block a port/service (http) sudo firewall-cmd --remove-service=http --permanent # Fail2ban: List all active jails sudo fail2ban-client status # Fail2ban: List all IP banned by a jail (sshd) sudo fail2ban-client status sshd # Fail2ban: Manually ban an IP sudo fail2ban-client set sshd banip 192.0.2.1 # Fail2ban: Manually un-ban an IP sudo fail2ban-client set sshd unbanip 192.0.2.1 -
FreeBSD:
# pf: active rules sudo pfctl -s rules # pf: Allow or block services # Edit /etc/pf.conf & add the port/service to the comma separated list in { } # # OR use the following command (allows dhcp) sed -i.bak 's/[[:space:]]}/, dhcp }/' /etc/pf.conf && pfctl -nf /etc/pf.conf && pfctl -vvf /etc/pf.conf # Fail2ban: List all active jails sudo fail2ban-client status # Fail2ban: List all IP banned by a jail (sshd) sudo fail2ban-client status sshd # Fail2ban: Manually ban an IP sudo fail2ban-client set sshd banip 192.0.2.1 # Fail2ban: Manually un-ban an IP sudo fail2ban-client set sshd unbanip 192.0.2.1
Status
Tested and working on:
- Linux:
- Debian 12, 13
- Fedora 42
- Ubuntu 22.04, 24.04, 24.10
- FreeBSD:
- FreeBSD 14.3
What does it do exactly?
Depending on options chosen & OS (Linux vs FreeBSD) it does the following:
- Reset
rootusers password (optional) - Create new user & give it
sudoprivileges (optional) - Generate OpenSSH (ed25519) keys (public & private) for the user with a passphrase
- Updates SSH configuration to:
a. Disable
rootlogin b. Disable password login c. Enable key-only login - Installs applications: a. Linux: curl, sudo, firewalld, fail2ban b. FreeBSD: curl, sudo, fail2ban
- Configures firewall which allows incoming sshd, http, https traffic & blocks everything else:
a. Linux:
firewalldis used as firewall b. FreeBSD:pfis used as firewall - Configures
fail2banto with following jails (FreeBSD:pftable is used to block IPs): a. sshd b. nginx-botsearch c. nginx-http-auth d. nginx-limit-req e. haproxy-http-auth f. recidive - Displays following on console: a. New root password b. New user name & password c. SSH Private & Public keys d. SSH Passphrase
- Deletes SSH Private Key from server
Why firewalld and not ufw?
firewalldis default firewall on Rocky Linux, SUSE, Fedora, RHEL- Can use similar commands like
ufwfor basic administration - Comes with a lot more power when needed
To-do
- LUKS encryption
- Unattended-updates if distro supports it (do it during installations)
- Layer 2 security: Midtier: OSSEC: Mid tier attack prevention
- Audit: Lynis: System security audits
- Monitoring + Alerts: Goaccess???
- Backups: ???
License
Copyright © 2025, Pratik Kumar Tripathy. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.