Skip to content

Crack SDE

Most of the content are generated by AI, with human being reviewed, edited, and revised

Menu
  • Home
  • Daily English Story
  • Tech Interviews
  • Cloud Native
  • DevOps
  • Artificial Intelligence
Menu

Linux Commands Examples

Posted on 12/08/202312/10/2023 by user

nslookup

nslookup is a cross-platform command-line tool for querying the Domain Name System (DNS).

PurposeCommand
Basic DNS Lookupnslookup example.com
Find Mail Servers for a Domainnslookup -type=mx example.com
Query Specific DNS Servernslookup example.com ns1.isp.com
Find Authoritative Name Serversnslookup -type=ns example.com
Reverse DNS Lookup (IP to Hostname)nslookup 192.0.2.1
Find Text (TXT) Recordsnslookup -type=txt example.com
Query with Non-Interactive Modeecho server 8.8.8.8 | nslookup example.com
Find Start of Authority (SOA) Recordsnslookup -type=soa example.com
List All Records for a Domain (Zone Transfer)nslookup -type=any example.com
Set Query Time-outnslookup -timeout=10 example.com
Display Detailed Query Informationnslookup -debug example.com

ifconfig

ifconfig (interface configuration) is a command-line utility in Unix-like operating systems to configure, control, and query TCP/IP network interface parameters. It is often used for network troubleshooting and setup. Below is a table listing some common ifconfig commands used for daily debugging, along with their purposes:

PurposeCommand
Display all active interfacesifconfig
Display a specific interface (e.g., eth0)ifconfig eth0
Enable an interface (e.g., eth0)sudo ifconfig eth0 up
Disable an interface (e.g., eth0)sudo ifconfig eth0 down
Assign an IP address to an interfacesudo ifconfig eth0 192.168.1.5
Set a netmask for an interfacesudo ifconfig eth0 netmask 255.255.255.0
Change the MAC address of an interfacesudo ifconfig eth0 hw ether 02:01:02:03:04:05
Add a new alias to an interfacesudo ifconfig eth0:0 192.168.1.6
Remove an alias from an interfacesudo ifconfig eth0:0 down

Note: Modern Linux systems have largely replaced ifconfig with the ip command from the iproute2 package, as ifconfig is considered deprecated. However, ifconfig is still in use in many systems and scenarios, especially in older or legacy systems.

ip

The ip command in Linux is a versatile tool for managing and configuring network interfaces, routing, and tunnels. It’s part of the iproute2 package and is used to replace many of the old network configuration tools like ifconfig, route, netstat, etc. Here’s a table with some common ip command examples used for daily debugging:

PurposeCommand
Display all network interfacesip link show
Display a specific interface (e.g., eth0)ip addr show eth0
Enable an interface (e.g., eth0)ip link set eth0 up
Disable an interface (e.g., eth0)ip link set eth0 down
Assign an IP address to an interfaceip addr add 192.168.1.5/24 dev eth0
Remove an IP address from an interfaceip addr del 192.168.1.5/24 dev eth0
Display the routing tableip route show
Add a routeip route add 192.168.1.0/24 via 192.168.1.1
Delete a routeip route del 192.168.1.0/24
Display ARP tableip neigh show
Change the MAC address of an interfaceip link set dev eth0 address 02:01:02:03:04:05
Display statistics for all interfacesip -s link
Monitor network interfaces for new eventsip monitor

These commands provide a powerful set of tools for managing network interfaces and configurations on a Linux system. The ip command is more consistent and offers more features compared to the older ifconfig, making it the preferred choice in modern Linux distributions.

traceroute

The traceroute command in Linux is a network diagnostic tool used to trace the path that an IP packet takes to reach a destination. It displays each hop along the route and measures transit delays of packets across the network. This can be useful for identifying network bottlenecks or misconfigurations. Here’s a table with some common traceroute command examples used for daily debugging:

PurposeCommand
Basic traceroute to a destinationtraceroute example.com
Specify the maximum number of hopstraceroute -m 30 example.com
Use ICMP ECHO instead of UDP datagramstraceroute -I example.com
Specify the size of the probing packetstraceroute --mtu example.com
Perform a traceroute with a specific source IPtraceroute -s [source_ip] example.com
Use a specific port numbertraceroute -p 80 example.com
Perform a verbose traceroutetraceroute -v example.com
Use TCP SYN for probes instead of UDP or ICMPtraceroute -T example.com
Specify the wait time for a response (in seconds)traceroute -w 5 example.com
Perform traceroute with a specific interfacetraceroute -i eth0 example.com

These commands can help network administrators and IT professionals diagnose routing issues and understand the path traffic takes to reach a destination. Note that the behavior and options of traceroute can vary slightly between different Linux distributions and versions.

ping

The ping command in Linux is a simple yet powerful tool used to test the reachability of a host on an Internet Protocol (IP) network. It measures the round-trip time for messages sent from the originating host to a destination computer and echoes back. Here’s a table with some common ping command examples used for daily debugging:

PurposeCommand
Basic ping to a destinationping example.com
Ping a specific number of times (e.g., 4 times)ping -c 4 example.com
Ping with an interval of seconds (e.g., 2 sec)ping -i 2 example.com
Ping with a specific packet size (e.g., 100 bytes)ping -s 100 example.com
Ping with a deadline (stop after N seconds)ping -w 10 example.com
Flood ping (for testing, requires privileges)sudo ping -f example.com
Ping and record the route takenping -R example.com
Ping with a specific source IP or interfaceping -I [source_ip/interface] example.com
Ping and bypass routing table, use direct routeping -r example.com
Verbose outputping -v example.com

These commands are useful for checking connectivity, response times, and the general health of a network connection between the host and a destination. However, it’s important to use them responsibly, especially commands like flood ping (ping -f), as they can generate a lot of traffic and might be considered a denial-of-service attack if used improperly.

netstat

The netstat command in Linux is a versatile networking tool used for examining network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. It’s useful for network troubleshooting and performance measurement. Here’s a table with some common netstat command examples used for daily debugging:

PurposeCommand
Display all ports (listening and non-listening)netstat -a
Display all listening portsnetstat -l
Display listening TCP portsnetstat -lt
Display listening UDP portsnetstat -lu
Show statistics for all portsnetstat -s
Display PID and program namesnetstat -p
Show network interfaces and statisticsnetstat -i
Display routing tablenetstat -r
Display masquerade connectionsnetstat -M
Continuously display network connectionsnetstat -c
Display both IPv4 and IPv6 statisticsnetstat -46
Show the number of network connections by statenetstat -an | grep ESTABLISHED | wc -l

Note: Modern Linux systems have started to replace netstat with more powerful and flexible tools like ss and ip from the iproute2 package. While netstat is still widely used and available in many systems, it’s considered somewhat deprecated, and users are encouraged to learn the newer tools for future compatibility.

dig

The dig (Domain Information Groper) command in Linux is a powerful tool for querying DNS name servers. It’s used for fetching DNS information and troubleshooting DNS problems. Here’s a table with some common dig command examples used for daily debugging:

PurposeCommand
Basic DNS lookupdig example.com
Query a specific DNS record type (e.g., MX)dig example.com MX
Query a specific DNS serverdig @ns1.example.com example.com
Perform a reverse DNS lookupdig -x 8.8.8.8
Query with short answer formatdig example.com +short
Query with detailed answer (trace the query)dig example.com +trace
Get DNSSEC informationdig example.com +dnssec
Query all record typesdig example.com ANY
Specify a different query timeoutdig example.com +time=5
Perform multiple queries at oncedig example.com example.org
Check the version of the DNS serverdig @ns1.example.com version.bind CHAOS TXT
Query with additional statisticsdig example.com +stats

dig is a part of the BIND DNS software suite and is a more advanced and flexible tool compared to older tools like nslookup. It’s widely used by system administrators and network engineers for DNS troubleshooting and analysis.

route

The route command in Linux is used to view and manipulate the IP routing table. Although it’s been largely superseded by the ip route command from the iproute2 package, route is still used in many systems for network configuration and troubleshooting. Here’s a table with some common route command examples used for daily debugging:

PurposeCommand
Display the current routing tableroute -n
Add a new routesudo route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
Delete a routesudo route del -net 192.168.1.0 netmask 255.255.255.0
Add a default gatewaysudo route add default gw 192.168.1.1
Delete a default gatewaysudo route del default gw 192.168.1.1
Add a route for a specific hostsudo route add -host 192.168.1.5 gw 192.168.1.1
Delete a route for a specific hostsudo route del -host 192.168.1.5
Add a route for an interfacesudo route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0
Display the routing table for IPv6route -A inet6

Please note that the route command requires root privileges for adding or deleting routes, hence the use of sudo in the examples. Also, as mentioned earlier, the ip command is now the preferred tool for most routing tasks in modern Linux distributions due to its advanced features and consistent syntax.

hostname

The hostname command in Linux is used to show or set the system’s host name. It’s a simple yet essential tool for system administrators, particularly when managing networks and server settings. Here’s a table with some common hostname command examples used for daily debugging:

PurposeCommand
Display the current hostnamehostname
Set a new hostnamesudo hostname new-name
Display the DNS domain namehostname -d
Display the FQDN (Fully Qualified Domain Name)hostname -f
Display the network node hostnamehostname -n
Display the IP address of the hosthostname -I
Display all network addresses of the hosthostname -i
Display the short hostname (first part of FQDN)hostname -s
Display the alias name of the hosthostname -a

Remember that changing the hostname with hostname new-name doesn’t persist across reboots. To permanently change the hostname on most systems, you need to edit system-specific configuration files (like /etc/hostname and /etc/hosts on many Linux distributions) and then use the hostname command or restart the system.

Also, the way the hostname command functions can vary slightly between different Linux distributions and versions, so it’s always a good idea to check the man page (man hostname) for the most accurate information on your specific system.

lsof

lsof stands for “List Open Files” and is a command in Unix and Linux that provides information about files opened by processes. An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream, or a network file (like a socket, an NFS file, or an Internet address).

Here’s a table with some common lsof command examples used for daily debugging:

PurposeCommand
List all open fileslsof
List files opened by a specific userlsof -u username
List open files by a specific processlsof -p PID
List open files on a specific portlsof -i :port
List all network connectionslsof -i
List all TCP or UDP connectionslsof -i tcp or lsof -i udp
List files in a specific directorylsof +D /path/to/dir
List open files based on file nameslsof /path/to/file
List files opened by a specific commandlsof -c command_name
List open files associated with a file systemlsof /dev/sda1
List open files with a specific network protocollsof -i tcp or lsof -i udp
List all open files by all users except onelsof -u ^username

lsof is an extremely powerful tool for system administrators, as it can be used to track down files that are being used by processes, which is helpful in diagnosing various system issues, such as why a file system cannot be unmounted or what is causing network traffic. Remember that many lsof commands require root privileges to provide complete and accurate information.

du

The du (disk usage) command in Linux is used to estimate file and directory space usage. It’s a crucial tool for managing disk space, identifying which files and directories are consuming the most space, and for general storage housekeeping. Here’s a table with some common du command examples used for daily debugging:

PurposeCommand
Display the disk usage of a directorydu /path/to/directory
Display disk usage in human-readable formatdu -h /path/to/directory
Summarize disk usage of each file in a directorydu -a /path/to/directory
Display disk usage up to N levels deepdu --max-depth=N /path/to/directory
Display total disk usage of a directorydu -s /path/to/directory
Display disk usage in bytesdu -b /path/to/directory
Display disk usage of all files and directoriesdu -ah
Display disk usage of all .jpg filesdu -ch *.jpg
Sort the output by size in human-readable formatdu -hs * | sort -h
Exclude files matching a patterndu --exclude="*.log" /path/to/directory

The du command is highly versatile and can be combined with other commands like sort for more advanced disk usage analysis. It’s a fundamental tool for Linux system administrators and users for monitoring and managing disk space usage.

df

The df (disk free) command in Linux is used to report file system disk space usage. It provides an overview of the available and used disk space on all mounted filesystems. Here’s a table with some common df command examples used for daily debugging:

PurposeCommand
Display all file systems disk space usagedf
Display in human-readable format (e.g., MB, GB)df -h
Display the file system typedf -T
Include dummy file systemsdf -a
Display inodes usage instead of block usagedf -i
Display disk space usage of a specific file systemdf /path/to/directory
Display disk space usage in 1K blocksdf -k
Display disk space usage in megabytesdf -m
Display disk space usage for a specific typedf -t ext4
Exclude a specific type of file systemdf -x tmpfs

The df command is essential for monitoring overall disk space usage, ensuring that systems do not run out of space, and for managing disk resources effectively. It’s one of the most basic and frequently used commands for system administrators and users alike.

To list large files in a Linux system sorted by size, you can use the find, du, and sort commands in combination. Here’s a command that searches for large files in a specific directory and sorts them by size in a human-readable format:

find /path/to/search -type f -exec du -h {} + | sort -rh

Breaking down the command:

  • find /path/to/search: This part of the command specifies the find command to search in /path/to/search directory. Replace /path/to/search with the specific directory you want to search in, or use . to represent the current directory.
  • -type f: This option tells find to look for files only (not directories).
  • -exec du -h {} +: For each file found, exec executes the du -h command. du -h displays the disk usage in a human-readable format (e.g., K, M, G). {} is replaced by the name of each file found, and + at the end executes du on multiple files at once for efficiency.
  • | sort -rh: The output from du is then piped (|) into the sort command. The -rh option tells sort to handle the sizes in a human-readable format (-h) and sort them in reverse order (-r), which puts the largest files at the top of the list.

If you want to search the entire filesystem and you have permission to access all directories, you can run this command starting from the root (/):

sudo find / -type f -exec du -h {} + | sort -rh

Remember that searching the entire filesystem can take a considerable amount of time and might produce a very long list. It’s often more practical to target specific directories known for containing large files.

Expand A Directory By Mounting New Volume

Expanding the storage capacity of a directory in Linux by mounting a new volume to that directory, need to use the new volume to increase the available space for that directory.

  1. Identify the New Volume: First, you need to identify the device name of the new volume. You can use lsblk or fdisk -l to list all storage devices and their partitions. The new volume might be something like /dev/sdb1, /dev/sdc1, etc.
  2. Create a Filesystem (if necessary): If the new volume doesn’t have a filesystem, you need to create one. For example, to create an ext4 filesystem on /dev/sdb1, use:
   sudo mkfs.ext4 /dev/sdb1

Warning: This step will erase existing data on the partition. Ensure that you’re operating on the correct device and that no important data is lost.

  1. Mount the Volume Temporarily: Before making it permanent, you might want to mount the volume temporarily to ensure everything works correctly.
   sudo mount /dev/sdb1 /path/to/directory

Replace /path/to/directory with the path to the directory you want to expand. This directory should exist.

  1. Test the Setup: Check if the volume is mounted correctly and if you can read/write data as expected.
  2. Unmount the Volume: If everything is fine, unmount the volume before proceeding to permanent mounting.
   sudo umount /path/to/directory
  1. Edit /etc/fstab for Permanent Mounting: To make the mounting permanent, you need to add an entry to the /etc/fstab file.
  • Open /etc/fstab with a text editor, for example: sudo nano /etc/fstab
  • Add a line at the end of the file: /dev/sdb1 /path/to/directory ext4 defaults 0 2 Replace /dev/sdb1 with your device name, /path/to/directory with your directory path, and ext4 with the filesystem type you used.
  1. Mount All Filesystems: After saving the changes to /etc/fstab, you can mount all filesystems mentioned in it without rebooting:
   sudo mount -a
  1. Verify the Mount: Finally, verify that the new volume is mounted correctly:
   df -h /path/to/directory

This process effectively “expands” the chosen directory by mounting a new volume to it, thereby increasing its available storage capacity. Remember to back up important data before performing operations like formatting or editing /etc/fstab, as mistakes can lead to data loss.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related

1

Recent Posts

  • LC#622 Design Circular Queue
  • Started with OpenTelemetry in Go
  • How Prometheus scrap works, and how to find the target node and get the metrics files
  • How to collect metrics of container, pods, node and cluster in k8s?
  • LC#200 island problem

Recent Comments

  1. another user on A Journey of Resilience

Archives

  • May 2025
  • April 2025
  • February 2025
  • July 2024
  • April 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023

Categories

  • Artificial Intelligence
  • Cloud Computing
  • Cloud Native
  • Daily English Story
  • Database
  • DevOps
  • Golang
  • Java
  • Leetcode
  • Startups
  • Tech Interviews
©2025 Crack SDE | Design: Newspaperly WordPress Theme
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}