{"id":33140019,"url":"https://github.com/pratiktri/server_init_harden","last_synced_at":"2025-11-20T05:02:45.661Z","repository":{"id":178301951,"uuid":"167062211","full_name":"pratiktri/server_init_harden","owner":"pratiktri","description":"Server hardening on 1st login as \"root\"","archived":false,"fork":false,"pushed_at":"2024-12-21T11:45:47.000Z","size":400,"stargazers_count":94,"open_issues_count":0,"forks_count":20,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-12-21T12:30:33.722Z","etag":null,"topics":["ed25519","fail2ban","linux-server","ssh","ssh-key","sshd-config","ufw","vps"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pratiktri.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-01-22T20:27:14.000Z","updated_at":"2024-12-21T11:45:51.000Z","dependencies_parsed_at":"2024-01-25T04:07:18.770Z","dependency_job_id":"1e338dde-d0c3-4090-8564-68cdcfb34337","html_url":"https://github.com/pratiktri/server_init_harden","commit_stats":null,"previous_names":["pratiktri/server_init_harden"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pratiktri/server_init_harden","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pratiktri%2Fserver_init_harden","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pratiktri%2Fserver_init_harden/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pratiktri%2Fserver_init_harden/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pratiktri%2Fserver_init_harden/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pratiktri","download_url":"https://codeload.github.com/pratiktri/server_init_harden/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pratiktri%2Fserver_init_harden/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":285375203,"owners_count":27161007,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-20T02:00:05.334Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ed25519","fail2ban","linux-server","ssh","ssh-key","sshd-config","ufw","vps"],"created_at":"2025-11-15T12:00:37.337Z","updated_at":"2025-11-20T05:02:45.654Z","avatar_url":"https://github.com/pratiktri.png","language":"Shell","funding_links":[],"categories":["Tools to apply security hardening"],"sub_categories":["GNU/Linux"],"readme":"# Linux Server Hardener\n\nPOSIX-compliant shell script that automates server security hardening on a new Linux/FreeBSD server.\nIt is intended to be executed **interactively** as `root`.\n\n## Usage\n\n- WARNING: Make sure you:\n    - Have root access to the server\n    - Have 2 ssh sessions active to the server:\n        - 1st: for running the script\n        - 2nd: for viewing script's logs and to recover from it's failure\n    - SAVE ALL CREDENTIALS SHOWN POST EXECUTION: THEY AREN'T SAVED ANYWHERE AND WON'T BE DISPLAYED AGAIN.\n\n- Options:\n    - `-r`: Reset root password\n    - `-u USERNAME`: Create a new user with sudo privileges\n    - `-h`: Display help message\n\n```sh\ncurl -L -o harden.sh https://sot.li/hardensh\ncat harden.sh          # review content\nchmod +x harden.sh\n\n# Harden server: SSH hardening, Fail2ban, Firewalld/pf\n./harden.sh\n\n# Create new privileged (sudo) user \u0026 harden server\n./harden.sh -u jay\n\n# Create new privileged user, reset root password \u0026 harden server\n./harden.sh -r -u jay\n```\n\n- Quick \u0026 Dirty:\n\n    ```sh\n    curl -sL https://sot.li/hardensh | sh -s -- -r -u jay\n    ```\n\n    \u003e There are security risks involved with running scripts directly from web, as done above. Everyone does it; but, you have been warned.\n\n## Post Installation\n\n- Linux:\n\n    ```sh\n    # Check firewall status\n    sudo firewall-cmd --state \u0026\u0026 sudo firewall-cmd --list-services\n\n    # Allow a port/service (e.g., dhcp)\n    sudo firewall-cmd --permanent --add-service=dhcp\n\n    # Block a port/service (e.g., http)\n    sudo firewall-cmd --permanent --remove-service=http\n\n    # List all active jails\n    sudo fail2ban-client status\n\n    # List all IPs banned by a jail (e.g., sshd)\n    sudo fail2ban-client status sshd\n\n    # Manually ban an IP\n    sudo fail2ban-client set sshd banip 192.0.2.1\n\n    # Manually un-ban an IP\n    sudo fail2ban-client set sshd unbanip 192.0.2.1\n    ```\n\n- FreeBSD:\n\n    ```sh\n    # Show active firewall rules\n    sudo pfctl -s rules\n\n    # Allow or block port/service\n    # Edit /etc/pf.conf \u0026 add/remove the port/service to the comma separated list in { }\n    #\n    # OR use the following command (e.g., allow dhcp)\n    sed -i.bak 's/[[:space:]]}/, dhcp }/' /etc/pf.conf \u0026\u0026 pfctl -nf /etc/pf.conf \u0026\u0026 pfctl -vvf /etc/pf.conf\n\n    # List all active Fail2ban jails\n    sudo fail2ban-client status\n\n    # List all IPs banned by a Fail2ban jail (e.g., sshd)\n    sudo fail2ban-client status sshd\n\n    # Manually ban an IP\n    sudo fail2ban-client set sshd banip 192.0.2.1\n\n    # Manually un-ban an IP\n    sudo fail2ban-client set sshd unbanip 192.0.2.1\n    ```\n\n## Status\n\nTested and working on:\n\n- Linux:\n    - Debian 13\n    - Ubuntu 22.04, 24.04\n    - Fedora 42\n    - Rocky Linux\n    - Alma Linux\n    - CentOS Stream 10\n    - openSUSE\n- FreeBSD:\n    - FreeBSD 14.3\n\n\u003e Tested with each OS's official qcow2 file through KVM virtualisation.\n\n## What does it do exactly?\n\nDepending on options chosen \u0026 OS (Linux vs FreeBSD), it does the following:\n\n1. (Optional) Resets `root` users password\n2. Creates new user \u0026 give it `sudo` privileges\n3. Generates OpenSSH (ed25519) keys (public \u0026 private) for the user with a passphrase\n4. Updates SSH configuration to:\n    - Disable `root` login\n    - Disable password login\n    - Enable sshkey-only login\n5. Installs applications:\n    - Linux: curl, sudo, firewalld, fail2ban\n    - FreeBSD: curl, sudo, fail2ban\n6. Configures firewall which allows incoming sshd, http, https traffic \u0026 blocks everything else:\n    - Linux: `firewalld` is used as firewall\n    - FreeBSD: `pf` is used as firewall\n7. Linux: Configures `fail2ban` to with following jails (FreeBSD: `pf` table is used to block IPs):\n    - sshd\n    - nginx-botsearch\n    - nginx-http-auth\n    - nginx-limit-req\n    - haproxy-http-auth\n    - recidive\n8. Displays following on console:\n    - New root password\n    - New user name \u0026 password\n    - SSH Private \u0026 Public keys\n    - SSH Passphrase\n9. Deletes SSH Private Key from server\n\n\u003e Handling Operation Failure\n\u003e\n\u003e - The script creates back up of each file it changes, in the same location as the original file. Backup file name: [original-name].bak.[timestamp]\n\u003e - On failure of an operation that depends on a configuration file, the script restores the original file and restarts the relevant service.\n\u003e - Reason for failures can be found in the log file.\n\n### Why `firewalld` and not `ufw`?\n\n- `firewalld` is default firewall on Rocky Linux, SUSE, Fedora, RHEL\n- Commands for basic administration are similar to that of `ufw`\n- Comes with a lot more power when needed\n\n## To-do\n\n- [ ] LUKS encryption\n- [ ] Unattended-updates if distro supports it (do it during installations)\n- [ ] Layer 2 security: Midtier: OSSEC\n- [ ] Audit: Lynis\n- [ ] Monitoring + Alerts: Goaccess???\n- [ ] Backups: ???\n\n## Retrospect: Why a script?\n\nYou CAN do everything this script does with Ansible. That is, if you know how it works (not trivial) and have it's *control node* installed on your local machine. I don't.\n\nPersonally, writing the script has given me deeper understanding of cloud security and about the similarities (and differences) between Unix-like operating systems.\n\nThat said, the quirks of shell scripting is tiring to keep up with. Also, most VPS providers support cloud-init. Cloud-init can't do everything the script does; but it's *trivial* to accomplish 80% of it using cloud-init. That makes it worthwhile to learn and use.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpratiktri%2Fserver_init_harden","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpratiktri%2Fserver_init_harden","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpratiktri%2Fserver_init_harden/lists"}