Linux Commands / IT Support Guides
Search Linux logs with grep and context
A matching error line rarely tells the full story. Include nearby lines to see the request, retry and preceding cause without copying an entire log file.
Environment and inputs: Linux · Bash-compatible shell; package and service names vary by distribution
Try the diagnostic command
Use the stated shell and platform. Replace sample domains, addresses, services and paths with values from the system you are authorized to support.
grep -n -i -C 3 -- 'connection refused' /var/log/app/app.log
How to read the result
The query uses a literal phrase that has no regex metacharacters, ignores case and includes three context lines on each side. Line numbers help another engineer locate the same passage.
What to do next
Compare repeated matches by timestamp and destination. Distinguish a startup retry that recovered from a persistent failure affecting the user's request.
Before you make a change
Replace the log path and redact credentials. Rotated compressed logs require another reader, and a missing match can reflect a different message or logging level.