DEV Community

Cover image for Save your time, have these advanced Linux commands in your cheatsheet!
Sachin Jha
Sachin Jha

Posted on

Save your time, have these advanced Linux commands in your cheatsheet!

🐚 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.

πŸ““ 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.

---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:

~ $ 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:

~ $ ps -aux --sort -pcpu | less

Sort by the highest Memory utilization in ascending order:

~ $ ps -aux --sort -pmem | less

Print all processes running as Root:

~ $ ps -U root -u root

Show process tree of all PIDs:

~ $ ps auxwf

Show all process info and hierarchy:

~ $ ps -efH

Find top running processes by highest memory and CPU usage:

~ $ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

or

~ $ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head

# SSH

Generate generic ssh key pair:

~ $ ssh-keygen -q -t rsa -f ~/.ssh/<name> -N '' -C <name>

# Removing files

Remove files over 30 days old:

~ $ find . -mtime +30 | xargs rm -rf

Remove files older than 7 day starting with 'backup':

~ $ find . -type f -name "backup*" -mtime +7 -exec rm {} \;

# Grep

Look through all files in current dir for word β€œfoo”:

~ $ grep -R "foo” .

View last ten lines of output:

~ $ grep -i -C 10 "invalid view source” /var/log/info.log

Display line number of message:

~ $ grep -n β€œpattern” <file>

# Find

Exclude directories in find:

~ $ find /tmp -not \( -path /tmp/dir -prune \) -type p -o -type b

# Sudo

To check sudo access for a user!

Method 1:

~ $ 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.

~ $ groups sachcode
sachcode : sachcode wheel docker

Method 3: list all sudo users of your system

~ $ genet group sudo

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

Top comments (4)

Collapse
 
corentinbettiol profile image
Corentin Bettiol

Great article :)

For generating ssh key pair I use ed25519 algorithm, like this:

ssh-keygen -t ed25519 -C "user@email.ext"
Enter fullscreen mode Exit fullscreen mode

(documentation)

Collapse
 
waylonwalker profile image
Waylon Walker

Such a great list of examples. Often these are far too basic or complicated. This is a very useful set of non-trivial examples.

Collapse
 
aathith_r profile image
AATHITH RAJENDRAN

Remove files over 30 days old: from date of creation? or from date of last modified? Asking this because I can't find the file creation time for any of my files.

Collapse
 
chathumal profile image
Chathumal Jayasinghe

❀