[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-71500":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":9,"language":9,"languages":9,"totalLinesOfCode":9,"stars":10,"forks":11,"watchers":12,"openIssues":13,"contributorsCount":14,"subscribersCount":14,"size":14,"stars1d":15,"stars7d":16,"stars30d":17,"stars90d":14,"forks30d":14,"starsTrendScore":18,"compositeScore":19,"rankGlobal":9,"rankLanguage":9,"license":20,"archived":21,"fork":21,"defaultBranch":22,"hasWiki":23,"hasPages":21,"topics":24,"createdAt":9,"pushedAt":9,"updatedAt":33,"readmeContent":34,"aiSummary":35,"trendingCount":14,"starSnapshotCount":14,"syncStatus":36,"lastSyncTime":37,"discoverSource":38},71500,"How-To-Secure-A-Linux-Server","imthenachoman\u002FHow-To-Secure-A-Linux-Server","imthenachoman","An evolving how-to guide for securing a Linux server.",null,27764,1836,390,32,0,39,61,332,117,44.79,"Creative Commons Attribution Share Alike 4.0 International",false,"master",true,[25,26,27,28,29,30,31,32],"cc-by-sa","hardening","hardening-steps","linux","linux-server","security","security-hardening","server","2026-06-12 02:02:53","# How To Secure A Linux Server\n\nAn evolving how-to guide for securing a Linux server that, hopefully, also teaches you a little about security and why it matters.\n\n[![CC-BY-SA](https:\u002F\u002Fi.creativecommons.org\u002Fl\u002Fby-sa\u002F4.0\u002F88x31.png)](#license)\n\n## Table of Contents\n\n- [Introduction](#introduction)\n  - [Guide Objective](#guide-objective)\n  - [Why Secure Your Server](#why-secure-your-server)\n  - [Why Yet Another Guide](#why-yet-another-guide)\n  - [Other Guides](#other-guides)\n  - [To Do \u002F To Add](#to-do--to-add)\n- [Guide Overview](#guide-overview)\n  - [About This Guide](#about-this-guide)\n  - [My Use-Case](#my-use-case)\n  - [Editing Configuration Files - For The Lazy](#editing-configuration-files---for-the-lazy)\n  - [Contributing](#contributing)\n- [Before You Start](#before-you-start)\n  - [Identify Your Principles](#identify-your-principles)\n  - [Picking A Linux Distribution](#picking-a-linux-distribution)\n  - [Installing Linux](#installing-linux)\n  - [Pre\u002FPost Installation Requirements](#prepost-installation-requirements)\n  - [Other Important Notes](#other-important-notes)\n  - [Using Ansible Playbooks to secure your Linux Server](#using-ansible-playbooks-to-secure-your-linux-server)\n- [The SSH Server](#the-ssh-server)\n  - [Important Note Before You Make SSH Changes](#important-note-before-you-make-ssh-changes)\n  - [SSH Public\u002FPrivate Keys](#ssh-publicprivate-keys)\n  - [Create SSH Group For AllowGroups](#create-ssh-group-for-allowgroups)\n  - [Secure `\u002Fetc\u002Fssh\u002Fsshd_config`](#secure-etcsshsshd_config)\n  - [Remove Short Diffie-Hellman Keys](#remove-short-diffie-hellman-keys)\n  - [2FA\u002FMFA for SSH](#2famfa-for-ssh)\n- [The Basics](#the-basics)\n  - [Limit Who Can Use sudo](#limit-who-can-use-sudo)\n  - [Limit Who Can Use su](#limit-who-can-use-su)\n  - [Run applications in a sandbox with FireJail](#run-applications-in-a-sandbox-with-firejail)\n  - [NTP Client](#ntp-client)\n  - [Securing \u002Fproc](#securing-proc)\n  - [Force Accounts To Use Secure Passwords](#force-accounts-to-use-secure-passwords)\n  - [Automatic Security Updates and Alerts](#automatic-security-updates-and-alerts)\n  - [More Secure Random Entropy Pool (WIP)](#more-secure-random-entropy-pool-wip)\n  - [Add Panic\u002FSecondary\u002FFake password Login Security System](#add-panic-secondary-fake-password-login-security-system)\n- [The Network](#the-network)\n  - [Firewall With UFW (Uncomplicated Firewall)](#firewall-with-ufw-uncomplicated-firewall)\n  - [iptables Intrusion Detection And Prevention with PSAD](#iptables-intrusion-detection-and-prevention-with-psad)\n  - [Application Intrusion Detection And Prevention With Fail2Ban](#application-intrusion-detection-and-prevention-with-fail2ban) \n  - [Application Intrusion Detection And Prevention With CrowdSec](#application-intrusion-detection-and-prevention-with-crowdsec)\n- [The Auditing](#the-auditing)\n  - [File\u002FFolder Integrity Monitoring With AIDE (WIP)](#filefolder-integrity-monitoring-with-aide-wip)\n  - [Anti-Virus Scanning With ClamAV (WIP)](#anti-virus-scanning-with-clamav-wip)\n  - [Rootkit Detection With Rkhunter (WIP)](#rootkit-detection-with-rkhunter-wip)\n  - [Rootkit Detection With chrootkit (WIP)](#rootkit-detection-with-chrootkit-wip)\n  - [logwatch - system log analyzer and reporter](#logwatch---system-log-analyzer-and-reporter)\n  - [ss - Seeing Ports Your Server Is Listening On](#ss---seeing-ports-your-server-is-listening-on)\n  - [Lynis - Linux Security Auditing](#lynis---linux-security-auditing)\n  - [OSSEC - Host Intrusion Detection](#ossec---host-intrusion-detection)\n- [The Danger Zone](#the-danger-zone)\n- [The Miscellaneous](#the-miscellaneous)\n  - [MSMTP (Simple Sendmail) with Google](#msmtp-alternative)\n  - [Gmail and Exim4 As MTA With Implicit TLS](#gmail-and-exim4-as-mta-with-implicit-tls)\n  - [Separate iptables Log File](#separate-iptables-log-file)\n- [Left Over](#left-over)\n  - [Contacting Me](#contacting-me)\n  - [Helpful Links](#helpful-links)\n  - [Acknowledgments](#acknowledgments)\n  - [License and Copyright](#license-and-copyright)\n\n(TOC made with [nGitHubTOC](https:\u002F\u002Fimthenachoman.github.io\u002FnGitHubTOC\u002F))\n\n## Introduction\n\n### Guide Objective\n\nThis guides purpose is to teach you how to secure a Linux server.\n\nThere are a lot of things you can do to secure a Linux server and this guide will attempt to cover as many of them as possible. More topics\u002Fmaterial will be added as I learn, or as folks [contribute](#contributing).\n\nAnsible playbooks of this guide are available at [How To Secure A Linux Server With Ansible](https:\u002F\u002Fgithub.com\u002Fmoltenbit\u002FHow-To-Secure-A-Linux-Server-With-Ansible) by [moltenbit](https:\u002F\u002Fgithub.com\u002Fmoltenbit).\n\n([Table of Contents](#table-of-contents))\n\n### Why Secure Your Server\n\nI assume you're using this guide because you, hopefully, already understand why good security is important. That is a heavy topic onto itself and breaking it down is out-of-scope for this guide. If you don't know the answer to that question, I advise you research it first.\n\nAt a high level, the second a  device, like a server, is in the public domain -- i.e. visible to the outside world -- it becomes a target for bad-actors. An unsecured device is a playground for bad-actors who want access to your data, or to use your server as another node for their large-scale DDOS attacks.\n\nWhat's worse is, without good security, you may never know if your server has been compromised. A bad-actor may have gained unauthorized access to your server and copied your data without changing anything, so you'd never know. Or your server may have been part of a DDOS attack, and you wouldn't know. Look at many of the large scale data breaches in the news -- the companies often did not discover the data leak or intrusion until long after the bad-actors were gone.\n\nContrary to popular belief, bad-actors don't always want to change something or [lock you out of your data for money](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FRansomware). Sometimes they just want the data on your server for their data warehouses (there is big money in big data) or to covertly use your server for their nefarious purposes.\n\n([Table of Contents](#table-of-contents))\n\n### Why Yet Another Guide\n\nThis guide may appear duplicative\u002Funnecessary because there are countless articles online that tell you [how to secure Linux](https:\u002F\u002Fduckduckgo.com\u002F?q=how+to+secure+linux&t=ffab&atb=v151-7&ia=web), but the information is spread across different articles, that cover different things, and in different ways. Who has time to scour through hundreds of articles?\n\nAs I was going through research for my Debian build, I kept notes. At the end I realized that, along with what I already knew, and what I was learning, I had the makings of a how-to guide. I figured I'd put it online to hopefully help others **learn**, and **save time**.\n\nI've never found one guide that covers everything -- this guide is my attempt.\n\nMany of the things covered in this guide may be rather basic\u002Ftrivial, but most of us do not install Linux every day, and it is easy to forget those basic things.\n\n([Table of Contents](#table-of-contents))\n\n### Other Guides\n\nThere are many guides provided by experts, industry leaders, and the distributions themselves. It is not practical, and sometimes against copyright, to include everything from those guides. I recommend you check them out before starting with this guide.\n\n- The [Center for Internet Security (CIS)](https:\u002F\u002Fwww.cisecurity.org\u002F) provides [benchmarks](https:\u002F\u002Fwww.cisecurity.org\u002Fcis-benchmarks\u002F) that are exhaustive, industry trusted, step-by-step instructions for securing many flavors of Linux. Check their [About Us](https:\u002F\u002Fwww.cisecurity.org\u002Fabout-us\u002F) page for details. My recommendation is to go through this guide (the one you're reading here) first and THEN CIS's guide. That way their recommendations will trump anything in this guide.\n- For distribution specific hardening\u002Fsecurity guides, check your distributions documentation.\n- https:\u002F\u002Fsecurity.utexas.edu\u002Fos-hardening-checklist\u002Flinux-7 - Red Hat Enterprise Linux 7 Hardening Checklist\n- https:\u002F\u002Fcloudpro.zone\u002Findex.php\u002F2018\u002F01\u002F18\u002Fdebian-9-3-server-setup-guide-part-1\u002F - # Debian 9.3 server setup guide\n- https:\u002F\u002Fblog.vigilcode.com\u002F2011\u002F04\u002Fubuntu-server-initial-security-quick-secure-setup-part-i\u002F - Ubuntu Server Initial Security guide\n- https:\u002F\u002Fwww.tldp.org\u002FLDP\u002Fsag\u002Fhtml\u002Findex.html\n- https:\u002F\u002Fseifried.org\u002Flasg\u002F\n- https:\u002F\u002Fnews.ycombinator.com\u002Fitem?id=19178964\n- https:\u002F\u002Fwiki.archlinux.org\u002Findex.php\u002FSecurity - many folks have also recommended this one\n- https:\u002F\u002Fsecurecompliance.co\u002Flinux-server-hardening-checklist\u002F\n\n([Table of Contents](#table-of-contents))\n\n### To Do \u002F To Add\n\n- [ ] [Custom Jails for Fail2ban](#custom-jails)\n- [ ] MAC (Mandatory Access Control) and Linux Security Modules (LSMs)\n   - https:\u002F\u002Fwiki.archlinux.org\u002Findex.php\u002Fsecurity#Mandatory_access_control\n   - Security-Enhanced Linux \u002F SELinux\n       - https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FSecurity-Enhanced_Linux\n       - https:\u002F\u002Flinuxtechlab.com\u002Fbeginners-guide-to-selinux\u002F\n       - https:\u002F\u002Flinuxtechlab.com\u002Freplicate-selinux-policies-among-linux-machines\u002F\n       - https:\u002F\u002Fteamignition.us\u002Fhow-to-stop-being-a-scrub-and-learn-to-use-selinux.html\n   - AppArmor\n       - https:\u002F\u002Fwiki.archlinux.org\u002Findex.php\u002FAppArmor\n       - https:\u002F\u002Fsecurity.stackexchange.com\u002Fquestions\u002F29378\u002Fcomparison-between-apparmor-and-selinux\n        - http:\u002F\u002Fwww.insanitybit.com\u002F2012\u002F06\u002F01\u002Fwhy-i-like-apparmor-more-than-selinux-5\u002F\n- [ ] disk encryption\n- [ ] Rkhunter and chrootkit\n    - http:\u002F\u002Fwww.chkrootkit.org\u002F\n    - http:\u002F\u002Frkhunter.sourceforge.net\u002F\n    - https:\u002F\u002Fwww.cyberciti.biz\u002Ffaq\u002Fhowto-check-linux-rootkist-with-detectors-software\u002F\n    - https:\u002F\u002Fwww.tecmint.com\u002Finstall-rootkit-hunter-scan-for-rootkits-backdoors-in-linux\u002F\n- [ ] shipping\u002Fbacking up logs - https:\u002F\u002Fnews.ycombinator.com\u002Fitem?id=19178681\n- [ ] CIS-CAT - https:\u002F\u002Flearn.cisecurity.org\u002Fcis-cat-landing-page\n- [ ] debsums - https:\u002F\u002Fblog.sleeplessbeastie.eu\u002F2015\u002F03\u002F02\u002Fhow-to-verify-installed-packages\u002F\n\n([Table of Contents](#table-of-contents))\n\n## Guide Overview\n\n### About This Guide\n\nThis guide...\n\n- ...**is** a work in progress.\n- ...**is** focused on **at-home** Linux servers. All of the concepts\u002Frecommendations here apply to larger\u002Fprofessional environments but those use-cases call for more advanced and specialized configurations that are out-of-scope for this guide.\n- ...**does not** teach you about Linux, how to [install Linux](#installing-linux), or how to use it. Check https:\u002F\u002Flinuxjourney.com\u002F if you're new to Linux.\n- ...**is** meant to be [Linux distribution agnostic](#picking-a-linux-distribution).\n- ...**does not** teach you everything you need to know about security nor does it get into all aspects of system\u002Fserver security. For example, physical security is out of scope for this guide.\n- ...**does not** talk about how programs\u002Ftools work, nor does it delve into their nook and crannies. Most of the programs\u002Ftools this guide references are very powerful and highly configurable. The goal is to cover the bare necessities -- enough to whet your appetite and make you hungry enough to want to go and learn more.\n- ...**aims** to make it easy by providing code you can copy-and-paste. You might need to modify the commands before you paste so keep your favorite [text editor](https:\u002F\u002Fnotepad-plus-plus.org\u002F) handy.\n- ...**is** organized in an order that makes logical sense to me -- i.e. securing SSH before installing a firewall. As such, this guide is intended to be followed in the order it is presented, but it is not necessary to do so. Just be careful if you do things in a different order -- some sections require previous sections to be completed.\n\n([Table of Contents](#table-of-contents))\n\n### My Use-Case\n\nThere are many types of servers and different use-cases. While I want this guide to be as generic as possible, there will be some things that may not apply to all\u002Fother use-cases. Use your best judgement when going through this guide.\n\nTo help put context to many of the topics covered in this guide, my use-case\u002Fconfiguration is:\n\n- A desktop class computer...\n- With a single NIC...\n- Connected to a consumer grade router...\n- Getting a dynamic WAN IP provided by the ISP...\n- With WAN+LAN on IPV4...\n- And LAN using [NAT](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FNetwork_address_translation)...\n- That I want to be able to SSH to remotely from unknown computers and unknown locations (i.e. a friend's house).\n\n([Table of Contents](#table-of-contents))\n\n### Editing Configuration Files - For The Lazy\n\nI am very lazy and do not like to edit files by hand if I don't need to. I also assume everyone else is just like me. :)\n\nSo, when and where possible, I have provided `code` snippets to quickly do what is needed, like add or change a line in a configuration file.\n\nThe `code` snippets use basic commands like `echo`, `cat`, `sed`, `awk`, and `grep`. How the `code` snippets work, like what each command\u002Fpart does, is out of scope for this guide -- the `man` pages are your friend.\n\n**Note**: The `code` snippets do not validate\u002Fverify the change went through -- i.e. the line was actually added or changed. I'll leave the verifying part in your capable hands. The steps in this guide do include taking backups of all files that will be changed.\n\nNot all changes can be automated with `code` snippets. Those changes need good, old-fashioned, manual editing. For example, you can't just append a line to an [INI](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FINI_file) type file. Use your [favorite](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FVi) Linux text editor.\n\n([Table of Contents](#table-of-contents))\n\n### Contributing\n\nI wanted to put this guide on [GitHub](http:\u002F\u002Fwww.github.com) to make it easy to collaborate. The more folks that contribute, the better and more complete this guide will become.\n\nTo contribute you can fork and submit a pull request or submit a [new issue](https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server\u002Fissues\u002Fnew).\n\n([Table of Contents](#table-of-contents))\n\n## Before You Start\n\n### Identify Your Principles\n\nBefore you start you will want to identify what your Principles are. What is your [threat model](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FThreat_model)? Some things to think about:\n\n- Why do you want to secure your server?\n- How much security do you want or not want?\n- How much convenience are you willing to compromise for security and vice-versa?\n- What are the threats you want to protect against? What are the specifics to your situation? For example:\n  - Is physical access to your server\u002Fnetwork a possible attack vector?\n  - Will you be opening ports on your router so you can access your server from outside your home?\n  - Will you be hosting a file share on your server that will be mounted on a desktop class machine? What is the possibility of the desktop machine getting infected and, in turn, infecting the server?\n - Do you have a means of recovering if your security implementation locks you out of your own server? For example, you [disabled root login](#disable-root-login) or [password protected GRUB](#password-protect-grub).\n\nThese are just **a few things** to think about. Before you start securing your server you will want to understand what you're trying to protect against and why so you know what you need to do.\n\n([Table of Contents](#table-of-contents))\n\n### Picking A Linux Distribution\n\nThis guide is intended to be distribution agnostic so users can use [any distribution](https:\u002F\u002Fdistrowatch.com\u002F) they want. With that said, there are a few things to keep in mind:\n\nYou want a distribution that...\n\n- ...**is stable**. Unless you like debugging issues at 2 AM, you don't want an [unattended upgrade](#automatic-security-updates-and-alerts), or a manual package\u002Fsystem update, to render your server inoperable. But this also means you're okay with not running the latest, greatest, bleeding edge software.\n- ...**stays up-to-date with security patches**. You can secure everything on your server, but if the core OS or applications you're running have known vulnerabilities, you'll never be safe.\n- ...**you're familiar with.** If you don't know Linux, I would advise you play around with one before you try to secure it. You should be comfortable with it and know your way around, like how to install software, where configuration files are, etc...\n- ...**is well-supported.** Even the most seasoned admin needs help every now and then. Having a place to go for help will save your sanity.\n\n([Table of Contents](#table-of-contents))\n\n### Installing Linux\n\nInstalling Linux is out-of-scope for this guide because each distribution does it differently and the installation instructions are usually well documented. If you need help, start with your distribution's documentation. Regardless of the distribution, the high-level process usually goes like so:\n\n1. download the ISO\n1. burn\u002Fcopy\u002Ftransfer it to your install medium (e.g. a CD or USB stick)\n1. boot your server from your install medium\n1. follow the prompts to install\n\nWhere applicable, use the expert install option so you have tighter control of what is running on your server. **Only install what you absolutely need.** I, personally, do not install anything other than SSH. Also, tick the Disk Encryption option.\n\n([Table of Contents](#table-of-contents))\n\n### Pre\u002FPost Installation Requirements\n\n- If you're opening ports on your router so you can access your server from the outside, disable the port forwarding until your system is up and secured.\n- Unless you're doing everything physically connected to your server, you'll need remote access so be sure SSH works.\n- Keep your system up-to-date (i.e. `sudo apt update && sudo apt upgrade` on Debian based systems).\n- Make sure you perform any tasks specific to your setup like:\n  - Configuring network\n  - Configuring mount points in `\u002Fetc\u002Ffstab`\n  - Creating the initial user accounts\n  - Installing core software you'll want like `man`\n  - Etc...\n- Your server will need to be able to send e-mails so you can get important security alerts. If you're not setting up a mail server check [Gmail and Exim4 As MTA With Implicit TLS](#gmail-and-exim4-as-mta-with-implicit-tls).\n- I would also recommend you **read** through the [CIS Benchmarks](https:\u002F\u002Fwww.cisecurity.org\u002Fcis-benchmarks\u002F) before you start with this guide just to digest\u002Funderstand what they have to say. My recommendation is to go through this guide (the one you're reading here) first and THEN CIS's guide. That way their recommendations will trump anything in this guide.\n\n([Table of Contents](#table-of-contents))\n\n### Other Important Notes\n\n- This guide is being written and tested on Debian. Most things below should work on other distributions. If you find something that does not, please [contact me](#contacting-me). The main thing that separates each distribution will be its package management system. Since I use Debian, I will provide the appropriate `apt` commands that should work on all [Debian based distributions](https:\u002F\u002Fwww.debian.org\u002Fderivatives\u002F). If someone is willing to [provide](#contributing) the respective commands for other distributions, I will add them.\n- File paths and settings also may differ slightly -- check with your distribution's documentation if you have issues.\n- Read the whole guide before you start. Your use-case and\u002For principals may call for not doing something or for changing the order.\n- Do not **blindly** copy-and-paste without understanding what you're pasting. Some commands will need to be modified for your needs before they'll work -- usernames for example.\n\n([Table of Contents](#table-of-contents))\n\n### Using Ansible playbooks to secure your Linux Server\nAnsible playbooks of this guide are available at [How To Secure A Linux Server With Ansible](https:\u002F\u002Fgithub.com\u002Fmoltenbit\u002FHow-To-Secure-A-Linux-Server-With-Ansible).\n\nMake sure to edit the variables according to your needs and read all tasks beforehand to confirm it does not break your system. After running the playbooks ensure that all settings are configured to your needs!\n\n1. Install [Ansible](https:\u002F\u002Fdocs.ansible.com\u002Fansible\u002Flatest\u002Finstallation_guide\u002Fintro_installation.html)\n2. git clone [How To Secure A Linux Server With Ansible](https:\u002F\u002Fgithub.com\u002Fmoltenbit\u002FHow-To-Secure-A-Linux-Server-With-Ansible)\n3. [Create SSH-Public\u002FPrivate-Keys](https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server#ssh-publicprivate-keys)\n  ```\n  ssh-keygen -t ed25519\n  ```\n   \n5. Change all variables in *group_vars\u002Fvariables.yml* according to your needs.\n6. Enable SSH root access before running the playbooks:\n   \n  ```\n  nano \u002Fetc\u002Fssh\u002Fsshd_config\n  [...]\n  PermitRootLogin yes\n  [...]\n  ```\n\n7. Recommended: configure static IP address on your system.\n8. Add your systems IP address to *hosts.yml*.\n\n&nbsp;\n\nRun the requirements playbook using the root password you specified while installing the server:\n\n    ansible-playbook --inventory hosts.yml --ask-pass requirements-playbook.yml\n\n&nbsp;\n\nRun the main playbook with the new users password you specified in the *variables.yml* file:\n\n    ansible-playbook --inventory hosts.yml --ask-pass main-playbook.yml\n\n&nbsp;\n\nIf you need to run the playbooks multiple times remember to use the SSH key and the new SSH port:\n\n    ansible-playbook --inventory hosts.yml -e ansible_ssh_port=SSH_PORT --key-file \u002FPATH\u002FTO\u002FSSH\u002FKEY main-playbook.yml\n\n\n([Table of Contents](#table-of-contents))\n\n## The SSH Server\n\n### Important Note Before You Make SSH Changes\n\nIt is highly advised you keep a 2nd terminal open to your server **before you make and apply SSH configuration changes**. This way if you lock yourself out of your 1st terminal session, you still have one session connected so you can fix it.\n\nThank you to [Sonnenbrand](https:\u002F\u002Fgithub.com\u002FSonnenbrand) for this [idea](https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server\u002Fissues\u002F56).\n\n### SSH Public\u002FPrivate Keys\n\n#### Why\n\nUsing SSH public\u002Fprivate keys is more secure than using a password. It also makes it easier and faster, to connect to our server because you don't have to enter a password.\n\n#### How It Works\n\nCheck the references below for more details but, at a high level, public\u002Fprivate keys work by using a pair of keys to verify identity.\n\n1. One key, the **public** key, **can only encrypt data**, not decrypt it\n1. The other key, the **private** key, can decrypt the data\n\nFor SSH, a public and private key is created on the client. You want to keep both keys secure, especially the private key. Even though the public key is meant to be public, it is wise to make sure neither keys fall in the wrong hands.\n\nWhen you connect to an SSH server, SSH will look for a public key that matches the client you're connecting from in the file `~\u002F.ssh\u002Fauthorized_keys` on the server you're connecting to. Notice the file is in the **home folder** of the ID you're trying to connect to. So, after creating the public key, you need to append it to `~\u002F.ssh\u002Fauthorized_keys`. One approach is to copy it to a USB stick and physically transfer it to the server. Another approach is to use [`ssh-copy-id`](https:\u002F\u002Fwww.ssh.com\u002Fssh\u002Fcopy-id) to transfer and append the public key.\n\nAfter the keys have been created and the public key has been appended to `~\u002F.ssh\u002Fauthorized_keys` on the host, SSH uses the public and private keys to verify identity and then establish a secure connection. How identity is verified is a complicated process but [Digital Ocean](https:\u002F\u002Fwww.digitalocean.com\u002Fcommunity\u002Ftutorials\u002Funderstanding-the-ssh-encryption-and-connection-process) has a very nice write-up of how it works. At a high level, identity is verified by the server encrypting a challenge message with the public key, then sending it to the client. If the client cannot decrypt the challenge message with the private key, the identity can't be verified and a connection will not be established.\n\nThey are considered more secure because you need the private key to establish an SSH connection. If you set [`PasswordAuthentication no` in `\u002Fetc\u002Fssh\u002Fsshd_config`](#PasswordAuthentication), then SSH won't let you connect without the private key.\n\nYou can also set a pass-phrase for the keys which would require you to enter the key pass-phrase when connecting using public\u002Fprivate keys. Keep in mind doing this means you can't use the key for automation because you'll have no way to send the passphrase in your scripts. `ssh-agent` is a program that is shipped in many Linux distros (and usually already running) that will allow you to hold your unencrypted private key in memory for a configurable duration. Simply run `ssh-add` and it will prompt you for your passphrase. You will not be prompted for your passphrase again until the configurable duration has passed.\n\nWe will be using Ed25519 keys which, according to [https:\u002F\u002Flinux-audit.com\u002F](https:\u002F\u002Flinux-audit.com\u002Fusing-ed25519-openssh-keys-instead-of-dsa-rsa-ecdsa\u002F):\n\n> It is using an elliptic curve signature scheme, which offers better security than ECDSA and DSA. At the same time, it also has good performance.\n\n#### Goals\n\n- Ed25519 public\u002Fprivate SSH keys:\n  - private key on your client\n  - public key on your server\n\n#### Notes\n\n- You'll need to do this step for every computer and account you'll be connecting to your server from\u002Fas.\n\n#### References\n\n- https:\u002F\u002Fwww.ssh.com\u002Fssh\u002Fpublic-key-authentication\n- https:\u002F\u002Fhelp.ubuntu.com\u002Fcommunity\u002FSSH\u002FOpenSSH\u002FKeys\n- https:\u002F\u002Flinux-audit.com\u002Fusing-ed25519-openssh-keys-instead-of-dsa-rsa-ecdsa\u002F\n- https:\u002F\u002Fwww.digitalocean.com\u002Fcommunity\u002Ftutorials\u002Funderstanding-the-ssh-encryption-and-connection-process\n- https:\u002F\u002Fwiki.archlinux.org\u002Findex.php\u002FSSH_Keys\n- https:\u002F\u002Fwww.ssh.com\u002Fssh\u002Fcopy-id\n- `man ssh-keygen`\n- `man ssh-copy-id`\n- `man ssh-add`\n\n#### Steps\n\n1. From the computer you're going to use to connect to your server, **the client**, not the server itself, create an [Ed25519](https:\u002F\u002Flinux-audit.com\u002Fusing-ed25519-openssh-keys-instead-of-dsa-rsa-ecdsa\u002F) key with `ssh-keygen`:\n\n    ``` bash\n    ssh-keygen -t ed25519\n    ```\n\n    > ```\n    > Generating public\u002Fprivate ed25519 key pair.\n    > Enter file in which to save the key (\u002Fhome\u002Fuser\u002F.ssh\u002Fid_ed25519):\n    > Created directory '\u002Fhome\u002Fuser\u002F.ssh'.\n    > Enter passphrase (empty for no passphrase):\n    > Enter same passphrase again:\n    > Your identification has been saved in \u002Fhome\u002Fuser\u002F.ssh\u002Fid_ed25519.\n    > Your public key has been saved in \u002Fhome\u002Fuser\u002F.ssh\u002Fid_ed25519.pub.\n    > The key fingerprint is:\n    > SHA256:F44D4dr2zoHqgj0i2iVIHQ32uk\u002FLx4P+raayEAQjlcs user@client\n    > The key's randomart image is:\n    > +--[ED25519 256]--+\n    > |xxxx  x          |\n    > |o.o +. .         |\n    > | o o oo   .      |\n    > |. E oo . o .     |\n    > | o o. o S o      |\n    > |... .. o o       |\n    > |.+....+ o        |\n    > |+.=++o.B..       |\n    > |+..=**=o=.       |\n    > +----[SHA256]-----+\n    > ```\n\n    **Note**: If you set a passphrase, you'll need to enter it every time you connect to your server using this key, unless you're using `ssh-agent`.\n\n1. Now you need to **append** the public key `~\u002F.ssh\u002Fid_ed25519.pub` from your client to the `~\u002F.ssh\u002Fauthorized_keys` file on your server. Since we're presumable still at home on the LAN, we're probably safe from [MIM](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FMan-in-the-middle_attack) attacks, so we will use `ssh-copy-id` to transfer and append the public key:\n\n    ``` bash\n    ssh-copy-id user@server\n    ```\n\n    > ```\n    > \u002Fusr\u002Fbin\u002Fssh-copy-id: INFO: Source of key(s) to be installed: \"\u002Fhome\u002Fuser\u002F.ssh\u002Fid_ed25519.pub\"\n    > The authenticity of host 'host (192.168.1.96)' can't be established.\n    > ECDSA key fingerprint is SHA256:QaDQb\u002FX0XyVlogh87sDXE7MR8YIK7ko4wS5hXjRySJE.\n    > Are you sure you want to continue connecting (yes\u002Fno)? yes\n    > \u002Fusr\u002Fbin\u002Fssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed\n    > \u002Fusr\u002Fbin\u002Fssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys\n    > user@host's password:\n    > \n    > Number of key(s) added: 1\n    > \n    > Now try logging into the machine, with:   \"ssh 'user@host'\"\n    > and check to make sure that only the key(s) you wanted were added.\n    > ```\n\nNow would be a good time to [perform any tasks specific to your setup](#prepost-installation-requirements).\n\n([Table of Contents](#table-of-contents))\n\n### Create SSH Group For AllowGroups\n\n#### Why\n\nTo make it easy to control who can SSH to the server. By using a group, we can quickly add\u002Fremove accounts to the group to quickly allow or not allow SSH access to the server.\n\n#### How It Works\n\nWe will use the [AllowGroups option](#AllowGroups) in SSH's configuration file [`\u002Fetc\u002Fssh\u002Fsshd_config`](#secure-etcsshsshd_config) to tell the SSH server to only allow users to SSH in if they are a member of a certain UNIX group. Anyone not in the group will not be able to SSH in.\n\n#### Goals\n\n- a UNIX group that we'll use in [Secure `\u002Fetc\u002Fssh\u002Fsshd_config`](#secure-etcsshsshd_config) to limit who can SSH to the server\n\n#### Notes\n\n- This is a prerequisite step to support the `AllowGroup` setting set in [Secure `\u002Fetc\u002Fssh\u002Fsshd_config`](#secure-etcsshsshd_config).\n\n#### References\n\n- `man groupadd`\n- `man usermod`\n\n#### Steps\n\n1. Create a group:\n\n    ``` bash\n    sudo groupadd sshusers\n    ```\n\n1. Add account(s) to the group:\n\n    ``` bash\n    sudo usermod -a -G sshusers user1\n    sudo usermod -a -G sshusers user2\n    sudo usermod -a -G sshusers ...\n    ```\n\n    You'll need to do this for every account on your server that needs SSH access.\n\n([Table of Contents](#table-of-contents))\n\n### Secure `\u002Fetc\u002Fssh\u002Fsshd_config`\n\n#### Why\n\nSSH is a door into your server. This is especially true if you are opening ports on your router so you can SSH to your server from outside your home network. If it is not secured properly, a bad-actor could use it to gain unauthorized access to your system.\n\n#### How It Works\n\n`\u002Fetc\u002Fssh\u002Fsshd_config` is the default configuration file that the SSH server uses. We will use this file to tell what options the SSH server should use.\n\n#### Goals\n\n- a secure SSH configuration\n\n#### Notes\n\n- Make sure you've completed [Create SSH Group For AllowGroups](#create-ssh-group-for-allowgroups) first.\n\n#### References\n\n- Mozilla's OpenSSH guidelines for OpenSSH 6.7+ at https:\u002F\u002Finfosec.mozilla.org\u002Fguidelines\u002Fopenssh#modern-openssh-67\n- https:\u002F\u002Flinux-audit.com\u002Faudit-and-harden-your-ssh-configuration\u002F\n- https:\u002F\u002Fwww.ssh.com\u002Fssh\u002Fsshd_config\u002F\n- https:\u002F\u002Fwww.techbrown.com\u002Fharden-ssh-secure-linux-vps-server\u002F (broken; try http:\u002F\u002Fweb.archive.org\u002Fweb\u002F20200413100933\u002Fhttps:\u002F\u002Fwww.techbrown.com\u002Fharden-ssh-secure-linux-vps-server\u002F)\n- https:\u002F\u002Fserverfault.com\u002Fquestions\u002F660160\u002Fopenssh-difference-between-internal-sftp-and-sftp-server\u002F660325\n- `man sshd_config`\n- Thanks to [than0s](https:\u002F\u002Fgithub.com\u002Fthan0s) for [how to find duplicate settings](https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server\u002Fissues\u002F38).\n\n#### Steps\n\n1. Make a backup of OpenSSH server's configuration file `\u002Fetc\u002Fssh\u002Fsshd_config` and remove comments to make it easier to read:\n\n    ``` bash\n    sudo cp --archive \u002Fetc\u002Fssh\u002Fsshd_config \u002Fetc\u002Fssh\u002Fsshd_config-COPY-$(date +\"%Y%m%d%H%M%S\")\n    sudo sed -i -r -e '\u002F^#|^$\u002F d' \u002Fetc\u002Fssh\u002Fsshd_config\n    ```\n\n1. Edit `\u002Fetc\u002Fssh\u002Fsshd_config` then find and edit or add these settings that should be applied regardless of your configuration\u002Fsetup:\n\n    **Note**: SSH does not like duplicate contradicting settings. For example, if you have `ChallengeResponseAuthentication no` and then `ChallengeResponseAuthentication yes`, SSH will respect the first one and ignore the second. Your `\u002Fetc\u002Fssh\u002Fsshd_config` file may already have some of the settings\u002Flines below. To avoid issues you will need to manually go through your `\u002Fetc\u002Fssh\u002Fsshd_config` file and address any duplicate contradicting settings. \n\n\t**Note:** If you are running OpenSSH 9.1 or later, uncomment the `RequiredRSASize 3072` line in the configuration below. This enforces a minimum RSA key size of 3072 bits and will reject smaller RSA keys during authentication. This only affects RSA keys. If you use ED25519 or ECDSA keys, you are not affected. You can check your key type and size with `ssh-keygen -l -f ~\u002F.ssh\u002Fid_rsa`. On older OpenSSH versions, leave the line commented out as it will prevent sshd from starting.\n   \n    ```\n    ########################################################################################################\n    # start settings from https:\u002F\u002Finfosec.mozilla.org\u002Fguidelines\u002Fopenssh#modern-openssh-67 as of 2019-01-01\n    ########################################################################################################\n\n    # Supported HostKey algorithms by order of preference.\n    HostKey \u002Fetc\u002Fssh\u002Fssh_host_ed25519_key\n    HostKey \u002Fetc\u002Fssh\u002Fssh_host_rsa_key\n    HostKey \u002Fetc\u002Fssh\u002Fssh_host_ecdsa_key\n\n    KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256\n\n    Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr\n\n    MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,umac-128@openssh.com\n\n    # LogLevel VERBOSE logs user's key fingerprint on login. Needed to have a clear audit track of which key was using to log in.\n    LogLevel VERBOSE\n\n    # Use kernel sandbox mechanisms where possible in unprivileged processes\n    # Systrace on OpenBSD, Seccomp on Linux, seatbelt on MacOSX\u002FDarwin, rlimit elsewhere.\n    # Note: This setting is deprecated in OpenSSH 7.5 (https:\u002F\u002Fwww.openssh.com\u002Ftxt\u002Frelease-7.5)\n    # UsePrivilegeSeparation sandbox\n\n    ########################################################################################################\n    # end settings from https:\u002F\u002Finfosec.mozilla.org\u002Fguidelines\u002Fopenssh#modern-openssh-67 as of 2019-01-01\n    ########################################################################################################\n\n    # don't let users set environment variables\n    PermitUserEnvironment no\n\n    # Log sftp level file access (read\u002Fwrite\u002Fetc.) that would not be easily logged otherwise.\n    Subsystem sftp  internal-sftp -f AUTHPRIV -l INFO\n\n    # disable X11 forwarding as X11 is very insecure\n    # you really shouldn't be running X on a server anyway\n    X11Forwarding no\n\n    # disable port forwarding\n    AllowTcpForwarding no\n    AllowStreamLocalForwarding no\n    GatewayPorts no\n    PermitTunnel no\n\n    # don't allow login if the account has an empty password\n    PermitEmptyPasswords no\n\n    # ignore .rhosts and .shosts\n    IgnoreRhosts yes\n\n    # verify hostname matches IP\n    UseDNS yes\n\n    Compression no\n    \n    # TCP keepalive is spoofable (runs outside the encrypted channel)\n\t# Use ClientAlive instead (runs inside the encrypted channel)\n    TCPKeepAlive no\n    \n    AllowAgentForwarding no\n    PermitRootLogin no\n\n    # don't allow .rhosts or \u002Fetc\u002Fhosts.equiv\n    HostbasedAuthentication no\n\n    # OpenSSH 9.1 and later\n    # Enforce a minimum RSA key size of 3072 bits\n    # https:\u002F\u002Fwww.keylength.com\u002Fen\u002Fcompare\u002F\n    # RequiredRSASize 3072\n\n    # https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server\u002Fissues\u002F115\n    HashKnownHosts yes\n    ```\n\n1. Then **find and edit or add** these settings, and set values as per your requirements:\n\n    |Setting|Valid Values|Example|Description|Notes|\n    |--|--|--|--|--|\n    |\u003Ca name=\"AllowGroups\">\u003C\u002Fa>**AllowGroups**|local UNIX group name|`AllowGroups sshusers`|group to allow SSH access to||\n    |**ClientAliveCountMax**|number|`ClientAliveCountMax 3`|maximum number of client alive messages sent without response||\n    |**ClientAliveInterval**|number of seconds|`ClientAliveInterval 15`|timeout in seconds before a response request||\n    |**ListenAddress**|space separated list of local addresses|\u003Cul>\u003Cli>`ListenAddress 0.0.0.0`\u003C\u002Fli>\u003Cli>`ListenAddress 192.168.1.100`\u003C\u002Fli>\u003C\u002Ful>|local addresses `sshd` should listen on|See [Issue #1](https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server\u002Fissues\u002F1) for important details.|\n    |**LoginGraceTime**|number of seconds|`LoginGraceTime 30`|time in seconds before login times-out||\n    |**MaxAuthTries**|number|`MaxAuthTries 2`|maximum allowed attempts to login||\n    |**MaxSessions**|number|`MaxSessions 2`|maximum number of open sessions||\n    |**MaxStartups**|number|`MaxStartups 2`|maximum number of login sessions||\n    |\u003Ca name=\"PasswordAuthentication\">\u003C\u002Fa>**PasswordAuthentication**|`yes` or `no`|`PasswordAuthentication no`|if login with a password is allowed||\n    |**Port**|any open\u002Favailable port number|`Port 22`|port that `sshd` should listen on||\n\n    Check `man sshd_config` for more details what these settings mean.\n\n1. Make sure there are no duplicate settings that contradict each other. The below command should not have any output.\n\n    ```bash\n    awk 'NF && $1!~\u002F^(#|HostKey)\u002F{print $1}' \u002Fetc\u002Fssh\u002Fsshd_config | sort | uniq -c | grep -v ' 1 '\n    ```\n\n1. Restart ssh:\n\n    ``` bash\n    sudo service sshd restart\n    ```\n\n1. You can check verify the configurations worked with `sshd -T` and verify the output:\n\n    ``` bash\n    sudo sshd -T\n    ```\n\n    > ```\n    > port 22\n    > addressfamily any\n    > listenaddress [::]:22\n    > listenaddress 0.0.0.0:22\n    > usepam yes\n    > logingracetime 30\n    > x11displayoffset 10\n    > maxauthtries 2\n    > maxsessions 2\n    > clientaliveinterval 15\n    > clientalivecountmax 3\n    > streamlocalbindmask 0177\n    > permitrootlogin no\n    > ignorerhosts yes\n    > ignoreuserknownhosts no\n    > hostbasedauthentication no\n    > ...\n    > subsystem sftp internal-sftp -f AUTHPRIV -l INFO\n    > maxstartups 2:30:2\n    > permittunnel no\n    > ipqos lowdelay throughput\n    > rekeylimit 0 0\n    > permitopen any\n    > ```\n\n([Table of Contents](#table-of-contents))\n\n### Remove Short Diffie-Hellman Keys\n\n#### Why\n\nPer [Mozilla's OpenSSH guidelines for OpenSSH 6.7+](https:\u002F\u002Finfosec.mozilla.org\u002Fguidelines\u002Fopenssh#modern-openssh-67), \"all Diffie-Hellman moduli in use should be at least 3072-bit-long\".\n\nThe Diffie-Hellman algorithm is used by SSH to establish a secure connection. The larger the moduli (key size) the stronger the encryption.\n\n#### Goals\n\n- remove all Diffie-Hellman keys that are less than 3072 bits long\n\n#### References\n\n- Mozilla's OpenSSH guidelines for OpenSSH 6.7+ at https:\u002F\u002Finfosec.mozilla.org\u002Fguidelines\u002Fopenssh#modern-openssh-67\n- https:\u002F\u002Finfosec.mozilla.org\u002Fguidelines\u002Fkey_management\n- `man moduli`\n\n#### Steps\n\n1. Make a backup of SSH's moduli file `\u002Fetc\u002Fssh\u002Fmoduli`:\n\n    ``` bash\n    sudo cp --archive \u002Fetc\u002Fssh\u002Fmoduli \u002Fetc\u002Fssh\u002Fmoduli-COPY-$(date +\"%Y%m%d%H%M%S\")\n    ```\n\n1. Remove short moduli:\n\n    ``` bash\n    sudo awk '$5 >= 3071' \u002Fetc\u002Fssh\u002Fmoduli | sudo tee \u002Fetc\u002Fssh\u002Fmoduli.tmp\n    sudo mv \u002Fetc\u002Fssh\u002Fmoduli.tmp \u002Fetc\u002Fssh\u002Fmoduli\n    ````\n\n([Table of Contents](#table-of-contents))\n\n### 2FA\u002FMFA for SSH\n\n#### Why\n\nEven though SSH is a pretty good security guard for your doors and windows, it is still a visible door that bad-actors can see and try to brute-force in. [Fail2ban](#fail2ban-application-intrusion-detection-and-prevention) will monitor for these brute-force attempts but there is no such thing as being too secure. Requiring two factors adds an extra layer of security.\n\nUsing Two-Factor Authentication (2FA) \u002F Multi-Factor Authentication (MFA) requires anyone entering to have **two** keys to enter which makes it harder for bad actors. The two keys are:\n\n1. Their password\n1. A 6 digit token that changes every 30 seconds\n\nWithout both keys, they won't be able to get in.\n\n#### Why Not\n\nMany folks might find the experience cumbersome or annoying. And, access to your system is dependent on the accompanying authenticator app that generates the code.\n\n#### How It Works\n\nOn Linux, PAM is responsible for authentication. There are four tasks to PAM that you can read about at https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FLinux_PAM. This section talks about the authentication task.\n\nWhen you log into a server, be it directly from the console or via SSH, the door you came through will send the request to the authentication task of PAM and PAM will ask for and verify your password. You can customize the rules each doors use. For example, you could have one set of rules when logging in directly from the console and another set of rules for when logging in via SSH.\n\nThis section will alter the authentication rules for when logging in via SSH to require both a password and a 6 digit code.\n\nWe will use Google's libpam-google-authenticator PAM module to create and verify a [TOTP](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FTime-based_One-time_Password_algorithm) key. https:\u002F\u002Ffastmail.blog\u002F2016\u002F07\u002F22\u002Fhow-totp-authenticator-apps-work\u002F and https:\u002F\u002Fjemurai.com\u002F2018\u002F10\u002F11\u002Fhow-it-works-totp-based-mfa\u002F have very good writeups of how TOTP works.\n\nWhat we will do is tell the server's SSH PAM configuration to ask the user for their password and then their numeric token. PAM will then verify the user's password and, if it is correct, then it will route the authentication request to libpam-google-authenticator which will ask for and verify your 6 digit token. If, and only if, everything is good will the authentication succeed and user be allowed to log in.\n\n#### Goals\n\n- 2FA\u002FMFA enabled for all SSH connections\n\n#### Notes\n\n- Before you do this, you should have an idea of how 2FA\u002FMFA works and you'll need an authenticator app on your phone to continue.\n- We'll use [google-authenticator-libpam](https:\u002F\u002Fgithub.com\u002Fgoogle\u002Fgoogle-authenticator-libpam).\n- With the below configuration, a user will only need to enter their 2FA\u002FMFA code if they are logging on with their password but **not** if they are using [SSH public\u002Fprivate keys](#ssh-publicprivate-keys). Check the documentation on how to change this behavior to suite your requirements.\n\n#### References\n\n- https:\u002F\u002Fgithub.com\u002Fgoogle\u002Fgoogle-authenticator-libpam\n- https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FLinux_PAM\n- https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FTime-based_One-time_Password_algorithm\n- https:\u002F\u002Ffastmail.blog\u002F2016\u002F07\u002F22\u002Fhow-totp-authenticator-apps-work\u002F\n- https:\u002F\u002Fjemurai.com\u002F2018\u002F10\u002F11\u002Fhow-it-works-totp-based-mfa\u002F\n\n#### Steps\n\n1. Install it libpam-google-authenticator.\n\n    On Debian based systems:\n\n    ``` bash\n    sudo apt install libpam-google-authenticator\n    ```\n\n1. **Make sure you're logged in as the ID you want to enable 2FA\u002FMFA for** and **execute** `google-authenticator` to create the necessary token data:\n\n    ``` bash\n    google-authenticator\n    ```\n\n    > ```\n    > Do you want authentication tokens to be time-based (y\u002Fn) y\n    > https:\u002F\u002Fwww.google.com\u002Fchart?chs=200x200&chld=M|0&cht=qr&chl=otpauth:\u002F\u002Ftotp\u002Fuser@host%3Fsecret%3DR4ZWX34FQKZROVX7AGLJ64684Y%26issuer%3Dhost\n    > \n    > ...\n    > \n    > Your new secret key is: R3NVX3FFQKZROVX7AGLJUGGESY\n    > Your verification code is 751419\n    > Your emergency scratch codes are:\n    >   12345678\n    >   90123456\n    >   78901234\n    >   56789012\n    >   34567890\n    > \n    > Do you want me to update your \"\u002Fhome\u002Fuser\u002F.google_authenticator\" file (y\u002Fn) y\n    > \n    > Do you want to disallow multiple uses of the same authentication\n    > token? This restricts you to one login about every 30s, but it increases\n    > your chances to notice or even prevent man-in-the-middle attacks (y\u002Fn) Do you want to disallow multiple uses of the same authentication\n    > token? This restricts you to one login about every 30s, but it increases\n    > your chances to notice or even prevent man-in-the-middle attacks (y\u002Fn) y\n    > \n    > By default, tokens are good for 30 seconds. In order to compensate for\n    > possible time-skew between the client and the server, we allow an extra\n    > token before and after the current time. If you experience problems with\n    > poor time synchronization, you can increase the window from its default\n    > size of +-1min (window size of 3) to about +-4min (window size of\n    > 17 acceptable tokens).\n    > Do you want to do so? (y\u002Fn) y\n    > \n    > If the computer that you are logging into isn't hardened against brute-force\n    > login attempts, you can enable rate-limiting for the authentication module.\n    > By default, this limits attackers to no more than 3 login attempts every 30s.\n    > Do you want to enable rate-limiting (y\u002Fn) y\n    > ```\n\n    Notice this is **not run as root**.\n\n    Select default option (y in most cases) for all the questions it asks and remember to save the emergency scratch codes.\n\n1. Make a backup of PAM's SSH configuration file `\u002Fetc\u002Fpam.d\u002Fsshd`:\n\n    ``` bash\n    sudo cp --archive \u002Fetc\u002Fpam.d\u002Fsshd \u002Fetc\u002Fpam.d\u002Fsshd-COPY-$(date +\"%Y%m%d%H%M%S\")\n    ```\n\n1. Now we need to enable it as an authentication method for SSH by adding this line to `\u002Fetc\u002Fpam.d\u002Fsshd`:\n\n    ```\n    auth       required     pam_google_authenticator.so nullok\n    ```\n\n    **Note**: Check [here](https:\u002F\u002Fgithub.com\u002Fgoogle\u002Fgoogle-authenticator-libpam\u002Fblob\u002Fmaster\u002FREADME.md#nullok) for what `nullok` means.\n\n    [For the lazy](#editing-configuration-files---for-the-lazy):\n\n    ``` bash\n    echo -e \"\\nauth       required     pam_google_authenticator.so nullok         # added by $(whoami) on $(date +\"%Y-%m-%d @ %H:%M:%S\")\" | sudo tee -a \u002Fetc\u002Fpam.d\u002Fsshd\n    ```\n\n1. Tell SSH to leverage it by adding or editing this line in `\u002Fetc\u002Fssh\u002Fsshd_config`:\n\n    ```\n    ChallengeResponseAuthentication yes\n    ```\n\n    [For the lazy](#editing-configuration-files---for-the-lazy):\n\n    ``` bash\n    sudo sed -i -r -e \"s\u002F^(challengeresponseauthentication .*)$\u002F# \\1         # commented by $(whoami) on $(date +\"%Y-%m-%d @ %H:%M:%S\")\u002FI\" \u002Fetc\u002Fssh\u002Fsshd_config\n    echo -e \"\\nChallengeResponseAuthentication yes         # added by $(whoami) on $(date +\"%Y-%m-%d @ %H:%M:%S\")\" | sudo tee -a \u002Fetc\u002Fssh\u002Fsshd_config\n    ```\n\n1. Restart ssh:\n\n    ``` bash\n    sudo service sshd restart\n    ```\n\n([Table of Contents](#table-of-contents))\n\n## The Basics\n\n### Limit Who Can Use sudo\n\n#### Why\n\nsudo lets accounts run commands as other accounts, including **root**. We want to make sure that only the accounts we want can use sudo.\n\n#### Goals\n\n- sudo privileges limited to those who are in a group we specify\n\n#### Notes\n\n- Your installation may have already done this, or may already have a special group intended for this purpose so check first.\n  - Debian creates the sudo group. To view users that are part of this group (thus have sudo privileges):\n\t  \n\t  ```\n\t  cat \u002Fetc\u002Fgroup | grep \"sudo\"\n\t  ```\n  - RedHat creates the wheel group\n- See [https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server\u002Fissues\u002F39](https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server\u002Fissues\u002F39) for a note on some distributions making it so `sudo` does not require a password. Thanks to [sbrl](https:\u002F\u002Fgithub.com\u002Fsbrl) for sharing.\n\n#### Steps\n\n1. Create a group:\n\n    ``` bash\n    sudo groupadd sudousers\n    ```\n\n1. Add account(s) to the group:\n\n    ``` bash\n    sudo usermod -a -G sudousers user1\n    sudo usermod -a -G sudousers user2\n    sudo usermod -a -G sudousers  ...\n    ```\n\n    You'll need to do this for every account on your server that needs sudo privileges.\n\n1. Make a backup of the sudo's configuration file `\u002Fetc\u002Fsudoers`:\n\n    ``` bash\n    sudo cp --archive \u002Fetc\u002Fsudoers \u002Fetc\u002Fsudoers-COPY-$(date +\"%Y%m%d%H%M%S\")\n    ```\n\n1. Edit sudo's configuration file `\u002Fetc\u002Fsudoers`:\n\n    ``` bash\n    sudo visudo\n    ```\n\n1. Tell sudo to only allow users in the `sudousers` group to use sudo by adding this line if it is not already there:\n\n    ```\n    %sudousers   ALL=(ALL:ALL) ALL\n    ```\n\n([Table of Contents](#table-of-contents))\n\n### Limit Who Can Use su\n\n#### Why\n\nsu also lets accounts run commands as other accounts, including **root**. We want to make sure that only the accounts we want can use su.\n\n#### Goals\n\n- su privileges limited to those who are in a group we specify\n\n#### References\n\n- Thanks to [olavim](https:\u002F\u002Fgithub.com\u002Folavim) for sharing [this idea](https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server\u002Fissues\u002F41)\n\n#### Steps\n\n1. Create a group:\n\n    ``` bash\n    sudo groupadd suusers\n    ```\n\n1. Add account(s) to the group:\n\n    ``` bash\n    sudo usermod -a -G suusers user1\n    sudo usermod -a -G suusers user2\n    sudo usermod -a -G suusers  ...\n    ```\n\n    You'll need to do this for every account on your server that needs sudo privileges.\n\n1. Make it so only users in this group can execute `\u002Fbin\u002Fsu`:\n\n    ``` bash\n    sudo dpkg-statoverride --update --add root suusers 4750 \u002Fbin\u002Fsu\n    ```\n\n([Table of Contents](#table-of-contents))\n\n### Run applications in a sandbox with FireJail\n\n#### Why\n\nIt's absolutely better, for many applications, to run in a sandbox.\n\nBrowsers (even more the Closed Source ones) and eMail Clients are highly suggested.\n\n#### Goals\n\n- confine applications in a jail (few safe directories) and block access to the rest of the system\n\n#### References\n\n- Thanks to [FireJail](https:\u002F\u002Ffirejail.wordpress.com\u002F)\n\n#### Steps\n\n1. Install the software:\n\n    ``` bash\n    sudo apt install firejail firejail-profiles\n    ```\n    \n    Note: for Debian 10 Stable, official Backport is suggested:\n\n    ``` bash\n    sudo apt install -t buster-backports firejail firejail-profiles\n    ```\n\n2. Allow an application (installed in `\u002Fusr\u002Fbin` or `\u002Fbin`) to run only in a sandbox (see few examples below here):\n\n    ``` bash\n    sudo ln -s \u002Fusr\u002Fbin\u002Ffirejail \u002Fusr\u002Flocal\u002Fbin\u002Fgoogle-chrome-stable\n    sudo ln -s \u002Fusr\u002Fbin\u002Ffirejail \u002Fusr\u002Flocal\u002Fbin\u002Ffirefox\n    sudo ln -s \u002Fusr\u002Fbin\u002Ffirejail \u002Fusr\u002Flocal\u002Fbin\u002Fchromium\n    sudo ln -s \u002Fusr\u002Fbin\u002Ffirejail \u002Fusr\u002Flocal\u002Fbin\u002Fevolution\n    sudo ln -s \u002Fusr\u002Fbin\u002Ffirejail \u002Fusr\u002Flocal\u002Fbin\u002Fthunderbird\n    ```\n\n3. Run the application as usual (via terminal or launcher) and check if it's running in a jail:\n\n    ``` bash\n    firejail --list\n    ```\n\n4. Allow a sandboxed app to run again as it was before (example: firefox)\n\n    ``` bash\n    sudo rm \u002Fusr\u002Flocal\u002Fbin\u002Ffirefox\n    ```\n\n([Table of Contents](#table-of-contents))\n\n### NTP Client\n\n#### Why\n\nMany security protocols leverage the time. If your system time is incorrect, it could have negative impacts to your server. An NTP client can solve that problem by keeping your system time in-sync with [global NTP servers](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FNetwork_Time_Protocol)\n\n#### How It Works\n\nNTP stands for Network Time Protocol. In the context of this guide, an NTP client on the server is used to update the server time with the official time pulled from official servers. Check https:\u002F\u002Fwww.pool.ntp.org\u002Fen\u002F for all of the public NTP servers.\n> **Note:** Starting with **Debian 13 (Trixie)**, the classic `ntp` package has been removed. Running `sudo apt install ntp` will fail with *\"Package ntp has no installation candidate\"*. Since this guide only uses NTP as a **client** (to sync the server's clock), the recommended approach on Debian 13+ is to use `systemd-timesyncd`, which is already pre-installed and requires no additional packages. See the [Debian 13+ steps](#debian-13-trixie-and-later-systemd-timesyncd) below.\n\n#### Goals\n\n- NTP client installed and keeping server time in-sync\n\n#### References\n\n- https:\u002F\u002Fcloudpro.zone\u002Findex.php\u002F2018\u002F01\u002F27\u002Fdebian-9-3-server-setup-guide-part-4\u002F\n- https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FNetwork_Time_Protocol\n- https:\u002F\u002Fwww.pool.ntp.org\u002Fen\u002F\n- https:\u002F\u002Fserverfault.com\u002Fquestions\u002F957302\u002Fsecuring-hardening-ntp-client-on-linux-servers-config-file\u002F957450#957450\n- https:\u002F\u002Ftf.nist.gov\u002Ftf-cgi\u002Fservers.cgi\n\n#### Steps\n\n##### Debian 13 (Trixie) and later: systemd-timesyncd\n\n`systemd-timesyncd` is a lightweight SNTP client that is already included in Debian. Unlike the full `ntpd` daemon, it does not listen on any port, which makes it a smaller attack surface. For the purposes of this guide - keeping your server's clock in sync - it is all you need.\n\n1. Enable NTP synchronization:\n\n    ``` bash\n    sudo timedatectl set-ntp true\n    ```\n\n1. Verify it is working:\n\n    ``` bash\n    timedatectl status\n    ```\n\n    You should see `NTP service: active` and `System clock synchronized: yes` in the output.\n\n1. Configure trusted NTP servers. Make a backup of the configuration file and then edit it:\n\n    ``` bash\n    sudo cp --archive \u002Fetc\u002Fsystemd\u002Ftimesyncd.conf \u002Fetc\u002Fsystemd\u002Ftimesyncd.conf-COPY-$(date +\"%Y%m%d%H%M%S\")\n    ```\n\n    Edit `\u002Fetc\u002Fsystemd\u002Ftimesyncd.conf` and uncomment\u002Fset the `[Time]` section:\n\n    ```\n    [Time]\n    NTP=pool.ntp.org\n    FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org\n    ```\n\n    [For the lazy](#editing-configuration-files---for-the-lazy):\n\n    ``` bash\n    sudo sed -i -r -e \"s\u002F^#?NTP=.*$\u002FNTP=pool.ntp.org         # added by $(whoami) on $(date +\"%Y-%m-%d @ %H:%M:%S\")\u002F\" \u002Fetc\u002Fsystemd\u002Ftimesyncd.conf\n    sudo sed -i -r -e \"s\u002F^#?FallbackNTP=.*$\u002FFallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org         # added by $(whoami) on $(date +\"%Y-%m-%d @ %H:%M:%S\")\u002F\" \u002Fetc\u002Fsystemd\u002Ftimesyncd.conf\n    ```\n\n1. Restart the service to apply the changes:\n\n    ``` bash\n    sudo systemctl restart systemd-timesyncd\n    ```\n\n1. Check the synchronization status:\n\n    ``` bash\n    timedatectl timesync-status\n    ```\n\n    > ```\n    >        Server: 108.61.56.35 (pool.ntp.org)\n    > Poll interval: 32s (min: 32s; max: 34min 8s)\n    >          Leap: normal\n    >       Version: 4\n    >       Stratum: 2\n    >     Reference: C342F10A\n    >     Precision: 1us (2^0)\n    >  Root distance: 24.054ms (max: 5s)\n    >        Offset: +2.156ms\n    >         Delay: 48.567ms\n    >        Jitter: 1.452ms\n    >  Packet count: 3\n    > ```\n\n##### Debian 12 (Bookworm) and earlier: ntp package\n\n> **Note:** These steps apply to **Debian 12 and earlier** only. On Debian 13+, the `ntp` package is no longer available -- use the [systemd-timesyncd steps](#debian-13-trixie-and-later-systemd-timesyncd) above instead.\n\n1. Install ntp.\n\n    On Debian based systems:\n\n    ``` bash\n    sudo apt install ntp\n    ```\n    \n1. Make a backup of the NTP client's configuration file `\u002Fetc\u002Fntp.conf`:\n\n    ``` bash\n    sudo cp --archive \u002Fetc\u002Fntp.conf \u002Fetc\u002Fntp.conf-COPY-$(date +\"%Y%m%d%H%M%S\")\n    ```\n\n1. The default configuration, at least on Debian, is already pretty secure. The only thing we'll want to make sure is we're the `pool` directive and not any `server` directives. The `pool` directive allows the NTP client to stop using a server if it is unresponsive or serving bad time. Do this by commenting out all `server` directives and adding the below to `\u002Fetc\u002Fntp.conf`.\n\t\n    ```\n    pool pool.ntp.org iburst\n    ```\n    \n    [For the lazy](#editing-configuration-files---for-the-lazy):\n    \n    ``` bash\n    sudo sed -i -r -e \"s\u002F^((server|pool).*)\u002F# \\1         # commented by $(whoami) on $(date +\"%Y-%m-%d @ %H:%M:%S\")\u002F\" \u002Fetc\u002Fntp.conf\n    echo -e \"\\npool pool.ntp.org iburst         # added by $(whoami) on $(date +\"%Y-%m-%d @ %H:%M:%S\")\" | sudo tee -a \u002Fetc\u002Fntp.conf\n    ```\n\n    **Example `\u002Fetc\u002Fntp.conf`**:\n    \n    > ```\n    > driftfile \u002Fvar\u002Flib\u002Fntp\u002Fntp.drift\n    > statistics loopstats peerstats clockstats\n    > filegen loopstats file loopstats type day enable\n    > filegen peerstats file peerstats type day enable\n    > filegen clockstats file clockstats type day enable\n    > restrict -4 default kod notrap nomodify nopeer noquery limited\n    > restrict -6 default kod notrap nomodify nopeer noquery limited\n    > restrict 127.0.0.1\n    > restrict ::1\n    > restrict source notrap nomodify noquery\n    > pool pool.ntp.org iburst         # added by user on 2019-03-09 @ 10:23:35\n    > ```\n    \n1. Restart ntp:\n\n    ``` bash\n    sudo service ntp restart\n    ```\n\n1. Check the status of the ntp service:\n\n    ``` bash\n    sudo systemctl status ntp\n    ```\n\n    > ```\n    > ● ntp.service - LSB: Start NTP daemon\n    >    Loaded: loaded (\u002Fetc\u002Finit.d\u002Fntp; generated; vendor preset: enabled)\n    >    Active: active (running) since Sat 2019-03-09 15:19:46 EST; 4s ago\n    >      Docs: man:systemd-sysv-generator(8)\n    >   Process: 1016 ExecStop=\u002Fetc\u002Finit.d\u002Fntp stop (code=exited, status=0\u002FSUCCESS)\n    >   Process: 1028 ExecStart=\u002Fetc\u002Finit.d\u002Fntp start (code=exited, status=0\u002FSUCCESS)\n    >     Tasks: 2 (limit: 4915)\n    >    CGroup: \u002Fsystem.slice\u002Fntp.service\n    >            └─1038 \u002Fusr\u002Fsbin\u002Fntpd -p \u002Fvar\u002Frun\u002Fntpd.pid -g -u 108:113\n    > \n    > Mar 09 15:19:46 host ntpd[1038]: Listen and drop on 0 v6wildcard [::]:123\n    > Mar 09 15:19:46 host ntpd[1038]: Listen and drop on 1 v4wildcard 0.0.0.0:123\n    > Mar 09 15:19:46 host ntpd[1038]: Listen normally on 2 lo 127.0.0.1:123\n    > Mar 09 15:19:46 host ntpd[1038]: Listen normally on 3 enp0s3 10.10.20.96:123\n    > Mar 09 15:19:46 host ntpd[1038]: Listen normally on 4 lo [::1]:123\n    > Mar 09 15:19:46 host ntpd[1038]: Listen normally on 5 enp0s3 [fe80::a00:27ff:feb6:ed8e%2]:123\n    > Mar 09 15:19:46 host ntpd[1038]: Listening on routing socket on fd #22 for interface updates\n    > Mar 09 15:19:47 host ntpd[1038]: Soliciting pool server 108.61.56.35\n    > Mar 09 15:19:48 host ntpd[1038]: Soliciting pool server 69.89.207.199\n    > Mar 09 15:19:49 host ntpd[1038]: Soliciting pool server 45.79.111.114\n    > ```\n\n1. Check ntp's status:\n\n    ``` bash\n    sudo ntpq -p\n    ```\n\n    > ```\n    >      remote           refid      st t when poll reach   delay   offset  jitter\n    > ==============================================================================\n    >  pool.ntp.org    .POOL.          16 p    -   64    0    0.000    0.000   0.000\n    > *lithium.constan 198.30.92.2      2 u    -   64    1   19.900    4.894   3.951\n    >  ntp2.wiktel.com 212.215.1.157    2 u    2   64    1   48.061   -0.431   0.104\n    > ```\n\n([Table of Contents](#table-of-contents))\n\n### Securing \u002Fproc\n\n#### Why\n\nTo quote https:\u002F\u002Flinux-audit.com\u002Flinux-system-hardening-adding-hidepid-to-proc\u002F:\n\n> When looking in `\u002Fproc` you will discover a lot of files and directories. Many of them are just numbers, which represent the information about a particular process ID (PID). By default, Linux systems are deployed to allow all local users to see this all information. This includes process information from other users. This could include sensitive details that you may not want to share with other users. By applying some filesystem configuration tweaks, we can change this behavior and improve the security of the system.\n\n**Note**: This may break on some `systemd` systems. Please see [https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server\u002Fissues\u002F37](https:\u002F\u002Fgithub.com\u002Fimthenachoman\u002FHow-To-Secure-A-Linux-Server\u002Fissues\u002F37) for more information. Thanks to [nlgranger](https:\u002F\u002Fgithub.com\u002Fnlgranger) for sharing.\n\n#### Goals\n\n- `\u002Fproc` mounted with `hidepid=2` so users can only see information about their processes\n\n#### References\n\n- https:\u002F\u002Flinux-audit.com\u002Flinux-system-hardening-adding-hidepid-to-proc\u002F\n- https:\u002F\u002Flikegeeks.com\u002Fsecure-linux-server-hardening-best-practices\u002F#Hardening-proc-Directory\n- https:\u002F\u002Fwww.cyberciti.biz\u002Ffaq\u002Flinux-hide-processes-from-other-users\u002F\n\n#### Steps\n\n1. Make a backup of `\u002Fetc\u002Ffstab`:\n\n    ``` bash\n    sudo cp --archive \u002Fetc\u002Ffstab \u002Fetc\u002Ffstab-COPY-$(date +\"%Y%m%d%H%M%S\")\n    ```\n\n1. Add this line to `\u002Fetc\u002Ffstab` to have `\u002Fproc` mounted with `hidepid=2`:\n\n    ```\n    proc     \u002Fproc     proc     defaults,hidepid=2     0     0\n    ```\n    \n    [For the lazy](#editing-configuration-files---for-the-lazy):\n    \n    ``` bash\n    echo -e \"\\nproc     \u002Fproc     proc     defaults,hidepid=2     0     0         # added by $(whoami) on $(date +\"%Y-%m-%d @ %H:%M:%S\")\" | sudo tee -a \u002Fetc\u002Ffstab\n    ```\n\n1. Reboot the system:\n\n    ``` bash\n    sudo reboot now\n    ```\n    \n    **Note**: Alternatively, you can remount `\u002Fproc` without rebooting with `sudo mount -o remount,hidepid=2 \u002Fproc`\n\n([Table of Contents](#table-of-contents))\n\n### Force Accounts To Use Secure Passwords\n\n#### Why\n\nBy default, accounts can use any password they want, including bad ones. [pwquality](https:\u002F\u002Flinux.die.net\u002Fman\u002F5\u002Fpwquality.conf)\u002F[pam_pwquality](https:\u002F\u002Flinux.die.net\u002Fman\u002F8\u002Fpam_pwquality) addresses this security gap by providing \"a way to configure the default password quality requirements for the system passwords\" and checking \"its strength against a system dictionary and a set of rules for identifying poor choices.\"\n\n#### How It Works\n\nOn Linux, PAM is responsible for authentication. There are four tasks to PAM that you can read about at https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FLinux_PAM. This section talks about the password task.\n\nWhen there is a need to set or change an account password, the password task of PAM handles the request. In this section we will tell PAM's password task to pass the requested new password to libpam-pwquality to make sure it meets our requirements. If the requirements are met it is used\u002Fset; if it does not meet the requirements it errors and lets the user know.\n\n#### Goals\n\n- enforced strong passwords\n\n#### Steps\n\n1. Install libpam-pwquality.\n\n    On Debian based systems:\n\n    ``` bash\n    sudo apt install libpam-pwquality\n    ```\n\n1. Make a backup of PAM's password configuration file `\u002Fetc\u002Fpam.d\u002Fcommon-password`:\n\n    ``` bash\n    sudo cp --archive \u002Fetc\u002Fpam.d\u002Fcommon-password \u002Fetc\u002Fpam.d\u002Fcommon-password-COPY-$(date +\"%Y%m%d%H%M%S\")\n    ```\n\n1. Tell PAM to use libpam-pwquality to enforce strong passwords by editing the file `\u002Fetc\u002Fpam.d\u002Fcommon-password` and change the line that starts like this:\n\n    ```\n    password        requisite                       pam_pwquality.so\n    ```\n\n    to this:\n\n    ```\n    password        requisite                       pam_pwquality.so retry=3 minlen=10 difok=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1 maxrepeat=3 gecoschec\n    ```\n\n   The above options are:\n\n     - `retry=3` = prompt user 3 times before returning with error.\n     - `minlen=10` = the minimum length of the password, factoring in any credits (or debits) from these:\n       - `dcredit=-1` = must have at least **one digit**\n       - `ucredit=-1` = must have at least **one upper case letter**\n       - `lcredit=-1` = must have at least **one lower case letter**\n       - `ocredit=-1` = must have at least **one non-alphanumeric character**\n     - `difok=3` = at least 3 characters from the new password cannot have been in the old password\n     - `maxrepeat=3` = allow a maximum of 3 repeated characters\n     - `gecoschec` = do not allow passwords with the account's name\n\n\n    [For the lazy](#editing-configuration-files---for-the-lazy):\n    \n    ``` bash\n    sudo sed -i -r -e \"s\u002F^(password\\s+requisite\\s+pam_pwquality.so)(.*)$\u002F# \\1\\2         # commented by $(whoami) on $(date +\"%Y-%m-%d @ %H:%M:%S\")\\n\\1 retry=3 minlen=10 difok=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1 maxrepeat=3 gecoschec         # added by $(whoami) on $(date +\"%Y-%m-%d @ %H:%M:%S\")\u002F\" \u002Fetc\u002Fpam.d\u002Fcommon-password\n    ```\n\n([Table of Contents](#table-of-contents))\n\n### Automatic Security Updates and Alerts\n\n#### Why\n\nIt is important to keep a server updated with the latest **critical security patches and updates**. Otherwise you're at risk of known security vulnerabilities that bad-actors could use to gain unauthorized access to your server.\n\nUnless you plan on checking your server every day, you'll want a way to automatically update the system and\u002For get emails about available updates.\n\nYou don't want to do all updates because with every update there is a risk of something breaking. It is important to do the critical updates but everything else can wait until you have time to do it manually.\n\n#### Why Not\n\nAutomatic and unattended updates may break your system and you may not be near your server to fix it. This would be especially problematic if it broke your SSH access.\n\n#### Notes\n\n- Each distribution manages packages and updates differently. So far I only have steps for Debian based systems.\n- Your server will need a way to send e-mails for this to work\n\n#### Goals\n\n- Automatic, unattended, updates of critical security patches\n- Automatic emails of remaining pending updates\n\n#### Debian Based Systems\n\n##### How It Works\n\nOn Debian based systems you can use:\n\n- unattended-upgrades to automatically do system updates you want (i.e. critical security updates)\n- apt-listchanges to get details about package changes before they are installed\u002Fupgraded\n- apticron to get emails for pending package updates\n\nWe will use unattended-upgrades to apply **critical security patches**. We can also apply stable updates since they've already been thoroughly tested by the Debian community.\n\n##### References\n\n- https:\u002F\u002Fwiki.debian.org\u002FUnattendedUpgrades\n- https:\u002F\u002Fdebian-handbook.info\u002Fbrowse\u002Fstable\u002Fsect.regular-upgrades.html\n- https:\u002F\u002Fblog.sleeplessbeastie.eu\u002F2015\u002F01\u002F02\u002Fhow-to-perform-unattended-upgrades\u002F\n- https:\u002F\u002Fwww.vultr.com\u002Fdocs\u002Fhow-to-set-up-unattended-upgrades-on-debian-9-stretch\n- https:\u002F\u002Fgithub.com\u002Fmvo5\u002Funattended-upgrades\n- https:\u002F\u002Fwiki.debian.org\u002FUnattendedUpgrades#apt-listchanges\n- https:\u002F\u002Fwww.cyberciti.biz\u002Ffaq\u002Fapt-get-apticron-send-email-upgrades-available\u002F\n- https:\u002F\u002Fwww.unixmen.com\u002Fhow-to-get-email-notifications-for-new-updates-on-debianubuntu\u002F\n- `\u002Fetc\u002Fapt\u002Fapt.conf.d\u002F50unattended-upgrades`\n\n##### Steps\n\n1. Install unattended-upgrades, apt-listchanges, and apticron:\n\n    ``` bash\n    sudo apt install unattended-upgrades apt-listchanges apticron\n    ```\n\n1. Now we need to configure unattended-upgrades to automatically apply the updates. This is typically done by editing the files `\u002Fetc\u002Fapt\u002Fapt.conf.d\u002F20auto-upgrades` and `\u002Fetc\u002Fapt\u002Fapt.conf.d\u002F50unattended-upgrades` that were created by the packages. However, because these file may get overwritten with a future update, we'll create a new file instead. Create the file `\u002Fetc\u002Fapt\u002Fapt.conf.d\u002F51myunattended-upgrades` and add this:\n\n    ``","这个项目是一个逐步完善的Linux服务器安全加固指南。它详细介绍了如何通过配置SSH、防火墙（如UFW）、入侵检测系统（如PSAD和Fail2Ban）等手段来增强服务器的安全性，同时提供了一些关于安全基础概念的教育内容。此外，该指南还涉及了使用Ansible Playbook自动化安全设置的方法。适用于需要提高Linux服务器安全性、防止未授权访问及恶意攻击的各种场景，无论是个人用户还是企业级应用都能从中受益。",2,"2026-06-11 03:38:04","high_star"]