In this article, I will talk about the Linux commands often used by middleware administrators or Sysadmin daily.
1. Finding WebSphere SystemOut.log
If you are working on the existing environment and if an administrator has redirected SystemOut.log to some other location, then it would be challenging to find it.
find / -name SystemOut.log
Tips: if you have multiple file systems, then it may take time to search. So best would be to replace / with the actual file system where you think the log would be. Let’s say you believe log is in /opt file system, so you can do this.
find /opt –name SystemOut.log
2. Know which process is holding a specific port number
You will often have to deal with port conflict issues, especially in the shared environment. If there is a situation to find out which process is holding the port number, here is how you can know.
netstat –anlp | grep 443
For Example:
[root@Chandan ~]# netstat -anlp | grep 443
tcp 0 0 0.0.0.0:443 LISTEN 20924/nginx
[root@Chandan ~]#
Note: Above example shows nginx with PID 20924 is holding 443 port.
3. Server boot time
You can use this command if you are performing auto-startup troubleshooting and would like to know when the server was rebooted.
who –b
Ex
[root@Chandan ~]# who -b
system boot Jun 28 01:11
[root@Chandan ~]#
who -b
Command will give you the exact date and time of the server reboot. Alternatively, you may also use uptime
to check how long the server is up.
[root@Chandan ~]# uptime
01:20:27 up 14 days, 9 min, 1 user, load average: 0.00, 0.00, 0.00
[root@Chandan ~]#
4. Check CPU/Memory utilization in runtime
If you have a performance issue, you might want to know the current CPU/Memory utilization. This will help you determine which process takes high CPU/Memory in real-time.
top
top - 01:16:21 up 14 days, 5 min,1 user,load average: 0.00,0.00,0.00
Tasks: 70 total,1 running, 69 sleeping,0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
Mem: 502220k total, 454920k used, 47300k free, 143476k buffers
Swap: 0k total, 0k used, 0k free,245412k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
29121 root 20 0 15008 1260 992 R 0.3 0.3 0:00.02 top
Tip: look for the CPU/Memory section for the first few PID to find out the utilization.
Alternatively, you may also use the free
command to find out total and free memory.
free –m
[root@Chandan ~]# free -m
total used free shared buffers cached
Mem: 490 444 46 0 140 239
-/+ buffers/cache: 64 425
Swap: 0 0 0
[root@Chandan ~]#
As you can see above, there is 490 MB of total memory, and only 46 MB of memory is available.
Another command called SAR (System Activity Report) is also very helpful in finding CPU and memory stats.
To check the CPU utilization
sar
To check the Memory utilization
sar -r
5. Kill the process
Occasionally, the process doesn’t stop gracefully if it’s hung or defunct. In this scenario, you can kill the process manually.
kill PID
If the above doesn’t help, you can forcefully use -9 to kill the process.
kill -9 PID
Note: PID is your process ID
6. Compression & Extraction
Most often you will have to deal with compressing the files as housekeeping activity for a file system.
Compression
gzip
command can be used to compress the files.
gzip filename
Tip: if you have multiple files to be compressed, you can use
gzip <strong>*</strong>
which will compress all the files available in that working directory.
Extraction
gunzip command will help you to extract the gz file.
gunzip filename
Tip: you can use gunzip * which will help you to extract all gz file in that working directory.
7. Total CPU, Memory, File System
Frequently asked to provide server information if you are working on migration or capacity planning. Here is a quick command to help you find the CPU, memory, and file system details.
CPU information
Find out CPU Cores, MHz, CPU manufacturer, Model Name and much more by using
cat /proc/cpuinfo
Tip: if you have a high-end server then output of the above command will be long. So you can use grep to filter out the required information. Ex, just to find out cores, you can use
cat /proc/cpuinfo | grep cores
Memory information
You can use one of the following commands to find out the total available memory.
First one….
free –G
This will show you total, free & cached memory
Second one…
cat /proc/meminfo
This will show you much more information along with total memory.
File System information
To find out file system size and usage, you can use df
command.
df –h
[root@Chandan tmp]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.6G 18G 9% /
tmpfs 246M 0 246M 0% /dev/shm
[root@Chandan tmp]#
Including –h will give you output in GB which is easy to understand.
8. Check out IP, Subnet Mask, MAC address and errors
It is probably one of the most widely used commands to discover IP-related information.
[root@Chandan tmp]# ifconfig
eth0 Link encap:Ethernet HWaddr 04:01:5A:25:57:01
inet addr:128.199.100.162 Bcast:128.199.127.255 Mask:255.255.192.0
inet6 addr: fe80::601:5aff:fe25:5701/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1491330 errors:0 dropped:0 overruns:0 frame:0
TX packets:1636419 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:306418374 (292.2 MiB) TX bytes:274350737 (261.6 MiB)
[root@Chandan tmp]#
As you can see above, ifconfig will show the IP addresses, Ethernet details, a hardware address (MAC), subnet mask, errors, and other information. This is an extremely useful handy command if you are looking for this information.
Tip: you can use –a with ifconfig to show all available Ethernet details. Ex:
ifconfig –a
9. Network commands like wget, telnet, traceroute
If you are doing application connectivity troubleshooting, then most likely, you will need to use these network commands.
Check if you can access particular URL from a server
You can quickly confirm if there is any connectivity issue on your server by using wget command. For example, if you need to check if the server can access an external website like https://geekflare.com or not, you can use the information below.
[root@Chandan tmp]# wget geekflare.com
--2015-07-12 02:52:56-- https://geekflare.com/
Resolving geekflare.com... 104.28.23.60, 104.28.22.60
Connecting to geekflare.com|104.28.23.60|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `index.html'
[ <=> ] 65,435 --.-K/s in 0.005s
2015-07-12 02:52:56 (13.4 MB/s) - `index.html' saved [65435]
[root@Chandan tmp]#
Tip: above you can see HTTP response code is 200 means there is no issue in connectivity.
Verify if the server can connect to backend service with the particular port number.
If you are connecting to some other application like a web service and need to verify if you can reach them, then you can use the telnet command. This is very useful for a connectivity test.
In the below example, I have checked if geekflare.com on 443 port can be reached or not.
[root@Chandan tmp]# telnet geekflare.com 443
Trying 104.28.23.60...
Connected to geekflare.com.
Escape character is '^]'.
If you see “Connected” then that confirms there is nothing wrong with connectivity/firewall. However, if you see a connection fail, then you know there is something like a firewall blocking your connection.
Tracing connection details
This may not always be used, but it is extremely helpful when you have a latency issue or just want to find out the connection path between your server and the destination.
Below example is to show the network path from my server to geekflare.com
[root@Chandan tmp]# traceroute geekflare.com
traceroute to geekflare.com (104.28.23.60), 30 hops max, 60 byte packets
1 128.199.127.253 (128.199.127.253) 5.110 ms 5.061 ms 128.199.127.254 (128.199.127.254) 0.418 ms
2 103.253.144.237 (103.253.144.237) 5.153 ms 0.463 ms 103.253.144.241 (103.253.144.241) 8.009 ms
3 as13335.singapore.megaport.com (103.41.12.6) 4.822 ms 13335.sgw.equinix.com (202.79.197.132) 4.819 ms as13335.singapore.megaport.com (103.41.12.6) 4.490 ms
4 104.28.23.60 (104.28.23.60) 4.454 ms 4.427 ms 4.366 ms
[root@Chandan tmp]#
10. Ownership modification
If you are dealing with multiple users on a server and having root permission, too, then most likely, once in a while, you will screw with permission/ownership. Well, not to worry its part of learning.
Here is the quick way to change the ownership or permission.
Change ownership
To change ownership of a particular file, you can use the chown
command as below.
chown user:group error.log
Tip: The above will change ownership of error.log to the mentioned user and group. If you have a folder, then you can use
–R
, which will change recursively to all the files within the folder. Ex:-
chown –R user:group somefolder
11. uname
Use uname
command with the -a
flag to print system information. This command will show you the kernel name, kernel release, kernel version, hostname, processor type & your hardware platform information.
ubuntu@ubuntu18:~$ uname -a
Linux ubuntu18 5.3.0-1028-azure #29~18.04.1-Ubuntu SMP Fri Jun 5 14:32:34 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Here is what it means
kernel name:Linux
hostname: ubuntu18
kernel release: 5.3.0-1028-azure
kernel version: #29~18.04.1-Ubuntu SMP Fri Jun 5 14:32:34 UTC 2020
machine hardware name: x86_64
processor: x86_64
hardware-platform: x86_64
operating system: GNU/Linux
12. du
To check disk space usage in a directory, use du
command. For example, to see disk space usage in the /var/log directory. Use -h
flag for human-readable format.
ubuntu@ubuntu18:~$ sudo du -h /var/log
24K /var/log/Microsoft/Azure/NetworkWatcherAgent/Logs
28K /var/log/Microsoft/Azure/NetworkWatcherAgent
32K /var/log/Microsoft/Azure
4.0K /var/log/samba
177M /var/log/journal/0f4f926f583b4691af7de11025b19ff6
177M /var/log/journal
...
204M /var/log
To see only the total usage only use -s
(summary) flag.
ubuntu@ubuntu18:~$ sudo du -hs /var/log
204M /var/log
13. ps
Use ps
to display status information about processes running on the system. To see all processes owned by user ubuntu, use -u
flag with the user name:
ubuntu@ubuntu18:~$ ps -u ubuntu
PID TTY TIME CMD
7804 ? 00:00:00 systemd
7805 ? 00:00:00 (sd-pam)
7940 ? 00:00:00 sshd
7941 pts/0 00:00:00 bash
8111 ? 00:00:00 sshd
8112 pts/1 00:00:00 bash
13868 ? 00:00:00 sshd
13869 pts/2 00:00:00 bash
13885 pts/0 00:00:00 man
13895 pts/0 00:00:00 pager
18111 pts/2 00:00:00 man
18121 pts/2 00:00:00 pager
18485 pts/1 00:00:00 ps
To view all processes run ps
with aux
flags:
ubuntu@ubuntu18:~$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.7 160076 7020 ? Ss Jun29 0:34 /sbin/init
root 2 0.0 0.0 0 0 ? S Jun29 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< Jun29 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< Jun29 0:00 [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< Jun29 0:00 [kworker/0:0H-kb]
....
where
Field | Description |
---|---|
PID | Process Identification Number |
%CPU | Percentage of CPU time the process is using |
%MEM | Percentage of RAM process is using |
VSZ | Virtual memory being used in KB |
RSS | Physical memory the process is using in KB |
TTY | Terminal associated with the process |
STAT | R – Running or ready to run, S – Sleeping, I – Idle, T – Stopped, Z – Zombie, D – Waiting for Disk I/O, X – Dead, W – Swapped out, N – Low priority process, < – High priority process |
14. dig
dig
is a great tool for DNS queries. It is used as follows :
dig <DNS server> <domain> <query-type>
where
- <DNS server> is the DNS server name you wish to query
- <domain> is the domain name you wish to query about
- <query-type> is the name of the record you wish to know – A, MX, NS SOA, etc.
To suppress verbose output, use +short
flag.
To view A record for google.com use:
ubuntu@ubuntu18:~$ dig google.com +short
172.217.164.174
To see MX records for google.com use:
ubuntu@ubuntu18:~$ dig google.com MX +short
50 alt4.aspmx.l.google.com.
10 aspmx.l.google.com.
20 alt1.aspmx.l.google.com.
40 alt3.aspmx.l.google.com.
30 alt2.aspmx.l.google.com.
I hope the above commands help you with your work.