Save your time, have these advanced Linux commands in your cheatsheet!
Tags: linux, productivity, programming, bash
Published: 2020-04-27 19:50:51

:shell: I describe Shell with words `omnipresence` or `has ubiquity`. Quick filtering or command chaining is like magic and gives us more powers, with Pipe ("|") and CLI utilities like sed, awk, grep, etc.


:notebook: Hence, if you are related to Linux and Server troubleshooting in any way, and that involves your day to day work. Here is some quick reference to command-line know-hows:

>  Note: This is Part 1, just a quick reference guide that I've had in my cheatsheet for server troubleshooting. The list I have is exhaustive, so I will keep things simple and continue to share insights in a phased-out manner.

 

```javascript
---Super-powers with:
ps | SSH | Removing files | Grep | Find | Sudo | SSL
---
```

## # SSL

With `curl's` insecure option we can filter the output and get only the `Server certificate` information:

```javascript
~ $ curl --insecure -v https://www.sachcode.com 2>&1 | \
> awk 'BEGIN { cert=0 } /^\* Server certificate:/ { cert=1 } \
>  /^\*/ { if (cert) print }'

* Server certificate:
* 	subject: CN=*.sachcode.com
* 	start date: Mar 22 04:34:05 2020 GMT
* 	expire date: Jun 20 04:34:05 2020 GMT
* 	common name: *.sachcode.com
* 	issuer: CN=Let's Encrypt Authority X3,O=Let's Encrypt,C=US
* Connection #0 to host www.sachcode.com left intact
```



## # ps(processes status)

Sort by the `highest CPU utilization` in ascending order:
```javascript
~ $ ps -aux --sort -pcpu | less
```

Sort by the `highest Memory utilization` in ascending order:
```javascript
~ $ ps -aux --sort -pmem | less
```

Print all processes running as `Root`: 
```javascript  
~ $ ps -U root -u root
```
Show process tree of all PIDs:
```javascript
~ $ ps auxwf
```
Show all process info and hierarchy:
```javascript
~ $ ps -efH
```

Find top running processes by highest memory and CPU usage:
```javascript
~ $ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
```
or 
```javascript
~ $ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
```


## # SSH

Generate generic ssh key pair:
```javascript
~ $ ssh-keygen -q -t rsa -f ~/.ssh/<name> -N '' -C <name>
```


## # Removing files

Remove files `over 30 days old`:
```javascript
~ $ find . -mtime +30 | xargs rm -rf
```

Remove files `older than 7 day` starting with 'backup':
```javascript
~ $ find . -type f -name "backup*" -mtime +7 -exec rm {} \;
```


## # Grep

Look through all files in current dir for word “foo”:
```javascript
~ $ grep -R "foo” .
```

View last ten lines of output:
```javascript
~ $ grep -i -C 10 "invalid view source” /var/log/info.log
```

Display line number of message:
```javascript
~ $ grep -n “pattern” <file>
```



## # Find

Exclude directories in find:
```javascript
~ $ find /tmp -not \( -path /tmp/dir -prune \) -type p -o -type b
```


## # Sudo

> To check sudo access for a user!

Method 1:
```javascript
~ $ sudo -l -U sachcode 
User sachcode may run the following commands on host:
    (ALL) ALL
```

Method 2: Another way to find out if a user has sudo access is by checking if the said user is member of the sudo group.
```javascript
~ $ groups sachcode
sachcode : sachcode wheel docker
```

Method 3: list all sudo users of your system
```javascript
~ $ genet group sudo
```

Read more about me: https://sachcode.com/
Reference on my website: https://sachcode.com/tech/linux-troubleshooting-cheatsheet/