Linux Commands: gunzip, hostnamectl, iptables, and More

The gunzip Command

  • The gunzip command decompresses files compressed by the gzip command.

  • It replaces files ending with .gz, -gz, .z, -z, or _z (case-insensitive) that begin with the correct magic number with an uncompressed file without the original extension.

  • .tgz and .taz extensions are recognized as shorthands for .tar.gz and .tar.Z respectively.

  • Examples:

    • gunzip filename.gz: Uncompresses filename.gz.
    • gunzip -r directory_name/: Recursively uncompresses content inside a directory, matching extensions accepted by gunzip.
    • gunzip -S .tgz *: Uncompresses all files in the current directory with the .tgz suffix.
    • gunzip -l file_1 file_2: Lists compressed and uncompressed sizes, compression ratio, and uncompressed names of input compressed files.
  • Syntax:

    gunzip [ -acfhklLnNrtvV ] [-S suffix] [ name ... ]
    
  • Flags:

    • -c, --stdout: Writes to standard output, keeps original files unchanged.
    • -h, --help: Displays help information.
    • -k, --keep: Keeps (doesn't delete) input files.
    • -l, --list: Lists compressed file contents.
    • -q, --quiet: Suppresses all warnings.
    • -r, --recursive: Operates recursively on directories.
    • -S, --suffix=SUF: Uses suffix SUF on compressed files.
    • --synchronous: Synchronous output (safer if the system crashes, but slower).
    • -t, --test: Tests compressed file integrity.
    • -v, --verbose: Verbose mode.
    • -V, --version: Displays the version number.

The hostnamectl Command

  • The hostnamectl command is used to control the Linux system hostname and its related settings.

  • It can change the hostname without directly editing the /etc/hostname file.

  • Syntax:

    $ hostnamectl [OPTIONS...] COMMAND ...
    
  • Commands:

    • status: Checks the current hostname settings.
    • set-hostname NAME: Sets the system hostname.
    • set-icon-name NAME: Sets the icon name for the host.
  • Examples:

    • hostnamectl or hostnamectl status: Views the current hostnames.
    • hostnamectl set-hostname myhostname --static: Changes the static hostname to myhostname (may require root access).
    • hostnamectl set-hostname myotherhostname --transient: Sets or changes a transient hostname.
    • hostnamectl set-hostname "prettyname" --pretty: Sets the pretty hostname (name must be in double quotes).

The iptables Command

  • The iptables command is used to set up and maintain tables for the Netfilter firewall for IPv4 in the Linux kernel.

  • It matches packets with rules defined in tables and takes specified actions on a match.

  • Syntax:

    iptables --table TABLE -A/-C/-D... CHAIN rule --jump Target
    
  • Examples:

    • iptables [-t table] --append [chain] [parameters]: Appends to the chain provided in parameters.
    • iptables -t filter --append INPUT -j DROP: Drops all traffic coming on any port.
  • Flags:

    • -C: Checks if a rule is present in the chain; returns 0 if the rule exists, 1 otherwise.
    • -A: Appends to the chain provided in parameters.

The netstat Command

  • netstat stands for Network Statistics.
  • It displays current network connections, networking protocol statistics, and various interfaces.
  • Check installation: netstat -v
  • Install if not available: sudo apt install net-tools
  • Use Cases and Examples:
    • netstat -nr: Shows the routing table detail on the terminal.
    • netstat -i: Shows statistics for the currently configured network interfaces.
    • netstat -tunlp: Provides a list of networks, their current states, and their associated ports.
    • netstat -at: Gets the list of all TCP port connections.
    • netstat -au: Gets the list of all UDP port connections.
    • netstat -l: Gets the list of all active connections.

The lsof Command

  • The lsof command shows file information for all files opened by a running process; stands for