Linux System Administration Commands
Command Line Utilities
Directory Creation with `mkdir`
The mkdir command is fundamental for creating directories. The -p flag is particularly useful as it allows you to create parent directories as needed, preventing errors if intermediate directories do not exist.
To create subdirectories even if the parent directories don't exist:
$ mkdir -p /tmp/foo/bar/this/dir
To create multiple directories with a single command, leveraging brace expansion for efficiency:
For example, to achieve this structure:
/disk1/ebooks/category-tech
/disk1/ebooks/category-motivation
/disk2/ebooks/category-tech
/disk2/ebooks/category-motivation
/disk3/ebooks/category-tech
/disk3/ebooks/category-motivation
You can use the following command:
mkdir -p /disk{1..5}/ebooks/category-{tech,motivation}
Disk Management Commands
Efficiently managing disk space and understanding disk usage are critical sysadmin tasks.
Checking Disk Space with `df`
To check overall disk space usage:
$ df -h
The -h flag provides human-readable output (e.g., KB, MB, GB).
Viewing Directory Sizes with `du`
To see the size of consumption per directory:
$ du -sh /path/to/dirs/*
The -s flag summarizes the total size for each argument, and -h makes the output human-readable.
Mapping Disks with `lsblk`
To view disk and SATA input mappings (where 0 typically represents the first port):
$ lsblk -o NAME,MODEL,SERIAL,WWN,HCTL
NAME MODEL SERIAL WWN HCTL
sda ST4000D0000-000000 XXXXXXXX 0x5000c50000000000 0:0:0:0
sdb ST4000D0000-000000 XXXXXXXX 0x5000c50000000000 1:0:0:0
└─sdb1 0x5000c50000000000
sdc ST4000D0000-000000 XXXXXXXX 0x5000c50000000000 2:0:0:0
└─sdc1 0x5000c50000000000
This command provides detailed information about block devices, including their model, serial number, and host controller target (HCTL).
Troubleshooting Unmount Issues
When a disk cannot be unmounted, it's usually because it's still in use by one or more processes.
umount /data
umount: /data: target is busy.
To identify which processes are using a directory, use the lsof (list open files) command. Running sudo lsof /data will list all processes accessing files within the /data directory.
sudo lsof /data
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sudo 21204 root cwd DIR 259,3 4096 27525121 /data/tmp
bash 28594 root cwd DIR 259,3 4096 27525121 /data/tmp
In the example above, a shell session (bash with PID 28594) was actively using the /data/tmp directory. You can either terminate the process using kill 28594 or switch to that session and exit the directory.
Memory Management and Monitoring
Understanding memory usage is crucial for system performance tuning.
- Free Memory: This is the memory not currently allocated to any process. A very low amount of free memory isn't necessarily bad, as unused RAM is often wasted.
- Available Memory: This represents the memory that can be readily allocated to new or existing processes without resorting to swapping.
Memory Statistics and Tools
Understanding the difference between VSZ (Virtual Set Size) and RSS (Resident Set Size):
The Virtual Set Size (VSZ) is the total amount of virtual memory assigned to a process. It includes all memory the process can access, including swapped-out memory and shared libraries.
The Resident Set Size (RSS) is the amount of physical RAM currently used by a process. This is a more accurate measure of the actual memory footprint of a process.
Using ps to sort processes by memory usage (RSS):
$ ps aux --sort -rss
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2124 0.1 11.4 422028 232864 ? Ssl Mar19 0:35 /bin/thanos
Examining memory information via /proc/meminfo:
$ cat /proc/meminfo
MemTotal: 16424260 kB
MemFree: 173988 kB
MemAvailable: 13493104 kB
Buffers: 253116 kB
Cached: 12950152 kB
Using vmstat for system statistics:
$ vmstat -s
16424260 K total memory
2598624 K used memory
3701676 K active memory
11821540 K inactive memory
328008 K free memory
253208 K buffer memory
13244420 K swap cache
Using top for real-time process monitoring:
$ top -n 1 -b
top - 07:50:06 up 13 days, 22:22, 1 user, load average: 0.06, 0.07, 0.02
Tasks: 124 total, 1 running, 73 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.0 us, 0.4 sy, 0.0 ni, 97.3 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
KiB Mem : 16424260 total, 249368 free, 2597924 used, 13576968 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 13490404 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15482 user 20 0 7949520 2.150g 24408 S 37.5 13.7 40:35.70 java
Explanation of key metrics in top:
%MEM: Percentage of physical memory used by the process, directly related to RES.
VIRT: Virtual memory size, encompassing all memory the process can access (shared, swapped, etc.).
RES: Resident Set Size, the amount of physical RAM the process is currently using.
SHR: Shared memory size, the amount of physical shared memory the process can access.
To get detailed memory usage per process, you can use the ps_mem.py script:
$ wget https://raw.githubusercontent.com/pixelb/ps_mem/master/ps_mem.py
$ sudo python ps_mem.py
File and Directory Permissions
Managing permissions is essential for system security and access control.
Setting Permissions with `chmod` and Ownership with `chown`
Example commands for setting permissions and ownership:
$ sudo chmod 755 /disk
$ sudo chown -R ubuntu:ubuntu /disk
$ sudo find /disk/2/htpc -type f -print -exec chmod 644 {} \;
$ sudo find /disk/2/htpc -type d -print -exec chmod 755 {} \;
chmod 755 grants read, write, and execute permissions to the owner, and read and execute to group and others. chown -R recursively changes the owner and group. The find command with -exec allows applying permissions to files (644) and directories (755) selectively.