Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential Linux System Commands and Operations

Tech 2

CPU Information

Checking CPU Architecture

# Determine if the system is 32-bit or 64-bit
getconf LONG_BIT

Verifying 64-bit Support

cat /proc/cpuinfo | grep flags | grep ' lm ' | wc -l

Displaying CPU Details

lscpu

Example Output - 4 cores, 4 threads:

Architecture:          x86_64
CPU(s):                4
Thread(s) per core:    1
Core(s) per socket:    4

Example Output - 6 cores, 12 threads:

Architecture:          x86_64
CPU(s):                12
Thread(s) per core:    2
Core(s) per socket:    6
Model name:            AMD Ryzen 5 5600X 6-Core Processor

Example Output - ARM aarch64 (8 cores):

Architecture:        aarch64
CPU(s):              8
Vendor ID:           HiSilicon
Model name:          Kunpeng-920

Viewing CPU Instruction Sets

cat /proc/cpuinfo | grep flags

Memory Management

/proc/meminfo

The /proc/meminfo file provides detailed memory usage statistics.

cat /proc/meminfo

Key fields include:

  • MemTotal: Total physical memory
  • MemFree: Free memory
  • MemAvailable: Available memory for new processes
  • SwapTotal: Total swap space
  • SwapFree: Free swap space

free Command

free -m
free -h

Output:

              total        used        free      shared  buff/cache   available
Mem:           15Gi       4.2Gi       8.1Gi       512Mi       2.7Gi        10Gi
Swap:          2.0Gi       0.0Gi       2.0Gi

vmstat Command

vmstat -s

smem for Advanced Memory Reporting

sudo apt install smem
smem -t -k

top and htop

top

Press M in top to sort by memory usage.

sudo apt install htop
htop

Process Memory Usage

ps aux --sort=-%mem

Managing Swap Space

Check current swap:

free -m

Create temporary swap file:

# Create 6GB swap file
sudo dd if=/dev/zero of=/swapfile bs=1G count=6

# Format as swap
sudo mkswap /swapfile

# Set permissions
sudo chmod 0600 /swapfile

# Enable swap
sudo swapon /swapfile

Automated swap creation script:

echo "######alloc swap######"
if [ ! -e /swapfile ];then
    sudo fallocate -l 4G /swapfile
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
    sudo /bin/sh -c 'echo  "/swapfile \t none \t swap \t defaults \t 0 \t 0" >> /etc/fstab'
    sudo swapon -a
fi

Remove swap:

# Disable and remove specific swap
sudo swapoff /swapfile
sudo rm /swapfile

# Disable all swap
sudo swapoff -a

Adjusting /dev/shm Size

Temporary modification:

mount -o size=1500m -o nr_inodes=1000000 -o noatime,nodiratime -o remount /dev/shm

Permanent modification:

tmpfs /dev/shm tmpfs defaults,size=1.5g 0 0
mount -o remount /dev/shm

Disk Operations

Checking Disk Space

lsblk
df -h

Extending Root Partition

View partition layout:

root@host:/# lsblk
NAME         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
mmcblk0      179:0    0 57.6G  0 disk
|-mmcblk0p1  179:1    0    4M  0 part
`-mmcblk0p6  179:6    0   14G  0 part /

Delete unused partitions:

root@host:/# fdisk /dev/mmcblk0

Command (m for help): d
Partition number (1-8, default 8): 8

Command (m for help): w

Extend the target partition using parted:

root@host:/# parted /dev/mmcblk0
(parted) resizepart 6
End? [15.4GB]?
(parted) quit

Resize the filesystem:

root@host:/# resize2fs /dev/mmcblk0p6

Fixing Read-Only USB Drives

# Monitor system logs
tail -f /var/log/syslog

# Find the USB device
df -h

# Unmount the USB
umount /media/user/DA18-EBFA

# Repair filesystem
sudo dosfsck -v -a /dev/sda1

Disk Performance Testing

sudo apt-get install hdparm

# Test SSD
sudo hdparm -t /dev/loop0

# Test HDD
sudo hdparm -t /dev/sda1

Mounting and Unmounting Drives

# Create mount point
sudo mkdir /mnt/drive

# Mount Windows drive
sudo mount -t drvfs C:\\ /mnt/c

# Mount SD card
mount /dev/mmcblk1p1 /mnt/sd

# Unmount
sudo umount /mnt/drive

Disk Partitioning

Create new partition:

# View available disks
lsblk

# Start fdisk
sudo fdisk /dev/sda
n           # New partition
p           # Primary partition
3           # Partition number
t           # Change type
8e          # Linux LVM type
w           # Write and exit

# Create filesystem
sudo mkfs -t ext4 /dev/sda3

# Mount partition
sudo mkdir /mnt/sda3
sudo mount /dev/sda3 /mnt/sda3

Delete partition:

sudo fdisk /dev/sda
d           # Delete partition
3           # Partition number
w           # Write and exit

sudo umount /dev/sda3

Process Management

# Find process by port
lsof -i :5001
netstat -tunlp | grep 5001

# View process status
cat /proc/PID/status

# Search processes
ps -aux | grep process_name
ps -ef | grep process_name
pgrep process_name

# Kill process
kill -9 PID
killall -9 process_name

# Batch kill processes
ps -ef | grep script.py | xargs kill -9

File Operations

# Directory size
du -sh
du -h --max-depth=1 ./work

# File size
du -sk filename
ls -lh filename

# Secure copy from remote
scp -r user@remote_ip:/remote/path /local/path

# Secure copy to remote
scp -r /local/path user@remote_ip:/remote/path

# Create nested directories
sudo mkdir -p /a/b/c/d

# Merge directory contents
find data/ -name "*.su" | xargs cat > combined.su

# Remove small files
find ./ -size -1k -exec rm {} \

File Type Detection

file executable

# x86_64 binary
ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)

# ARM binary
ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV)

System Information

OS Details

lsb_release -a
cat /etc/issue
cat /etc/os-release
uname -a

Detailed System Commands

uname -a       # Full system info
uname -i       # Hardware platform
uname -n       # Node name
uname -o       # OS name
uname -p       # Processor architecture
uname -r       # Kernel release
uname -v       # Kernel version

GPU Information

lshw -c video

IP Configuration

ip addr show

Shutdown and Reboot

# Shutdown immediately
sudo shutdown -h now
sudo poweroff
halt

# Schedule shutdown
shutdown -h 10        # 10 minutes
shutdown -c            # Cancel shutdown

# Reboot
sudo reboot
sudo shutdown -r now

# Schedule reboot
shutdown -r 10         # 10 minutes
shutdown -r 20:35      # At specific time

Networking

Network Manager Reset

sudo service network-manager stop
sudo rm /var/lib/NetworkManager/NetworkManager.state
sudo service network-manager start

IP Configuration

Temporary IP:

sudo ifconfig eth0 192.168.1.100 up
sudo route add default gw 192.168.1.1 eth0

Persistent IP:

sudo vim /etc/profile
# Add at end:
ifconfig eth0 192.168.1.100 up
route add default gw 192.168.1.1
source /etc/profile

Route Management

# View routes
route -n

# Add default route
sudo route add default gw 192.168.1.1 eth0

# Add host route
sudo route add -host 192.168.1.50 gw 192.168.1.1 eth0

# Add network route
sudo route add -net 192.168.2.0 netmask 255.255.255.0 eth0

# Delete routes
sudo route del default gw 192.168.1.1

DNS Configuration

sudo vim /etc/systemd/resolved.conf
# Add:
DNS=114.114.114.114

Or edit resolv.conf:

sudo vim /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 223.5.5.5

sudo /etc/init.d/networking restart

Network Interface Details

# View link speed
cat /sys/class/net/eth0/speed

# Detailed interface info
ethtool eth0

Hostname Management

# View hostname
hostname
hostnamectl

# Modify hostname
sudo vim /etc/hostname
127.0.1.1 new_hostname

DHCP Client

dhclient -v eth0

Port Monitoring

netstat -ltpn

Time Operations

# Get timestamp with nanoseconds
date +%Y-%m-%d' '%H:%M:%S.%N

# Millisecond precision
date +%Y-%m-%d' '%H:%M:%S.%N | cut -b 1-23

# Continuous monitoring
while true; do date +%Y-%m-%d' '%H:%M:%S.%N; sleep 0.1; done

Environment Variables

.bashrc Configuration

# Add to ~/.bashrc
export PACKAGE_ROOT=/path/to/package
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${PACKAGE_ROOT}/lib
export PATH=/opt/toolchain/bin:${PATH}

# Apply changes
source ~/.bashrc

Note: Use : as separator on Linux, ; on Windows.

/etc/profile

sudo vim /etc/profile
# Add at end:
export PATH=$PATH:/custom/path
source /etc/profile

List Variables

export -p

Compression Tools

zip/unzip

# Compress directory
zip -vr archive.zip /etc/

# Extract to current directory
unzip archive.zip

# Extract to specific directory
unzip archive.zip -d ./target/

tar

# Create tar.gz
tar -cvzf output.tar.gz /source/directory

# Extract tar.gz
tar -xvzf archive.tar.gz
tar -xvzf archive.tar.gz -C /target/directory

# Extract .tar.xz
tar -xJvf archive.tar.xz

7z

sudo apt-get install p7zip-full

# Extract zip
7z x archive.zip -o/target/path

# Extract rar
7z x archive.rar

File Downloads

curl

# Download single file
curl -O https://example.com/file.zip

# Download with progress
curl -# -O https://example.com/file.zip

# Limit speed
curl --limit-rate 1m -O https://example.com/file.zip

# Timeout settings
curl --connect-timeout 30 --max-time 300 https://example.com/file.zip

wget

# Basic download
wget https://example.com/file.tar.gz

# Save with custom name
wget -O output.tar.gz https://example.com/file.tar.gz

# Download to directory
wget -P /usr/software https://example.com/file.tar.gz

# Resume download
wget -c https://example.com/large-file.tar.gz

# Background download
wget -b https://example.com/file.tar.gz

# Skip SSL verification
wget --no-check-certificate https://example.com/file.tar.gz

Symbolic Links

# Create symlink
ln -s /source/path /link/path

# Remove symlink
rm -rf /link/path

# Modify symlink
ln -snf /new/target /link/path

File Integrity Verification

MD5 Checksum

# Generate checksum
md5sum -b file.bin > file.bin.md5

# Verify file
md5sum -c file.bin.md5

SHA256 Checksum

# Generate checksum
sha256sum -b archive.tar.gz > archive.tar.gz.sha256

# Verify file
sha256sum -c archive.tar.gz.sha256

Binary Analysis Tools

ldd - Library Dependencies

ldd /path/to/binary

file - Binary Type Detection

file /path/to/binary

nm - Symbol Table

nm binary | grep GLIBC_

strings - Embedded Strings

strings binary | grep GLIBCXX

readelf - ELF Analysis

# File header
readelf -h binary

# Dynamic section
readelf -d binary

Network Configuration

ifconfig

# List all interfaces
ifconfig -a

# Enable interface
sudo ifconfig eth0 up

# Set IP address
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

ldconfig - Library Cache

# Update library cache
sudo ldconfig

# Search for library
ldconfig -p | grep library_name

Add library path:

sudo vim /etc/ld.so.conf.d/custom.conf
# Add library path
sudo ldconfig

pkg-config

# Get package version
pkg-config --modversion package_name

# Get include paths
pkg-config --cflags package_name

# Get library paths
pkg-config --libs package_name

# List all packages
pkg-config --list-all

# Search packages
pkg-config --list-all | grep search_term

Quick Space Allocation

# Preallocate 2GB file
fallocate -l 2G testfile.dat

# Difference from dd:
# fallocate: instant allocation, no data written
# dd: writes actual data, slower

Kernel Messages

dmesg | grep driver_name

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.