As a developer, having robust and secure remote access to your CentOS server is crucial for managing your infrastructure and deployments. SSH (Secure Shell) is the gold standard for encrypted network communication protocols that ticks all the boxes for functionality, security, and convenience.
In this comprehensive guide, we will dive deep on exactly how to enable SSH on CentOS 8 while exploring all aspects of SSH along the way. Both novice sysadmins and experienced developers alike may discover new tips and techniques for taking advantage of SSH‘s extensive capabilities.
An Overview of SSH and Its Advantages
SSH stands for "Secure SHell", developed as a secure replacement for earlier remote access tools like Telnet or rlogin using proven, modern encryption. Some key advantages of SSH include:
Encryption – All SSH traffic is encrypted end-to-end using industry standard protocols like AES, protecting sessions against man-in-the-middle attacks.
Access Control – SSH utilizes public-key cryptography for stringent authentication, only granting access to users with the right credentials.
Auditing – Detailed connection and authentication logs provide visibility into all SSH activity for security auditing and oversight.
Port Forwarding – SSH connections can tunnel arbitrary ports over their encrypted tunnel for accessing remote network services.
Automation – Commands, shell scripts, file transfers can be executed seamlessly over SSH.
With powerful security, versatile functionality like port forwarding, and ubiquitous availability on virtually all systems – it‘s no wonder SSH has become the tool of choice for remote sysadmin tasks.
Step 1 – Install Required SSH Server Packages
CentOS 8 includes comprehensive package repositories containing all the required components we need for enabling SSH access to our server…
[Abridged package installation commands]
This installs the OpenSSH server daemon sshd along with supporting utilities like ssh-keygen used for managing authentication credentials.
RHEL based systems have a modular architecture that allows swapping out OpenSSH for alternate implementations if desired – but the openssh-server package provides the standard, battle-tested OpenSSH 7.4 release.
Step 2 – Open Firewall Ports for SSH
With our packages ready, we need to configure CentOS‘s firewalld daemon to allow inbound SSH connections through…
[Abridged firewall-cmd rules]
By default firewalld will block all incoming connections to ports – including SSH‘s standard port 22. Explicitly opening port 22 only permits protocol traffic necessary for SSH, while keeping all other ports protected.
Later on, we will explore replacing the default port with a non-standard alternative as an extra security measure.
Step 3 – Start the SSH Service
Now we are finally ready to unleash the power of sshd! The systemctl utility manages starting services…
[Abridged systemctl start/enable commands]
The sshd service should now be active and running on the server, listening for client connection attempts.
systemctl also allows fine-grained control over services, like stopping or restarting them – handy for applying configuration tweaks to sshd later on.
With the basics covered, let‘s dive into securely configuring our new SSH server.
Step 4 – Configure SSH Daemon Options
The default OpenSSH server ruleset supplied is reasonably secure, but hardening configurations is always a best practice. The main sshd configuration reside at /etc/ssh/sshd_config.
Common changes include:
Disallow Root Login – Direct root access poses security risks:
PermitRootLogin no
Use SSH Keys Instead of Passwords – More on this later, but prevent password-based auth:
PasswordAuthentication no
Non-Standard Port – Change from default port 22 for obscurity:
Port 22222
There are many more options for things like connection timeouts, banning users, limiting commands and more – see man sshd_config.
After making changes, restart sshd to apply them:
systemctl restart sshd
We will implement some of these later on. But first, let‘s explore SSH authentication mechanisms for granting access.
Overview of SSH Authentication Methods
Proper access controls are critical for any secure remote access system. SSH provides multiple authentication techniques, including:
Password Auth – Simple but insecure passwords vulnerable to brute force. Still useful in concert with other factors.
PublicKey Auth – Utilize asymmetric cryptography key pairs for robust security.
Hostbased Auth – Trust based on client host identity, useful for trusted internal hosts.
2FA – Leverage one-time-passwords from apps/tokens for dual factor auth.
We will focus on public key authentication which strikes a nice balance of security and usability…
Step 5 – Configure SSH Public Key Authentication
Public key authentication uses SSH key pairs containing linked public and private keys for proving identity. By installing the public key on our CentOS server, the owner of the matching private key is authenticated via cryptographic challenge.
First, generate the keypair on your client:
ssh-keygen -t rsa -b 4096
The -t rsa specifies the RSA algorithm while -b 4096 sets an robust 4096 bit key size.
You will be prompted for a location and passphrase to further protect the private key. Now add the public key to CentOS‘s authorized keys:
ssh-copy-id -i ~/.ssh/id_rsa user@centoshost
This installs your public key to ~/.ssh/authorized_keys.
You can now login password-free with your private key identity!
For added security, disable password auth in sshd_config:
PasswordAuthentication no
With keypairs, you can also take advantage of "agent forwarding" to proxy logins between servers.
Now that we have locked down access, let‘s look at some advanced OpenSSH capabilities…
Using SSH Port Forwarding Tunnels
SSH can tunnel arbitrary TCP ports over its encrypted connection, granting secure passage straight through firewalls. This enables exposing services hidden behind your CentOS server to public access.
Common port forwarding techniques include:
Local Forwarding – Tunnel local ports to remote resources:
ssh -L 9000:db.internal:5432 server
Here local port 9000 connects to a private DB server at port 5432 on the remote end.
Remote Forwarding – Expose remote ports locally:
ssh -R 8000:localhost:80 server
Forwards connections to local port 8000 over SSH to remote port 80.
Dynamic Forwarding – SOCKS proxy for all communications over SSH transport:
ssh -D 1080 server
Port forwarding is immensely powerful for accessing remote LAN resources or securing web traffic – especially when chained across multiple servers.
Monitoring and Auditing SSH Connections
Maintaining visibility into SSH activity is critical for both security and operational support. Thankfully OpenSSH offers rich logging and auditing capabilities:
Log Active Connections – Direct sshd to record all active socket connections alongside session information with the LogLevel directive:
LogLevel VERBOSE
Audit Logins – sshd logs all authentication attempts to /var/log/secure for review:
grep sshd /var/log/secure
Failed logins clearly indicate brute force attacks.
Session Logging -Utilize the syslog facility to record terminal session transcripts and commands entered over SSH:
SyslogFacility AUTHPRIV
Reviewing audit logs for anomalous activity is key for identifying threats. Pairing SSH with central log analysis systems like the ELK stack unlocks further analytical capabilities.
For detailed real-time insights, monitor active SSH sockets with ss:
ss state connected sport = :ssh
This reveals live connections for identifying suspicious activity.
Distributing and Managing SSH Keys
The power of SSH keys lies in their flexibility for distributed authentication. Some strategies for streamlining SSH trust include:
Configuration Management – Tools like Ansible can seamlessly deploy authorized_keys across fleets of servers.
SSH Certificates – Certificates allow centralized signing of key-based trust by private certificate authorities.
Hardware Tokens – Dedicated hardware keys provide 2-factor authentication without passwords.
Following best practices for issuing limited lifetime certificates or requiring secondary factors where appropriate ensures keys remain a robust authentication control.
Additional SSH Hardening Techniques
Given SSH‘s ubiquitous presence on most Linux systems, hardening its encryption and configurations is particularly impactful for improving security posture across the board:
- Favor stronger ciphers like AES-256 over legacy algorithms
- Reduce connection timeout windows to limit brute force durations
- Run sshd on a non-privileged port not shared by a less secure service
- Limit users to subset of approved commands with restrictive shells
- Consider 2-factor requirements via time-based OTP setups
- Whitelist specific client IP ranges permitted to connect
Hardening server networks begins with eliminating any unnecessary remote access attack surface – so prune protocols like Telnet/RDP and propagate SSH as appropriate.
Debugging SSH Connectivity Issues
Of course even the best laid SSH implementations can run into issues. Some troubleshooting techniques for diagnosing connection problems include:
- Use
-vvfor verbose ssh client output detailing failures - Check firewall rules blocking port 22 with
iptables - Validate sshd listening on :22 using
ssornetstat - Tail
/var/log/secureandjournalctlfor failure logs in real-time - Enumerate host key fingerprint mismatches
- Attempt authentication for non key-related causes
- Rule out client blacklisting in
hosts.deny
Drilling down on where precisely a connection attempt fails guides resolution – whether due to firewalls, privileges, crypto issues or more.
How Does SSH Compare to Alternatives?
Other solutions exist for remotely accessing Linux servers beyond SSH, including:
Remote Desktop – GUI solutions like VNC or RDP allow graphical remote logins rather than just terminal. However they consume more resources, have weaker security models, and limited automation capabilities compared to SSH.
HTTP Tunneling – Alternatives like web SSH tunnel SSH itself inside HTTP headers to bypass firewalling. This comes at a performance tradeoff with additional protocol overhead no longer being native SSH.
Direct Console Access – Physical console access via keyboard/screen naturally has no remote attack surface. But fails to provide the automation benefits, port forwarding or accessibility of SSH connections.
At the end of the day, SSH strikes the ideal balance of security, performance and functionality – cementing its place as the tool of choice for remote sysadmin tasks.
Conclusion
That covers a comprehensive deep dive into all aspects of enabling and taking advantage of secure shell on CentOS 8 servers. We explored OpenSSH installation and configuration, managing authentication, tunneling tricks, logging, and more.
Whether you manage a single VPS or an enterprise Linux infrastructure, unlocking robust access control and encrypted channels for administration via SSH is a must. Properly wielded, its versatility and ubiquitous nature position SSH as a cornerstone for remote systems management.
Hopefully these tips and techniques for hardening SSH have provided ideas to further lock down and empower your CentOS admin game. Secure and accessible command line interfaces represent the heart of Linux management – so leverage SSH to keep that heart beating!


