How to Check if a Linux Server is Hacked

Introduction

The following steps may help you find traces of hacking on your Linux server.

1 Monitor users’ activities

2 Check system process

3 Check the network traffic

4 Check cron jobs

5 Check Rootkits infections

1 Monitor users’ activities

1.1 Check the currently logged-in user

First, log in to your Linux server to view the currently logged-in user through the command "w". Then, go to https://www.iplocation.net/ to check the logged-in IP address. If there are any unfamiliar IPs, your server may be hacked.

Check the currently logged-in user
1.2 Check recently logged in users and IP information

Use the command "last -10" to view the information of users who have recently logged in to the system.

Check recently logged in users and IP information
1.3 Check the bash history

If you suspect a specific user of malicious activity, you can check the bash history. Log in as the user you would like to investigate and run the commands below.

Check the bash history

2 Check system process

The first step is to check if there are any unknown or suspicious processes.
2.1 Check processes with high CPU and memory usage

Use the command "top" to view the processes that occupy more than 30% of the CPU or memory. If it is not the process you are running, your Linux server may be implanted with malicious programs.

Check processes with high CPU and memory usage
2.2 Check all processes

View all process information through the command "ps -aux".

Check all processes
2.3 Check process-related files based on PID

Check the files opened by the process by the command "lsof -p PID". Please replace the PID with the PID number of the suspicious process obtained in the previous two steps.

If it prompts the "-bash: lsof: command not found" error, you need to install lsof:
CentOS: yum install -y lsof
Ubuntu: sudo apt-get install -y lsof

Check all processes
2.4 Check the exe file of suspicious process

Use the command "ll /proc/PID/exe" to view the exe file associated with the suspicious process. Make sure to replace the PID with the PID number of the suspicious process obtained in the previous two steps. If you detect any suspicious script file, then your Linux server is probably hacked.

Check the exe file of suspicious process

3 Check network traffic

If a hacker keeps something in your system for communication or sending messages, you can detect it by monitoring your traffic for unusual activity.
3.1 Check bandwidth usage

Use the command "iftop -n -P" to monitor the current network traffic.

If it prompts the "-bash: iftop: command not found" error, you need to install iftop first:
CentOS: yum install -y iftop
Ubuntu: sudo apt-get install -y iftop

Check bandwidth usage

The first column shows the localhost, => and <= indicates the traffic is incoming and outgoing respectively. Some are followed by the remote host addresses.
The last column presents the bandwidth used by each connection.
TX: send traffic
RX: receive traffic
TOTAL: total traffic
Cum: Total traffic from running iftop to the current time
peak: peak flow
For more information about the command "iftop", please access https://www.unixmen.com/iftop-a-network-bandwidth-monitoring-tool-for-linux/