DEV Community

KALPESH
KALPESH

Posted on

Essential Linux Commands- 2

Shortcuts:

Reverse Search

Ctrl + R then type the command you want to search then Tab

Commands:

Info of Command

# brief description of command
whatis cat
Enter fullscreen mode Exit fullscreen mode

Real time process info

# Real time process
top

# Wrapper: Graphical Process
htop

# System stat for performance check
vmstat

# Amount of CPU available
nproc
Enter fullscreen mode Exit fullscreen mode

List Process & Hierarchy

# List Process not a Real time
ps

# Detail all process list
ps aux

# No of process(Line No)
ps aux | ln

# Only shows no lines
ps aux | wc -l

# kill process
kill <PID>

# Force process delete
kill -9 <PID>

# Thread dumnp
kill -3 <PID>

# Stop process
kill -STOP <PID>

# Resume Stop process
kill -CONT <PID>

# Prioritize Process (-n [1-20], lower no means high prioritize)
renice -n 10 -p <PID>

# Process Hierarchy
pstree -p

# Port used by Process
lsof -i :8085
Enter fullscreen mode Exit fullscreen mode

Inspect Network Connection

# Get active ports in Use
netstat -tuln

# Network Interface Info
ifconfig

# Network Troubleshoot (Can WireShark tool)
# enX0: Network Interface
sudo tcpdump -i enX0 port 80

# Test Connectivity
ping google.com

# Tarce the path packet to reach destination
traceroute google.com
Enter fullscreen mode Exit fullscreen mode

Disk Space, Size & Memory

# Check Disk Space
df -h

# Size of dir or file
# opt: directory
du -sh opt

# Memory (RAM)
free -h

# List Blob(All type of formats- Raw state) attach to Instance
lsblk

# Format the Blob Storage to linux supperted file system (ext4)
mkfs -t ext4 /dev/xvdf

# Mount it in order to use
mount /dev/xvdf mnt/demo-volume/
Enter fullscreen mode Exit fullscreen mode

Services

  • Systemd manages services
# Logs of Services
journalctl

# Particular Service
journalctl -u nginx

# Logs of services from Last Boot
journalctl -b
Enter fullscreen mode Exit fullscreen mode

Logs Filter

# Last 10 line of logs
tail -n 10 /var/log/auth.log

# First 10 line of logs
head -n 10 /var/log/auth.log
Enter fullscreen mode Exit fullscreen mode

Alias

alias detail_list='ls -la'

# Want to persist the Alias, add in '~/.bashrc'
Enter fullscreen mode Exit fullscreen mode

Symbolic Link

  • Soft Link (Like Windows Shortcuts)- It can be broken

    # ln: link
    # -s: flag for soft link 
    # myfile: file you want to crate soft link
    # slink: name of soft link file created
    ln -s myfile slink
    
  • Hard Link (Actual Copy)- Doesnโ€™t break the other one

    # ln: link
    # myfile: file you want to crate soft link
    # hlink: name of hard link file created
    ln myfile hlink
    

Users

# Full setup of User
adduser tim

# Just Add user
useradd tim

# Delete user
userdel tim

# Login into User with sudo privilege
sudo su - tim

# Root user indication
"#"

# Standard user indication
"$"
Enter fullscreen mode Exit fullscreen mode

Groups & Ownership

==================================
# Before you shoud be in root user
==================================
# Create Group
groupadd devops

# Add User into Group- (Adding user:tim into group:devops)
usermod -aG devops tim

# Remove user tim from group devops
deluser tim devops 

# To see how manu group user belong to
id tim

# Change ownership of dir
# change owenership of dir to nexus:nexus(user:group) -R recursive
chown -R nexus:nexus <dir-name>
Enter fullscreen mode Exit fullscreen mode

SSH Server

# ssh server config- sshd
ls /etc/ssh/sshd_config.d/<50-cloud-init.conf>
Enter fullscreen mode Exit fullscreen mode

File Management

# Overwriting the existing content
echo "Hello" > file.txt

# Appending the content
echo "Hello" >> file.txt
Enter fullscreen mode Exit fullscreen mode

Services

# Re-read all service configuration files from /etc/systemd/system/
sudo systemctl daemon-reload

# Registers your service to start automatically at boot time
sudo systemctl enable myservice

# Starts your service right now
sudo systemctl start myservice

# Check status of your service
sudo systemctl status myservice
Enter fullscreen mode Exit fullscreen mode

Refer Linux Journey

Linux Journey

Top comments (0)