0% found this document useful (0 votes)
9 views

Commands

Uploaded by

Kome Mohamad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Commands

Uploaded by

Kome Mohamad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

iostat is a command-line utility in Linux that is part of the sysstat package.

It is used to
monitor system input/output (I/O) device load, providing statistics about CPU usage and the
throughput of block devices such as disks. It's especially helpful for analyzing performance
bottlenecks related to disk I/O and CPU.

What iostat Displays

1. CPU Usage: It shows how the system is using its CPU, including time spent on user
processes, system processes, I/O wait, and idle time.
2. Device Statistics: It shows information about the throughput (read/write speeds), IOPS
(input/output operations per second), and I/O wait time for disks and partitions.

Basic Syntax
css
Copy code
iostat [options] [interval] [count]

● Interval: The time (in seconds) between updates.


● Count: How many updates you want.

Key Sections in iostat Output

1. CPU Utilization Report:


○ %user: Time spent on user-level processes.
○ %nice: Time spent on processes with adjusted nice values (lower priority).
○ %system: Time spent on kernel-level processes.
○ %iowait: Time spent waiting for I/O operations to complete.
○ %steal: Time stolen by other virtual machines in a virtualized environment.
○ %idle: Time when the CPU is idle.
2. Device Utilization Report:
○ tps: Transfers per second. The number of I/O operations that the device
completed per second.
○ kB_read/s: Kilobytes read per second.
○ kB_wrtn/s: Kilobytes written per second.
○ kB_read: Total kilobytes read since boot.
○ kB_wrtn: Total kilobytes written since boot.

Common Usage Examples


Check the basic I/O statistics:
bash
Copy code
iostat

1. This command will display the CPU usage since the last system boot and statistics about
the disks.

Monitor disk usage with intervals:


bash
Copy code
iostat 5

2. This will update the statistics every 5 seconds.

Monitor specific devices:


bash
Copy code
iostat -d /dev/sda 5

3. This will show statistics only for the /dev/sda disk every 5 seconds.

Display extended statistics including IOPS (input/output operations per second):


bash
Copy code
iostat -x 5

4. The -x flag gives extended statistics such as:


○ %util: Device utilization percentage.
○ await: The average time (in milliseconds) for I/O requests to be completed.
○ svctm: The average service time (in milliseconds) for I/O requests.
○ avgqu-sz: The average queue length of the device.

Check CPU and I/O statistics together:


bash
Copy code
iostat -c -d 5

5. The -c option shows only CPU statistics, and -d shows only device I/O statistics.

Example Output
bash
Copy code
Linux 5.11.0 (hostname) 10/14/2024 _x86_64_ (4 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle


2.30 0.10 1.60 0.50 0.00 95.50

Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn


sda 3.12 15.72 12.44 452364
214392

Key Points

● High %iowait: If %iowait is high, the system is spending a lot of time waiting for disk
I/O. This could indicate a bottleneck with storage.
● Device stats: If tps, kB_read/s, or kB_wrtn/s are very high, your disk is under
heavy usage.
● %util close to 100%: If device utilization is close to 100%, the device is fully utilized and
may become a bottleneck.

This makes iostat a very useful tool for diagnosing performance problems related to CPU and
disk I/O.

4o

You said:
vmstat in linux

ChatGPT said:
ChatGPT

vmstat (Virtual Memory Statistics) is a command-line tool in Linux that provides a snapshot of
system performance, including details about processes, memory, paging, block I/O, traps, and
CPU activity. It is a valuable tool for diagnosing performance issues, especially those related to
memory and CPU bottlenecks.

Basic Syntax
bash
Copy code
vmstat [options] [delay] [count]

● delay: The time (in seconds) between updates.


● count: The number of updates to display.

If you run vmstat without any options or parameters, it shows an average since the system
was booted.
Key Sections in vmstat Output

When you run vmstat, it presents a table with multiple columns showing different metrics:

Processes:

● r: The number of processes waiting for run time (ready to run). A high number indicates
CPU contention.
● b: The number of processes in an uninterruptible sleep state (usually waiting on I/O).

Memory:

● swpd: The amount of virtual memory used (in KB).


● free: The amount of idle/free memory (in KB).
● buff: The amount of memory used as buffers (in KB), mainly for block devices.
● cache: The amount of memory used as cache (in KB), to speed up file access.

Swap:

● si (swap in): The amount of memory swapped in from disk (in KB per second).
● so (swap out): The amount of memory swapped out to disk (in KB per second).

I/O:

● bi (blocks in): Blocks received from a block device (in KB per second).
● bo (blocks out): Blocks sent to a block device (in KB per second).

System:

● in: The number of interrupts per second, including the clock.


● cs: The number of context switches per second. High values can indicate a heavily
loaded system.

CPU:

● us (%user): Time spent running user-level (non-kernel) processes.


● sy (%system): Time spent running kernel-level (system) processes.
● id (%idle): Time the CPU is idle.
● wa (%iowait): Time the CPU spends waiting for I/O to complete.
● st (%steal): Time stolen by a hypervisor for other tasks in virtualized environments.

Common Usage Examples

1. Basic System Overview

To see a one-time snapshot of the system performance since the last reboot:
bash
Copy code
vmstat

2. Real-Time Monitoring

To display real-time updates every 2 seconds:

bash
Copy code
vmstat 2

This will show an ongoing update of system statistics every 2 seconds. Press Ctrl+C to stop.

3. Show a Specific Number of Updates

To show the system statistics every 5 seconds, but only 10 times:

bash
Copy code
vmstat 5 10

4. Monitor Memory Usage


bash
Copy code
vmstat -s

This shows a detailed breakdown of system memory usage in a human-readable format.

5. Monitoring Disk Activity (I/O stats)


bash
Copy code
vmstat -d

This provides detailed information on disk I/O.

Example Output
bash
Copy code
procs -----------memory---------- ---swap-- -----io---- -system--
------cpu-----
r b swpd free buff cache si so bi bo in cs us
sy id wa st
2 0 0 84316 26284 251456 0 0 104 50 295 381 3
1 96 0 0

Key Points:

● High r value: A high number of processes waiting in the run queue (r) may indicate
CPU bottlenecking.
● High b value: If the b column shows many processes in uninterruptible sleep, it could
mean that processes are waiting on disk I/O or other resources.
● Swap activity (si and so): If swap in (si) and swap out (so) values are high, the
system is actively using swap space, which can indicate memory exhaustion and
performance degradation.
● Disk I/O (bi and bo): High values for blocks in (bi) and blocks out (bo) may suggest
high disk activity, which could be a bottleneck.
● High CPU wa: A high %iowait (wa) value indicates that the CPU is often waiting for I/O
operations (disk or network).
● High us or sy: High user (us) or system (sy) times mean that the CPU is busy
processing tasks. High sy (system time) could indicate excessive kernel activity.

Practical Use Cases

● Diagnosing High CPU Usage: If r is high and %id (idle) is low, you likely have CPU
contention. Check which processes are using CPU with top or ps.
● Troubleshooting Memory Shortages: Look for high values in the si and so columns,
which could mean the system is swapping memory to disk, leading to slowdowns.
● Checking for I/O Bottlenecks: High values in bi (blocks in) and bo (blocks out) or %wa
(I/O wait) indicate that the system is waiting on slow disk operations.

Summary

vmstat is a versatile tool that provides real-time statistics on various system components,
helping troubleshoot CPU, memory, and I/O performance issues.

4o

You said:
dd in linux
ChatGPT said:
ChatGPT

The dd command in Linux is a powerful utility used for copying and converting data at the block
level. It is commonly used for tasks such as creating disk images, cloning disks, writing raw data
to devices, wiping disks, or creating backups.

Basic Syntax
bash
Copy code
dd [OPTIONS] [OPERAND]

Key Options and Operands

● if=: Specifies the input file (source). This can be a file, device, or even a disk partition.
● of=: Specifies the output file (destination), which can also be a file or device.
● bs=: Specifies the block size for reading and writing. For example, bs=1M sets the block
size to 1 megabyte.
● count=: Specifies the number of blocks to copy. For example, count=10 copies 10
blocks.
● status=progress: Shows the progress of the copy process, useful for long-running dd
operations.

Common Usage Examples

1. Copy a File

This example copies a file source_file to destination_file.

bash
Copy code
dd if=source_file of=destination_file

2. Create a Bootable USB Drive

This example copies a bootable ISO image to a USB drive.

bash
Copy code
dd if=/path/to/image.iso of=/dev/sdX bs=4M status=progress
Here, /dev/sdX refers to the USB device (e.g., /dev/sdb). Be careful when specifying the
destination (of=), as it will overwrite the device.

3. Create a Disk Image (Backup)

This example creates an image of an entire disk (/dev/sda) and saves it to a file
(backup.img).

bash
Copy code
dd if=/dev/sda of=/path/to/backup.img bs=1M status=progress

4. Restore a Disk Image

This example restores a previously created disk image (backup.img) to a disk (/dev/sda).

bash
Copy code
dd if=/path/to/backup.img of=/dev/sda bs=1M status=progress

5. Wipe a Disk (Fill with Zeros)

This example wipes all data from a disk by writing zeros to it.

bash
Copy code
dd if=/dev/zero of=/dev/sdX bs=1M status=progress

● /dev/zero is a special file that provides an endless stream of null (zero) bytes.
● /dev/sdX is the target disk (e.g., /dev/sda).

6. Wipe a Disk with Random Data

This example writes random data to a disk, which can be more secure for data wiping.

bash
Copy code
dd if=/dev/urandom of=/dev/sdX bs=1M status=progress

● /dev/urandom is a file that provides random data, which helps overwrite sensitive
information.
7. Measure Disk Read/Write Speed

This command writes a 1 GB file filled with zeroes to a disk to test the write speed:

bash
Copy code
dd if=/dev/zero of=testfile bs=1M count=1024 conv=fdatasync

● conv=fdatasync ensures that data is physically written to disk before the command
finishes, ensuring accurate timing for speed measurement.

8. Clone a Disk

This example clones the entire disk /dev/sda to /dev/sdb (make sure sdb has enough
capacity).

bash
Copy code
dd if=/dev/sda of=/dev/sdb bs=64K conv=noerror,sync status=progress

● conv=noerror: Continues the copy even if errors occur.


● conv=sync: Fills any bad blocks with null bytes, ensuring that the output file has the
same size as the input.

9. Backup the Master Boot Record (MBR)

This example backs up the first 512 bytes of a disk (where the MBR is stored).

bash
Copy code
dd if=/dev/sda of=/path/to/mbr_backup.img bs=512 count=1

Important Flags

● status=progress: Shows ongoing progress, which is especially useful for large data
transfers.
● bs=SIZE: Specifies the block size. Larger block sizes can speed up the copy process.
● conv=sync,noerror: Handles errors by syncing and continuing if bad blocks are
encountered.

Examples of Performance Tuning


Using a larger block size for faster copying:
bash
Copy code
dd if=/dev/sda of=/dev/sdb bs=64K status=progress

1. By increasing the block size, you can improve the speed of the copying process.

Compressing while creating a backup: You can pipe the dd output through gzip or bzip2 to
create a compressed image:
bash
Copy code
dd if=/dev/sda | gzip > /path/to/backup.img.gz

2.

Key Considerations

● Data Loss: dd works at the block level and can easily overwrite devices if you're not
careful. Always double-check the of= target to avoid overwriting critical data.
● Slowness: If the block size (bs) is too small, dd may take a long time to complete.
Using a block size like bs=1M or larger can improve performance.
● Error Handling: The conv=noerror and conv=sync options ensure that the
command continues even if there are errors, which is useful for cloning faulty drives.

Summary

● dd is a highly versatile tool that can perform low-level data copying, backups, cloning,
and data destruction.
● Always use caution with dd since it can overwrite data or devices without warning.

4o

You said:
iotop in linux

ChatGPT said:
ChatGPT

iotop is a command-line utility in Linux used to monitor disk I/O (input/output) usage by
processes in real time. It is particularly useful for identifying which processes are consuming the
most disk bandwidth, which can help troubleshoot disk I/O performance issues.

Installation
On many Linux distributions, iotop is not installed by default, so you may need to install it:

Debian/Ubuntu:
bash
Copy code
sudo apt install iotop

CentOS/RHEL/Fedora:
bash
Copy code
sudo yum install iotop

Arch Linux:
bash
Copy code
sudo pacman -S iotop

Basic Usage

Once installed, you can simply run the iotop command to see real-time I/O statistics:

bash
Copy code
sudo iotop

You need root privileges (or the ability to run sudo) to execute iotop.

Key Sections in iotop Output

● PID: The process ID of the running process.


● PRIO: The I/O priority of the process.
● USER: The user who owns the process.
● DISK READ: The amount of data being read from the disk by the process.
● DISK WRITE: The amount of data being written to the disk by the process.
● SWAPIN: The percentage of time the process has been waiting for swap I/O.
● IO: The percentage of time the process has been waiting for I/O operations (input/output
wait).
● COMMAND: The name of the process.

Sample Output
bash
Copy code
Total DISK READ : 5.01 M/s | Total DISK WRITE : 10.55 M/s
Actual DISK READ: 5.01 M/s | Actual DISK WRITE: 10.55 M/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO COMMAND
2341 be/4 user 0.00 B/s 5.01 M/s 0.00 % 0.00 % process1
567 be/4 user 5.01 M/s 0.00 B/s 0.00 % 0.00 % process2
3420 be/4 user 0.00 B/s 2.35 M/s 0.00 % 3.45 % process3

Useful Options

1. Monitor Only Active Processes

By default, iotop shows all processes, including those that are not doing any I/O. To filter and
show only the processes that are actively performing I/O, use the -o option:

bash
Copy code
sudo iotop -o

2. Accumulate I/O Over Time

To accumulate I/O usage over time, use the -a option. This option helps track the total I/O a
process has used since iotop started:

bash
Copy code
sudo iotop -a

3. Display I/O for Threads

If you want to see I/O usage by individual threads rather than processes, use the -T option:

bash
Copy code
sudo iotop -T
4. Show Only Processes with I/O Wait

You can use the -P option to show only processes that are waiting for I/O operations:

bash
Copy code
sudo iotop -P

5. Batch Mode

To run iotop in batch mode, where it prints output at regular intervals without requiring user
interaction, use the -b option. This is useful for logging or scripts:

bash
Copy code
sudo iotop -b

You can specify an interval in seconds for how often to update the output:

bash
Copy code
sudo iotop -b -d 5

This example updates the output every 5 seconds.

Example Use Cases

1. Identify High Disk I/O Processes

If your system is slow and you suspect that disk I/O is the bottleneck, running iotop will help
you identify which processes are causing the high disk usage. You can use the following to
monitor active processes:

bash
Copy code
sudo iotop -o

2. Monitor Disk Activity Over Time

To accumulate the total I/O of processes over time, useful for long-running tasks:
bash
Copy code
sudo iotop -a

3. Track a Process’s I/O Usage in Scripts

You can integrate iotop in scripts or automate the logging of I/O activity:

bash
Copy code
sudo iotop -b -o -d 2 > io_log.txt

This will record active I/O processes every 2 seconds and log them into io_log.txt.

Practical Example: Troubleshooting Disk Latency

If you’re experiencing high disk latency or slow disk response, you can run iotop to find out
which process is causing the issue. Processes with high values under the DISK READ or DISK
WRITE columns are likely responsible.

If you notice that a specific process (e.g., a database or a background service) is causing
significant I/O, you can use tools like kill, renice, or system tuning techniques to limit or stop
that process from overloading the disk.

Summary

● iotop is a real-time I/O monitoring tool that shows you which processes are consuming
the most disk I/O.
● It can help troubleshoot slow system performance related to high disk activity or find
rogue processes.
● Use it with root privileges to get detailed information about disk read/write rates, swap
usage, and I/O wait time.

4o

You said:
fio in linux

ChatGPT said:
ChatGPT

fio (Flexible I/O Tester) is a versatile and powerful benchmarking tool in Linux used to test disk
I/O performance. It can simulate various types of workloads and stress tests for disks and
filesystems, making it a go-to tool for analyzing the performance of storage devices like SSDs,
hard drives, and even network-based storage.

Installation

Before using fio, you might need to install it on your system:

Debian/Ubuntu:
bash
Copy code
sudo apt install fio

CentOS/RHEL/Fedora:
bash
Copy code
sudo yum install fio

Arch Linux:
bash
Copy code
sudo pacman -S fio

Basic Syntax
bash
Copy code
fio [OPTIONS] [job file]

fio can be run directly from the command line with specific options or by creating a job file that
defines various testing scenarios.

Common Parameters

● --name=NAME: Sets the name of the job.


● --rw=READWRITE: Specifies the type of I/O operation, such as:
○ read: Sequential read test.
○ write: Sequential write test.
○ randread: Random read test.
○ randwrite: Random write test.
○ rw: Mixed read/write.
● --size=SIZE: Sets the total size of the data to read/write.
● --bs=BLOCKSIZE: Sets the block size (default: 4K), meaning the size of each read or
write operation.
● --numjobs=NUM: Defines the number of processes or threads to use for the job.
● --time_based: Runs the test for a specific amount of time instead of based on the total
size.
● --runtime=TIME: Sets the runtime for time-based tests (e.g., --runtime=30s).
● --output=FILENAME: Redirects output to a file.

Example Usage

1. Simple Read Test

This example performs a sequential read test on a file or device with a block size of 4KB:

bash
Copy code
fio --name=read_test --rw=read --bs=4k --size=1G --numjobs=1
--time_based --runtime=30s --group_reporting

● --rw=read: Performs a sequential read operation.


● --size=1G: Operates on 1 GB of data.
● --time_based --runtime=30s: Runs for 30 seconds instead of completing the full 1
GB.
● --group_reporting: Shows combined results from all threads.

2. Simple Write Test

This example writes data sequentially to a file or device:

bash
Copy code
fio --name=write_test --rw=write --bs=4k --size=1G --numjobs=1
--time_based --runtime=30s --group_reporting

3. Random Read and Write Test


To simulate a workload where data is read and written randomly, which is common in
databases:

bash
Copy code
fio --name=randrw_test --rw=randrw --bs=4k --size=1G --numjobs=1
--runtime=30s --group_reporting

● --rw=randrw: Randomly reads and writes data.


● --bs=4k: Uses a block size of 4 KB.

4. Test with Multiple Jobs (Threads)

To simulate a multi-threaded workload where several processes are writing data at the same
time:

bash
Copy code
fio --name=multi_thread_test --rw=write --bs=4k --size=1G --numjobs=4
--runtime=30s --group_reporting

● --numjobs=4: Runs four simultaneous threads performing write operations.

5. Simulate SSD Workload

If you're testing an SSD and want to include both random reads and writes, this test simulates
real-world workloads:

bash
Copy code
fio --name=ssd_test --rw=randrw --rwmixread=70 --bs=4k --size=10G
--numjobs=4 --runtime=60s --group_reporting

● --rwmixread=70: Specifies 70% reads and 30% writes, which is typical for SSD usage
patterns.

6. Test IOPS (Input/Output Operations Per Second)

To measure the IOPS, run the following test:

bash
Copy code
fio --name=iops_test --rw=randread --bs=4k --size=1G --numjobs=4
--runtime=30s --group_reporting

This test focuses on random read performance with small 4 KB blocks, which is commonly used
to assess IOPS.

7. Latency Measurement

To measure latency during read operations:

bash
Copy code
fio --name=latency_test --rw=read --bs=4k --size=1G --numjobs=4
--runtime=30s --group_reporting --latency

The --latency option helps in tracking the I/O latency, which is useful for understanding
delays in storage systems.

Understanding the Output

Here's what a typical fio output looks like:

bash
Copy code
Run status group 0 (all jobs):
READ: io=1024MB, bw=3456KB/s, iops=864, runt=300000msec
WRITE: io=1024MB, bw=3456KB/s, iops=864, runt=300000msec

lat (usec) : 10=5.12%, 20=10.25%, 50=33.28%, 100=41.64%,


250=9.71%

Key elements in the output:

● READ/WRITE: Amount of data read/written, bandwidth, IOPS (input/output operations


per second), and total runtime.
● lat (usec): Latency statistics, showing how much time (in microseconds) the I/O
operations took.

Example Job File

Instead of passing options through the command line, you can define them in a job file:
ini
Copy code
[global]
ioengine=libaio
direct=1
bs=4k
size=1G
runtime=30s
group_reporting

[rand_read]
rw=randread

[rand_write]
rw=randwrite

You can run this job file with:

bash
Copy code
fio my_test_job.fio

Key Options for Fine-Tuning

● ioengine: Specifies the I/O engine. Common values are:


○ sync: Synchronous I/O.
○ libaio: Asynchronous I/O.
○ mmap: Memory-mapped I/O.
● direct=1: Bypasses the kernel page cache, ensuring direct I/O to disk.
● rwmixread: When using mixed workloads (read/write), it controls the percentage of
reads vs. writes.

Summary

● fio is an advanced I/O benchmarking tool used to test disk performance under different
conditions.
● It supports various read/write operations, including sequential, random, and mixed.
● You can use fio to measure metrics like IOPS, latency, and throughput, helping identify
performance bottlenecks in storage devices.
● It provides extensive configuration options to simulate real-world scenarios and
workloads, making it a key tool for system admins, database administrators, and storage
engineers.

You might also like