Secure your OpenSSH Server
Alright, here's how you can secure your OpenSSH server using the sshd_config file:
Open the SSH configuration file using a text editor like vim or nano: sudo vim /etc/ssh/sshd_config
- Find the line that says PermitRootLogin yes and change it to PermitRootLogin no. This will prevent root from logging in directly to your server.
- Find the line that says PasswordAuthentication yes and change it to PasswordAuthentication no. This will disable password-based authentication.
- Add a line that says UsePAM no. This will disable PAM (Pluggable Authentication Modules), which allows users to authenticate using various methods, including passwords.
- Find the line that says GatewayPorts yes and change it to GatewayPorts no. This will prevent incoming connections from being forwarded through your SSH server.
- Add a line that says X11Forwarding no. This will disable X11 forwarding, which can be used by attackers to gain access to your X11 session.
- Add a line that says AllowAgentForwarding no. This will prevent attackers from using your SSH client to authenticate to other servers.
- Add a line that says AllowTcpForwarding no. This will prevent attackers from using your SSH client to forward TCP connections.
- Find the line that says Protocol 2 and add a line that says Port 22222. This will change the default SSH port from 22 to 22222, making it harder for attackers to find your SSH server.
- Find the line that says PubkeyAuthentication yes and make sure it's not commented out. This will enable public key authentication, which is more secure than password authentication.
- Add a line that says UseDNS no. This will prevent attackers from using DNS to bypass access restrictions.
- Add a line that says MaxAuthTries 3. This will limit the number of failed login attempts before the SSH server blocks the attacker's IP address.
- Find the line that says PermitEmptyPasswords no and make sure it's not commented out. This will prevent users with empty passwords from logging in.
- Find the line that says ChallengeResponseAuthentication no and make sure it's not commented out. This will prevent attackers from using challenge-response authentication to guess user passwords.
- Add a line that says IgnoreRhosts yes. This will prevent attackers from using the rhosts authentication mechanism to gain access to your server.
- Add a line that says ClientAliveInterval 60 and another line that says ClientAliveCountMax 3. This will disconnect idle SSH sessions after 60 seconds, preventing attackers from hijacking inactive sessions.
- Find the line that says LogLevel INFO and change it to LogLevel VERBOSE. This will increase the verbosity of the SSH logs, making it easier to detect and track suspicious activity.
- Add a line that says LogLevel AUTHPRIV.
- Find the line that says SyslogFacility AUTH. Change it to SyslogFacility AUTHPRIV. This will log authentication information to a separate, secure log file, making it harder for attackers to tamper with your logs.
- Find the line that says MaxSessions 10 and reduce the number to MaxSessions 2. This will limit the number of active sessions on your server, making it harder for attackers to launch brute-force attacks.
- Add a line that says UsePrivilegeSeparation yes. This will separate the SSH daemon process into two separate processes, one that runs with root privileges and one that runs with user privileges. This makes it harder for attackers to gain root access if they manage to compromise the user-privileged process.
- Find the line that says PubkeyAuthentication yes. Change it to PubkeyAuthentication without-passphrase. This will require users to use public keys without passphrases, making it harder for attackers to guess the passphrase if they manage to steal a user's private key.
- Add a line that says TCPKeepAlive yes. This will make the SSH server send TCP keep-alive packets to keep the connection open, preventing attackers from hijacking idle sessions.
- Add a line that says Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr.
- Add a line that says MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,umac-128@openssh.com,hmac-sha1-etm@openssh.com,hmac-sha1.
- Add a line that says KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group1-sha1.
- This setting specifies the key exchange algorithms that SSH will use to negotiate encryption keys between the client and server.
- Add a line that says ServerKeyBits 3072. This setting specifies the length of the Diffie-Hellman key used for key exchange. A longer key length means stronger security, but also slower performance.
- Add a line that says KexAlgorithms none. This setting disables key exchange completely. It is a security measure that should only be used if you have a specific reason to disable key exchange.
- Add a line that says GSSAPIAuthentication no. This setting disables GSSAPI authentication, which uses Kerberos for authentication. GSSAPI is not widely used, so it's safe to disable it for most users
Comments
Post a Comment