Support >
  About cybersecurity >
  SSH Logging, Auditing, and Troubleshooting: A Practical Guide from Investigating Anomalies to Security Hardening
SSH Logging, Auditing, and Troubleshooting: A Practical Guide from Investigating Anomalies to Security Hardening
Time : 2026-11-23 13:53:38
Edit : Jtti

SSH(Secure Shell)serves as the primary channel for connecting to and managing Linux servers—and,consequently,it is a prime target for brute-force attacks and malicious intrusions.For system administrators,SSH logs are not merely tools for troubleshooting issues like login failures or dropped connections;they act as a"black box"essential for detecting security incidents and auditing user activity.

When faced with SSH connection issues,many beginners resort to blindly changing passwords or rebooting the server,overlooking the fact that the root cause is often already documented in the logs.This article provides a systematic overview of SSH logging mechanisms,security auditing methods,and practical troubleshooting techniques,helping you fully master this"gateway"to your server.

Where are SSH logs stored,and how can you pinpoint them?

Depending on the Linux distribution and version,the storage location for SSH logs varies:

-Debian/Ubuntu-based systems:Authentication logs(both successful and failed attempts)are typically stored in`/var/log/auth.log`.

-CentOS/RHEL/Rocky Linux-based systems:These logs are found in`/var/log/secure`.

-Modern systems using Systemd:You can view logs specific to the SSH service in real-time using`journalctl-u sshd`(or`journalctl-u ssh`).

Common commands for viewing logs:

#View the latest login activity in real-time

tail-f/var/log/auth.log

#Filter for all failed login attempts

grep"Failed password"/var/log/auth.log

#View the last 100 log entries using journalctl

journalctl-u sshd-n 100--no-pager

Auditing:Uncovering"hidden threats"in your logs

Logs are just useless data if they aren't analyzed and acted upon.When auditing SSH,focus on the following three areas:

1.Signs of brute-force attacks("Failed password"/"Invalid user")

If you see a large number of consecutive entries in`auth.log`such as"Failed password for root from 1.2.3.4"or"Invalid user admin from 1.2.3.4,"it indicates that your server is being subjected to a targeted brute-force or dictionary attack.If an unfamiliar IP address makes hundreds or thousands of attempts within a short period,do not hesitate—block that IP immediately using your firewall.

2.Successful Login Traces(`Accepted password`/`Accepted publickey`)

Search for the keyword`Accepted`to view all successful login records.Key points for auditing:Is the login time unusual(e.g.,3:00 AM)?Is the source IP familiar?(For instance,if an unknown IP from within your country suddenly logs into an overseas server,it is highly likely that the system has been compromised.)What was the authentication method—password or public key?This helps you quickly identify unauthorized access.

3.Auditing Post-Login Activity

A correct password alone does not guarantee security.You can use the`history`command in conjunction with the system-level`auditd`service to track the specific commands executed after a user logs in,ensuring that no sensitive data has been stolen and no malicious scripts have been planted.

Troubleshooting:Can’t Connect?Check the Logs

The core logic for SSH troubleshooting is to use logs to determine the root cause:is it a network connectivity issue,an authentication failure,or a service crash?

Scenario 1:Authentication Failure(`Permission denied(publickey/password)`)

-Troubleshooting:Check the logs.If you see`Failed publickey`,it means the key provided by the client is not accepted by the server.Verify that the public key in the server’s`~/.ssh/authorized_keys`file is free of typos,and ensure that the user’s home directory and the`.ssh`folder have the correct,strict permissions—`700`and`600`,respectively.

-If it is a password error:Confirm that the password you entered is correct,and verify whether the user is permitted to log in via SSH(check the`AllowUsers`configuration in`sshd_config`).

Scenario 2:Connection Timed Out or Refused(`Connection timed out`/`Connection refused`)

-Troubleshooting:If there are no authentication failure records in the logs,it means the data packets never reached the SSH service.This is typically because the cloud platform’s security group or the server’s local firewall has not allowed traffic on port 22(or the custom port you have configured).Check the status of`iptables`or`ufw`,and log in to your cloud provider’s console to verify the security group rules.-If you receive a"Connection refused"error:This indicates that the port is open,but the SSH service is not running.Check the status using`systemctl status sshd`to see if it has failed,or verify if the service is listening on port 22 using`ss-tlnp|grep 22`.If the service is down,it may be due to a syntax error introduced while modifying the configuration file(you can test this using`sshd-t`).

Scenario 3:Being disconnected immediately after logging in("Connection closed by remote host")

-Troubleshooting:If the logs show an"Accepted"entry followed immediately by a"Disconnected"event,the cause is often a full disk or an Out-of-Memory(OOM)situation that triggered the termination of the`sshd`process.Check disk space with`df-h`and memory usage with`free-h`.Additionally,this can be caused by restrictive SELinux policies or incorrect permissions on the user's home directory.

Strengthening Defenses:Beyond Just Checking Logs

Troubleshooting and auditing are merely reactive measures.Effective system administration requires the implementation of proactive,automated defense mechanisms:

-Change the default port:Moving the SSH service from port 22 to 2222(or a higher port number)can block 90%of indiscriminate automated scans.

-Enforce key-based authentication:Set`PasswordAuthentication no`in your`sshd_config`file to completely eliminate the risk of password-based brute-force attacks.

-Deploy Fail2Ban:This tool monitors SSH logs in real-time;if it detects repeated failed login attempts from a specific IP address,it automatically updates the firewall to block that IP for several hours.

Underlying Infrastructure:Ensuring Robust Logging and Network Connectivity

The effectiveness of SSH auditing and troubleshooting depends not only on your configuration but also on the stability of the underlying server infrastructure.

Frequent log writes place a constant strain on disk I/O.If you are using low-cost shared storage,a brute-force attack—which generates a massive volume of log entries—could cause disk congestion,potentially preventing the`sshd`process from accepting new connections.Furthermore,if you attempt to check logs remotely during peak hours,poor network conditions can lead to significant latency,causing even a simple command like`tail`to hang for several seconds.

Frequently Asked Questions

Q:What should I do if the SSH log file(`auth.log`)has not been cleared for months and has filled up the entire disk?

A:The Ubuntu system comes with the`logrotate`tool installed by default,which automatically rotates and compresses`auth.log`on a daily basis.If your disk space is still full,you can adjust the retention count in`/etc/logrotate.d/rsyslog`or run`journalctl--vacuum-time=7d`to clear system logs.

Q:My IP has been automatically banned by Fail2Ban;how can I unban myself?

A:Log in to your server via the VNC terminal in your cloud console and run`fail2ban-client set sshd unbanip<your-IP>`to remove the ban.If the IP was manually blocked via the firewall,use`ufw delete deny from<your-IP>`or modify your`iptables`rules.

Q:What does the log message"Connection closed by authenticating user"mean?

A:This indicates that the client successfully established a network connection but disconnected while exchanging authentication information.The most common reasons are that the client failed to provide a valid private key,or the server rejected the public key provided(i.e.,it is not listed in`authorized_keys`).It could also be due to conflicting parameters in the client's SSH configuration file.

Pre-sales consultation
JTTI-Eom
JTTI-Selina
JTTI-Luca
JTTI-Ellis
JTTI-Defl
JTTI-Coco
JTTI-Amano
JTTI-Benoit
Technical Support
JTTI-NOC
Title
Email Address
Type
Sales Issues
Sales Issues
System Problems
After-sales problems
Complaints and Suggestions
Marketing Cooperation
Information
Code
Please complete the security check first
Submit