# Essential Linux Commands- 2

## Shortcuts:

### Reverse Search

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

## Commands:

### Info of Command

```bash
# brief description of command
whatis cat
```

### Real time process info

```bash
# Real time process
top

# Wrapper: Graphical Process
htop

# System stat for performance check
vmstat

# Amount of CPU available
nproc
```

### List Process & Hierarchy

```bash
# 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
```

### Inspect Network Connection

```bash
# 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
```

### Disk Space, Size & Memory

```bash
# 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/
```

### Services

* `Systemd` manages services
    

```bash
# Logs of Services
journalctl

# Particular Service
journalctl -u nginx

# Logs of services from Last Boot
journalctl -b
```

### Logs Filter

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

# First 10 line of logs
head -n 10 /var/log/auth.log
```

### Alias

```bash
alias detail_list='ls -la'

# Want to persist the Alias, add in '~/.bashrc'
```

### **Symbolic Link**

* **Soft Link** (Like Windows Shortcuts)- It can be broken
    
    ```bash
    # 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
    
    ```bash
    # ln: link
    # myfile: file you want to crate soft link
    # hlink: name of hard link file created
    ln myfile hlink
    ```
    

### Users

```bash
# 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
"$"
```

### Groups & Ownership

```bash
==================================
# 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>
```

### SSH Server

```bash
# ssh server config- sshd
ls /etc/ssh/sshd_config.d/<50-cloud-init.conf>
```

### File Management

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

# Appending the content
echo "Hello" >> file.txt
```

### Services

```bash
# 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
```

## Refer Linux Journey

[Linux Journey](https://linuxjourney.com/)
