Linux Commands
Commands to PROCESS MANAGEMENT in Linux
Commands for process management in Linux, along with explanations, examples, and sample outputs for each command:
1. `ps`: Display information about active processes.
Explanation: The `ps` command shows information about running processes.
Example:
bash
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
username 1234 0.0 0.5 123456 7890 ? S Aug06 0:01 /usr/bin/example
2. `top`: Display real-time system processes and resource usage.
Explanation: The `top` command provides an interactive view of running processes and system resource usage.
Example:
bash
$ top
3. `htop`: Interactive process viewer with system metrics.
Explanation: Similar to `top`, `htop` provides an improved and user-friendly interface for process management.
Example (requires installation):
bash
$ htop
4. `kill`: Terminate processes by sending signals.
Explanation: The `kill` command is used to send signals to processes, often used to terminate them.
Example (terminate a process):
bash
$ kill PID
5. `killall`: Terminate processes by name.
Explanation: The `killall` command terminates processes by their names.
Example (terminate processes by name):
bash
$ killall process_name
6. `pgrep`: Search for processes by name.
Explanation: The `pgrep` command searches for processes based on their names.
Example (search for a process):
bash
$ pgrep process_name
7. `pkill`: Terminate processes by name.
Explanation: The `pkill` command is used to terminate processes based on their names.
Example (terminate processes by name):
bash
$ pkill process_name
8. `renice`: Change priority of running processes.
Explanation: The `renice` command changes the priority of running processes.
Example (change process priority):
bash
$ renice -n 10 -p PID
9. `nice`: Launch a process with a specified priority.
Explanation: The `nice` command starts a process with a specified priority.
Example (start a process with increased priority):
bash
$ nice -n -10 command_to_run
10. `nohup`: Run a command that survives terminal hangup.
Explanation: The `nohup` command runs a process that continues even after the terminal is closed.
Example (run a process in the background):
bash
$ nohup long_running_command &
11. `bg`: Place a process in the background.
Explanation: The `bg` command resumes a suspended process in the background.
Example (resume a suspended process):
bash
$ bg %1
12. `fg`: Bring a background process to the foreground.
Explanation: The `fg` command brings a background process to the foreground.
Example (bring a process to the foreground):
bash
$ fg %1
13. `jobs`: List currently running jobs.
Explanation: The `jobs` command lists processes running in the background or suspended.
Example (list background jobs):
bash
$ jobs
14. `pstree`: Display a tree of processes.
Explanation: The `pstree` command shows a tree-like representation of processes.
Example:
bash
$ pstree
15. `strace`: Trace system calls and signals.
Explanation: The `strace` command traces system calls and signals of a running process.
Example (trace system calls of a process):
bash
$ strace -p PID
16. `lsof`: List open files and network connections.
Explanation: The `lsof` command lists open files and network connections by processes.
Example (list open files for a process):
bash
$ lsof -p PID
17. `watch`: Execute a command repeatedly and display the output.
Explanation: The `watch` command runs a given command repeatedly and displays its output.
Example (monitor CPU usage):
bash
$ watch -n 1 "top -b -n 1"
18. `psrecord`: Record resource utilization of a process.
Explanation: The `psrecord` command records resource utilization of a process over time.
Example (requires installation):
bash
$ psrecord PID --log activity.log
19. `kill -9`: Forcefully terminate a process.
Explanation: The `kill -9` command forcefully terminates a process using the SIGKILL signal.
Example (forcefully terminate a process):
bash
$ kill -9 PID
20. `nice`: Adjust a process's priority.
Explanation: The `nice` command sets the priority of a process, making it run with lower or higher priority.
Example (run a command with lower priority):
bash
$ nice -n 10 command_to_run
utilization.
Commands for performance monitoring and statistics in Linux
Commands for performance monitoring and statistics in Linux, along with explanations, examples, and sample outputs for each command:
1. `top`: Display real-time system processes and resource usage.
Explanation: The `top` command provides an interactive view of running processes, their resource usage, and system statistics.
Example:
bash
$ top
2. `htop`: Interactive process viewer with system metrics.
Explanation: Similar to `top`, `htop` provides an improved and user-friendly interface for monitoring processes and system performance.
Example (requires installation):
bash
$ htop
3. `vmstat`: Report virtual memory statistics.
Explanation: The `vmstat` command provides information about system memory, processes, and CPU usage.
Example:
bash
$ vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 2027896 315352 11228036 0 0 6 9 23 45 2 1 97 0 0
4. `iostat`: Report CPU and I/O statistics.
Explanation: The `iostat` command displays CPU and input/output statistics for devices and partitions.
Example:
bash
$ iostat -c 1
Linux 5.4.0-81-generic (hostname) 08/06/23 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
10.87 0.00 1.18 0.16 0.00 87.80
5. `sar`: Collect and report system activity information.
Explanation: The `sar` command collects, reports, and analyzes system activity statistics over time.
The `sar -u 1` command is used to display CPU utilization statistics at regular intervals using the `sar` utility. Here's an explanation of the command, an example, and the expected output:
**Command Explanation**:
- `sar`: The System Activity Reporter (SAR) is a command-line utility that collects and reports system activity and performance metrics.
- `-u`: Specifies that CPU utilization statistics should be displayed.
- `1`: The interval in seconds at which the statistics should be collected and displayed.
**Example**:
$ sar -u 1
**Output Explanation**:
The output of the `sar -u 1` command will provide a real-time report of CPU utilization statistics, including various percentages related to CPU activity:
- `%user`: Percentage of time the CPU spends in user-level processes.
- `%nice`: Percentage of time the CPU spends on user-level processes with low priority (niceness).
- `%system`: Percentage of time the CPU spends in kernel-level processes.
- `%iowait`: Percentage of time the CPU is idle while waiting for I/O operations to complete.
- `%steal`: Percentage of time the hypervisor steals CPU cycles from a virtual machine.
- `%idle`: Percentage of time the CPU is idle and not executing any tasks.
The output will be updated every second, showing the values for each of the above CPU utilization categories.
Here's a sample output:
```plaintext
Linux 5.4.0-81-generic (hostname) 08/06/23 _x86_64_ (4 CPU)
11:59:55 AM CPU %user %nice %system %iowait %steal %idle
12:00:56 PM all 9.29 0.00 1.25 0.12 0.00 89.33
12:00:57 PM 0 6.82 0.00 2.23 0.26 0.00 90.69
12:00:58 PM 1 10.95 0.00 0.53 0.09 0.00 88.43
12:00:59 PM 2 7.33 0.00 0.89 0.05 0.00 91.74
12:01:00 PM 3 7.90 0.00 1.12 0.10 0.00 90.88
```
In the output above, each row represents CPU utilization statistics for a specific time interval. The columns represent CPU-related percentages as explained earlier. The `all` row shows aggregated statistics for all CPUs, and individual rows represent each CPU core (CPU 0, CPU 1, etc.). The output will continue to update at the specified interval.
6. `mpstat`: Report processor related statistics.
Explanation: The `mpstat` command displays processor statistics, including individual CPU utilization.
Example:
bash
$ mpstat 1
Linux 5.4.0-81-generic (hostname) 08/06/23 _x86_64_ (4 CPU)
11:02:01 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
11:02:02 AM all 8.67 0.00 1.33 0.00 0.00 0.00 0.00 0.00 0.00 89.00
7. `nload`: Monitor network traffic and bandwidth usage.
Explanation: The `nload` command displays real-time network traffic statistics.
Example (requires installation):
bash
$ nload
8. `ifstat`: Report network interface statistics.
Explanation: The `ifstat` command displays statistics for network interfaces.
Example (requires installation):
bash
$ ifstat -i eth0 1
eth0
KB/s in KB/s out
0.05 0.05
9. `netstat`: Display network connections, routing tables, and more.
Explanation: The `netstat` command provides information about network connections, routing, and interfaces.
Example (list network connections):
bash
$ netstat -tuln
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
10. `dstat`: Report system resource usage.
Explanation: The `dstat` command provides a comprehensive overview of system resource usage in real-time.
Example (requires installation):
bash
$ dstat
11. `iotop`: Monitor I/O usage by processes.
Explanation: The `iotop` command displays I/O usage of processes in real-time.
Example (requires installation):
bash
$ iotop
12. `free`: Display memory usage statistics.
Explanation: The `free` command shows information about memory usage and available space.
Example:
bash
$ free -h
total used free shared
buff/cache available
Mem: 7.7G 1.8G 3.9G 256M 2.0G 5.3G
13. `sar -r`: Report memory utilization.
Explanation: The `sar -r` command provides memory utilization statistics.
Example (requires installation):
bash
$ sar -r 1
Linux 5.4.0-81-generic (hostname) 08/06/23 _x86_64_ (4 CPU)
11:04:01 AM kbmemfree kbavail kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
11:04:02 AM 401296 512204 3483404 84.49 333948 1976884 932772 22.31 2021464 1257516 224
14. `pidstat`: Report process-related statistics.
Explanation: The `pidstat` command provides statistics about individual processes.
Example (requires installation):
bash
$ pidstat -u -r -p PID 1
Linux 5.4.0-81-generic (hostname) 08/06/23 _x86_64_ (4 CPU)
11:06:11 AM UID PID %usr %system %guest %CPU CPU Command
11:06:12 AM 0 1234 8.00 0.00 0.00 8.00 1 example_process
15. `uptime`: Display system uptime and load average.
Explanation: The `uptime` command shows the current system uptime and load average.
Example:
bash
$ uptime
11:08:53 up 1:23, 3 users, load average: 0.24, 0.26, 0.33
16. `atop`: System and process monitor.
Explanation: The `atop` command provides detailed system and process activity information.
Example (requires installation):
bash
$ atop
17. `mpstat -P`: Report per-processor statistics.
Explanation: The `mpstat -P` command displays per-processor statistics.
Example:
bash
$ mpstat -P ALL 1
Linux 5.4.0-81-generic (hostname) 08/06/23 _x86_64_ (4 CPU)
11:12:33 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
11:12:34 AM all 8.78 0.00 1.29 0.11 0.00 0.00 0.00 0.00 0.00 89.81
11:12:34 AM 0 6.82 0.00 2.23 0.26 0.00 0.00 0.00 0.00 0.00 90.69
11:12:34 AM 1 10.95 0.00 0.53 0.09 0.00 0.00 0.00 0.00 0.00 88.43
18. `lscpu`: Display CPU architecture information.
Explanation: The `lscpu` command provides detailed information about the CPU architecture.
Example:
bash
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
19. `lsblk`: List block devices and partitions.
Explanation: The `lsblk` command displays information about block devices and their partitions.
Example:
bash
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 238.5G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 238G 0 part /
20. `lsof`: List open files and network connections.
Explanation: The `lsof` command lists open files, sockets, and network connections by processes.
Example (list open files for a process):
bash
$ lsof -p PID
Commands related to file permissions in Linux
In Linux, file permissions control who can access a file and what actions they can perform on it. There are three types of permissions: read, write, and execute. These permissions are assigned to three categories of users: owner, group, and others. The permissions are represented using a combination of letters and symbols. Here's an explanation of file permissions in Linux:
1. **Read (r)**: Allows users to view the contents of a file or list the contents of a directory.
2. **Write (w)**: Allows users to modify the contents of a file or create, delete, and rename files within a directory.
3. **Execute (x)**: Allows users to execute a file (if it's a script or program) or traverse (cd) into a directory.
Permissions are assigned to three categories of users:
- **Owner (u)**: The user who owns the file or directory.
- **Group (g)**: The group associated with the file or directory.
- **Others (o)**: All other users who are not the owner or in the group.
Permissions are represented using a 10-character string:
- The first character represents the file type (`-` for a regular file, `d` for a directory).
- The next three characters represent owner permissions.
- The next three characters represent group permissions.
- The last three characters represent others' permissions.
Each permission category is represented by a combination of `r`, `w`, and `x`, or `-` if the permission is not granted.
Here are some examples of file permission strings and their meanings:
- `rw-r--r--`: Owner has read and write permissions; group and others have read-only permissions.
- `rwxr-x---`: Owner has read, write, and execute permissions; group has read and execute permissions; others have no permissions.
- `drwxrwxr-x`: A directory with owner and group having read, write, and execute permissions, and others having read and execute permissions.
You can use the `ls -l` command to view file permissions in the long format. For example:
```bash
$ ls -l myfile.txt
-rw-r--r-- 1 user group 1024 Aug 6 10:00 myfile.txt
```
In the above example, the file `myfile.txt` has `-rw-r--r--` permissions, indicating that the owner (`user`) has read and write permissions, the group (`group`) has read permissions, and others have read permissions.
==========================================================================
Commands related to file permissions in Linux, along with explanations, examples, and sample outputs for each command:
1. **`ls -l`**: List files with detailed information including permissions.
- Explanation: The `ls -l` command displays a long listing format that includes file permissions, ownership, size, and more.
- Example: `$ ls -l`
- Output Example:
```
-rw-r--r-- 1 user group 1024 Aug 6 10:00 myfile.txt
drwxr-xr-x 2 user group 4096 Aug 6 10:15 mydir
```
2. **`chmod`**: Change file permissions.
- Explanation: The `chmod` command is used to change file permissions for owner, group, and others.
- Example: `$ chmod u+rwx myfile.txt`
- Output Example: The permissions of `myfile.txt` are modified to allow read, write, and execute for the owner.
3. **`chown`**: Change file ownership.
- Explanation: The `chown` command changes the owner and group ownership of a file.
- Example: `$ chown newuser:newgroup myfile.txt`
- Output Example: The owner and group of `myfile.txt` are changed to `newuser` and `newgroup`.
4. **`chgrp`**: Change group ownership.
- Explanation: The `chgrp` command changes the group ownership of a file.
- Example: `$ chgrp newgroup myfile.txt`
- Output Example: The group of `myfile.txt` is changed to `newgroup`.
5. **`umask`**: Set default permissions for new files.
- Explanation: The `umask` command sets the default permissions mask for new files created by a user.
- Example: `$ umask 0022`
- Output Example: New files will be created with permissions `rw-r--r--`.
6. **`stat`**: Display file or file system status.
- Explanation: The `stat` command provides detailed information about a file, including permissions.
- Example: `$ stat myfile.txt`
- Output Example: Displays various file details, including permissions, timestamps, and more.
7. **`su`**: Switch user identity to become the superuser.
- Explanation: The `su` command allows you to switch to another user, often the superuser (root).
- Example: `$ su`
- Output Example: You become the superuser and can perform administrative tasks.
8. **`sudo`**: Execute a command as the superuser.
- Explanation: The `sudo` command allows authorized users to execute commands with superuser privileges.
- Example: `$ sudo apt-get update`
- Output Example: The command is executed with elevated privileges.
9. **`passwd`**: Change user password.
- Explanation: The `passwd` command allows users to change their passwords.
- Example: `$ passwd`
- Output Example: User is prompted to change their password.
10. **`umask -S`**: Display the current umask in symbolic notation.
- Explanation: The `umask -S` command shows the current default permission mask in symbolic notation.
- Example: `$ umask -S`
- Output Example: `u=rwx,g=rx,o=rx` indicates the default permissions for new files.
11. **`id`**: Display user and group information.
- Explanation: The `id` command shows user and group information, including memberships.
- Example: `$ id`
- Output Example: Displays user's UID, GID, and group memberships.
12. **`getfacl`**: Get file access control lists.
- Explanation: The `getfacl` command displays the file's access control lists, including extended permissions.
- Example: `$ getfacl myfile.txt`
- Output Example: Displays ACL entries for the file.
13. **`setfacl`**: Set file access control lists.
- Explanation: The `setfacl` command sets access control lists on a file, providing finer-grained permissions.
- Example: `$ setfacl -m u:newuser:rwx myfile.txt`
- Output Example: Adds read, write, and execute ACL for `newuser` on `myfile.txt`.
14. **`umask`**: Display or set the file creation mask.
- Explanation: The `umask` command displays or sets the default file creation mask.
- Example: `$ umask`
- Output Example: Displays the current umask value.
15. **`adduser`**: Create a new user account.
- Explanation: The `adduser` command is used to create a new user account.
- Example: `$ adduser newuser`
- Output Example: Creates a new user account named `newuser`.
16. **`deluser`**: Delete a user account.
- Explanation: The `deluser` command deletes a user account.
- Example: `$ deluser olduser`
- Output Example: Deletes the user account named `olduser`.
17. **`addgroup`**: Create a new group.
- Explanation: The `addgroup` command creates a new group.
- Example: `$ addgroup newgroup`
- Output Example: Creates a new group named `newgroup`.
18. **`delgroup`**: Delete a group.
- Explanation: The `delgroup` command deletes a group.
- Example: `$ delgroup oldgroup`
- Output Example: Deletes the group named `oldgroup`.
19. **`passwd`**: Change user password.
- Explanation: The `passwd` command allows users to change their passwords.
- Example: `$ passwd`
- Output Example: User is prompted to change their password.
20. **`su`**: Switch user identity to become the superuser.
- Explanation: The `su` command allows you to switch to another user, often the superuser (root).
- Example: `$ su`
- Output Example: You become the superuser and can perform administrative tasks.
Commands related to Networking in Linux
Commands related to networking in Linux, along with explanations, examples, and sample outputs for each command:
1. **`ifconfig`**: Display network interface configuration.
- Explanation: The `ifconfig` command shows the IP addresses and network configuration of all network interfaces on the system.
- Example: `$ ifconfig`
- Output Example:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
...
2. **`ip addr`**: Display IP address information.
- Explanation: The `ip addr` command provides detailed information about network interfaces, including IP addresses.
- Example: `$ ip addr`
- Output Example:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
inet 127.0.0.1/8 scope host lo
...
3. **`ping`**: Send ICMP echo requests to a host.
- Explanation: The `ping` command tests network connectivity by sending ICMP echo requests to a target host.
- Example: `$ ping google.com`
- Output Example:
PING google.com (172.217.12.78) 56(84) bytes of data.
64 bytes from 172.217.12.78: icmp_seq=1 ttl=116 time=18.4 ms
4. **`traceroute`**: Display the route packets take to a destination.
- Explanation: The `traceroute` command traces the route that packets take to reach a destination host, showing each hop.
- Example: `$ traceroute google.com`
- Output Example:
traceroute to google.com (172.217.12.78), 30 hops max, 60 byte packets
1 router (192.168.1.1) 0.543 ms 0.593 ms 0.657 ms
...
5. **`netstat`**: Display network connections, routing tables, and more.
- Explanation: The `netstat` command provides information about network connections, routing, and interfaces.
- Example: `$ netstat -tuln`
- Output Example:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
6. **`ss`**: Display socket statistics.
- Explanation: The `ss` command provides socket statistics, including active connections, listening ports, and more.
- Example: `$ ss -tuln`
- Output Example:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
7. **`hostname`**: Display or set the system's hostname.
- Explanation: The `hostname` command shows the system's hostname or can be used to set a new hostname.
- Example: `$ hostname`
- Output Example: `hostname.example.com`
8. **`ifup` / `ifdown`**: Bring up or down a network interface.
- Explanation: The `ifup` command is used to bring up a network interface, while `ifdown` is used to bring it down.
- Example: `$ sudo ifup eth0`
- Output Example: The network interface is brought up.
9. **`ifquery`**: Query network interface configuration.
- Explanation: The `ifquery` command displays detailed configuration information for a network interface.
- Example: `$ ifquery eth0`
- Output Example:
auto eth0
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
10. **`route`**: Display or manipulate the IP routing table.
- Explanation: The `route` command shows the IP routing table and allows you to manage routes.
- Example: `$ route -n`
- Output Example:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
11. **`iwconfig`**: Display wireless network interface configuration.
- Explanation: The `iwconfig` command shows the configuration of wireless network interfaces.
- Example: `$ iwconfig wlan0`
- Output Example:
wlan0 IEEE 802.11 ESSID:"MyWiFi"
Mode:Managed Frequency:2.462 GHz Access Point: XX:XX:XX:XX:XX:XX
Bit Rate=54 Mb/s Tx-Power=15 dBm
...
12. **`nmcli`**: Command-line tool for NetworkManager.
- Explanation: The `nmcli` command provides a command-line interface to manage network connections using NetworkManager.
- Example: `$ nmcli connection show`
- Output Example:
NAME UUID TYPE DEVICE
Wired 5a63ac99-0d12-4f16-82c1-38a90e717702 ethernet eth0
MyWiFi f48aa62e-89f3-4f25-aa92-1f746cf54a1b wifi wlan0
13. **`sshd`**: SSH server daemon.
- Explanation: The `sshd` daemon is responsible for running the SSH (Secure Shell) server.
- Example: N/A
- Output Explanation: The `sshd` daemon listens for incoming SSH connections on port 22.
14. **`ssh`**: Securely connect to a remote server.
- Explanation: The `ssh` command allows you to securely log in to a remote server over the network.
- Example: `$ ssh user@remotehost`
- Output Example: Establishes a secure SSH connection to the remote host.
15. **`scp`**: Securely copy files between hosts.
- Explanation: The `scp` command is used to securely copy files between a local and a remote host over SSH.
- Example: `$ scp file.txt user@remotehost:/path`
- Output Example: Copies `file.txt` from the local system to the remote host.
16. **`ftp`**: File Transfer Protocol client.
- Explanation: The `ftp` command allows interactive file transfers between a local and a remote host using FTP.
- Example: `$ ftp ftp.example.com`
- Output Explanation: The `ftp` session starts, and you can interactively transfer files.
17. **`wget`**: Download files from the web.
- Explanation: The `wget` command is used to download files from the internet using HTTP, HTTPS, or FTP.
- Example: `$ wget https://example.com/file.txt`
- Output Explanation: Downloads `file.txt` from the given URL.
18. **`curl`**: Transfer data with URLs.
- Explanation: The `curl` command is used to transfer data to or from a server using various protocols (HTTP, FTP, etc.).
- Example: `$ curl https://example.com`
- Output Explanation: Displays the HTML content of the specified URL.
19. **`netcat`**: Networking utility for reading or writing data across network connections.
- Explanation: The `netcat` (or `nc`) command is a versatile tool for reading or writing data across network connections.
- Example: `$ echo "Hello, World!" | nc -l -p 12345`
- Output Explanation: Listens on port 12345 and outputs "Hello, World!" to the connecting client.
20. **`nmap`**: Network exploration and security auditing.
- Explanation: The `nmap` command is used to discover devices and services on a network, and perform security assessments.
- Example: `$ nmap -sP 192.168.1.0/24`
- Output Explanation: Scans the specified IP range for active hosts using ping.
21. **Display All Active Network Connections**:
- Explanation: This command shows all active network connections, including both listening and established connections.
- Example: `$ netstat -a`
- Output Example:
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::443 :::* LISTEN
...
22. **Show Network Statistics by Protocol**:
- Explanation: This command displays network statistics based on different protocols (TCP, UDP, etc.).
- Example: `$ netstat -s`
- Output Example: Displays a comprehensive list of network statistics for each protocol, including packets sent, received, errors, and more.
23. **Display Listening Ports with Process Information**:
- Explanation: This command shows all listening ports along with the associated process and their process IDs (PIDs).
- Example: `$ netstat -tuln`
- Output Example:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::80 :::* LISTEN
udp 0 0 0.0.0.0:5353 0.0.0.0:*
24. **Show Network Interfaces and Their IP Addresses**:
- Explanation: This command displays IP addresses and other information for all network interfaces.
- Example: `$ netstat -i`
- Output Example:
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 123456 0 0 0 78901 0 0 0 BMRU
lo 65536 9876 0 0 0 9876 0 0 0 LRU
25. **Display Routing Table**:
- Explanation: This command shows the routing table, including destination, gateway, netmask, and flags.
- Example: `$ netstat -r`
- Output Example:
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
TAR command to create, extract, compress, and manage files and directories in tar archive format
`tar` command to create, extract, compress, and manage files and directories in tar archive format. The outputs show the results of each operation.
1. Create a Tar Archive:
- Explanation: This command creates a tar archive of files and directories.
- Example: `$ tar -cvf archive.tar file1.txt dir1`
- Output Example:
file1.txt
dir1/
dir1/file2.txt
dir1/file3.txt
2. Extract Files from a Tar Archive:
- Explanation: This command extracts files from a tar archive.
- Example: `$ tar -xvf archive.tar`
- Output Example:
file1.txt
dir1/
dir1/file2.txt
dir1/file3.txt
3. Create a Compressed Tar Archive (gzip):
- Explanation: This command creates a compressed tar archive using gzip compression.
- Example: `$ tar -cvzf archive.tar.gz file1.txt dir1`
- Output Example: Creates `archive.tar.gz` with compressed files.
4. Extract Files from a Compressed Tar Archive (gzip):
- Explanation: This command extracts files from a compressed tar archive using gzip.
- Example: `$ tar -xzvf archive.tar.gz`
- Output Example: Extracts files from `archive.tar.gz`.
5. Create a Compressed Tar Archive (bzip2):
- Explanation: This command creates a compressed tar archive using bzip2 compression.
- Example: `$ tar -cvjf archive.tar.bz2 file1.txt dir1`
- Output Example: Creates `archive.tar.bz2` with compressed files.
6. Extract Files from a Compressed Tar Archive (bzip2):
- Explanation: This command extracts files from a compressed tar archive using bzip2.
- Example: `$ tar -xjvf archive.tar.bz2`
- Output Example: Extracts files from `archive.tar.bz2`.
7. List Contents of a Tar Archive:
- Explanation: This command lists the contents of a tar archive.
- Example: `$ tar -tf archive.tar`
- Output Example:
file1.txt
dir1/
dir1/file2.txt
dir1/file3.txt
8. Add Files to an Existing Tar Archive:
- Explanation: This command adds files to an existing tar archive.
- Example: `$ tar -rvf archive.tar file4.txt`
- Output Example:
file4.txt
9. Update Files in an Existing Tar Archive:
- Explanation: This command updates files in an existing tar archive if they are newer.
- Example: `$ tar -uvf archive.tar file1.txt`
- Output Example:
file1.txt
10. Extract a Specific File from a Tar Archive:
- Explanation: This command extracts a specific file from a tar archive.
- Example: `$ tar -xvf archive.tar file1.txt`
- Output Example:
file1.txt
Package Installation with YUM Command
various tasks you can perform using the `yum` package manager in Linux, including package installation, updates, removal, repository management, and more. The outputs demonstrate the outcomes of each operation.
1. Install a Package:
- Explanation: This command installs a package from the available repositories.
- Example: `$ sudo yum install package-name`
- Output Example:
Installed:
package-name.x86_64 0:version
Complete!
2. Search for a Package:
- Explanation: This command searches for packages based on a keyword.
- Example: `$ yum search keyword`
- Output Example:
======================================================= N/S matched: keyword =======================================================
package1.x86_64 : Description of package 1
package2.x86_64 : Description of package 2
3. List Installed Packages:
- Explanation: This command lists all installed packages on the system.
- Example: `$ yum list installed`
- Output Example:
Installed Packages
package1.x86_64 version
package2.x86_64 version
4. Update System Packages:
- Explanation: This command updates all installed packages to the latest available versions.
- Example: `$ sudo yum update`
- Output Example:
Updated:
package1.x86_64 0:version
package2.x86_64 0:version
Complete!
5. Remove a Package:
- Explanation: This command removes an installed package from the system.
- Example: `$ sudo yum remove package-name`
- Output Example:
Removed:
package-name.x86_64 0:version
Complete!
6. Info about a Package:
- Explanation: This command provides detailed information about a package.
- Example: `$ yum info package-name`
- Output Example:
Available Packages
Name : package-name
Version : version
7. Enable a Repository:
- Explanation: This command enables a disabled repository.
- Example: `$ sudo yum config-manager --enable repository-name`
- Output Example: The specified repository is enabled.
8. Disable a Repository:
- Explanation: This command disables an enabled repository.
- Example: `$ sudo yum config-manager --disable repository-name`
- Output Example: The specified repository is disabled.
9. Clean YUM Cache:
- Explanation: This command cleans the local YUM cache.
- Example: `$ sudo yum clean all`
- Output Example: The YUM cache is cleaned.
10. List Available Repositories:
- Explanation: This command lists all available YUM repositories.
- Example: `$ yum repolist`
- Output Example:
repo-id repo-name status
repository1 Repository 1 enabled
repository2 Repository 2 enabled
SSH commands for securely accessing remote hosts, copying files, key management, port forwarding, and more
1. SSH into a Remote Host:
- Explanation: This command establishes a secure SSH connection to a remote host.
- Example: `$ ssh username@remote-host`
- Output Example: Establishes a secure shell session on the remote host.
2. Specify a Different SSH Port:
- Explanation: This command connects to a remote host using a non-default SSH port.
- Example: `$ ssh -p port-number username@remote-host`
- Output Example: Establishes an SSH connection on the specified port.
3. Copy Files to Remote Host using SCP:
- Explanation: This command securely copies files to a remote host using SCP (Secure Copy).
- Example: `$ scp local-file username@remote-host:/path/to/destination`
- Output Example: Copies `local-file` to the specified destination on the remote host.
4. Copy Files from Remote Host using SCP:
- Explanation: This command securely copies files from a remote host using SCP.
- Example: `$ scp username@remote-host:/path/to/remote-file local-destination`
- Output Example: Copies `remote-file` from the remote host to the local destination.
5. Generate SSH Key Pair:
- Explanation: This command generates an SSH key pair (public and private keys).
- Example: `$ ssh-keygen`
- Output Example: Generates SSH key pair files: `id_rsa` (private key) and `id_rsa.pub` (public key).
6. Add SSH Key to Remote Host:
- Explanation: This command adds your SSH public key to the authorized keys file on the remote host, allowing passwordless authentication.
- Example: `$ ssh-copy-id username@remote-host`
- Output Example: Copies your SSH public key to the remote host's authorized keys.
7. Forward Local Port to Remote Host:
- Explanation: This command forwards a local port to a remote host using SSH tunneling.
- Example: `$ ssh -L local-port:remote-destination:remote-port username@remote-host`
- Output Example: Forwards the local port to the remote host and port.
8. Reverse SSH Tunneling:
- Explanation: This command creates a reverse SSH tunnel to a remote host, allowing remote access to your local machine.
- Example: `$ ssh -R remote-port:local-destination:local-port username@remote-host`
- Output Example: Creates a reverse SSH tunnel to the remote host.
9. Execute Remote Command:
- Explanation: This command executes a command on a remote host without entering an interactive shell.
- Example: `$ ssh username@remote-host "command"`
- Output Example: Executes the specified command on the remote host.
10. SFTP (Secure File Transfer Protocol):
- Explanation: This command opens an interactive SFTP session for secure file transfer.
- Example: `$ sftp username@remote-host`
- Output Example: Opens an SFTP session for transferring files between local and remote hosts.