Chapter 6

Commands

A random assortment of commands for reference

Subsections of Commands

Linux: ARP Command

Note

The net-tools linux package is an old one, and provides a large number of standard commands:

  • arp
  • ifconfig
  • iptunnel
  • iwconfig
  • nameif
  • netstat

net-tools was deprecated in 2011. While is it still available it is advisable to move on to more modern commands.

ARP commands are useful for showing the ARP table in use by the OS. Certainly the ARP table will contain the MAC addresses of known network nodes.

ip n
ip n
ip n
ip n
Tip

n for neighbor


Install the ip command with:

dnf -y install iproute2
apt -y install iproute2
apk add iproute2
pacman -Syu iproute2

Linux: Default Gateway

Note

The net-tools linux package is an old one, and provides a large number of standard commands:

  • arp
  • ifconfig
  • iptunnel
  • iwconfig
  • nameif
  • netstat

net-tools was deprecated in 2011. While is it still available it is advisable to move on to more modern commands.

The standard command for showing IP address in Linux is ip a. ip also provides an option to view configured gateways:

ip r
ip r
ip r
ip r

Here is an example of adding a default gateway:

ip route add default via 192.168.1.1

If your distribution or container is missing this command you can add it with:

dnf -y install iproute2
apt -y install iproute2
apk add iproute2
pacman -Syu iproute2

Linux: Disk Space Usage

Simple command to show disk space used for all connected file systems, in human readable sizes instead of bytes

df -h
df -h
df -h
df -h

Linux: Network Connection Statistics

Note

The net-tools linux package is an old one, and provides a large number of standard commands:

  • arp
  • ifconfig
  • iptunnel
  • iwconfig
  • nameif
  • netstat

net-tools was deprecated in 2011. While is it still available it is advisable to move on to more modern commands.

Troubleshooting network applications is often aided by reviewing the applications on the system with open ports, waiting for network traffic to connect. To do this in a Linux terminal, run this command:

ss -tulpn
ss -tulpn
ss -tulpn
ss -tulpn

ss options mean:

SwitchMeaning
tshow TCP connections
ushow UDP connections
lshow listening sockets only
pinclude PID for listening processes
nfaster output by skipping resolving IP addresses to hostnames

Install the ss command with:

dnf -y install iproute2
apt -y install iproute2
apk add iproute2
pacman -Syu iproute2

Linux: Ping Command

Though many commands in Linux don’t match to Windows, in the case of ping it does:

ping <ip address|hostname>
ping <ip address|hostname>
ping <ip address|hostname>
ping <ip address|hostname>

If missing from your distribution or container, install with:

dnf -y install iputils
apt -y install iputils
apk add iputils-ping
pacman -Syu iputils

Linux: PS Command

In Linux use the ‘ps’ command to list running processes. Sometimes I’ve found this basic command missing from Linux containers.

ps aux
ps aux
ps aux
ps aux


If missing from your distribution or container, install with:

dnf -y install procps
apt -y install procps
apk add procps
pacman -Syu procps-ng

Linux: Resolve DNS Names to IP Address

Another part of standard troubleshooting for network connections is testing your DNS server connectivity and name resolution. In Windows this is command would be nslookup; in Linux it’s:

drill <hostname>
drill <hostname>
drill <hostname>
drill <hostname>
Note

dig is an older command for doing DNS lookups. It considered legacy and has been replaced with drill in many distributions

dnf -y install bind-utils
apt -y install dnsutils
apk add bind-tools
pacman -Syu bind

If missing from your distribution or container, install with:

dnf -y install ldns
apt -y install ldnsutils
apk add drill
pacman -Syu ldns

Linux: Setting Static or Dynamic IP Address

Virtually no computer used today would not have an IP address of some kind assigned to it. Here’s how to assign IP address settings in various Linux OS flavors. See the following sections for commands to show the current IP settings:

ip a
ip a
ip a
ip a
ip r
ip r
ip r
ip r
drill <hostname>
drill <hostname>
drill <hostname>
drill <hostname>
ping <ip address|hostname>
ping <ip address|hostname>
ping <ip address|hostname>
ping <ip address|hostname>

Direct Configuration File Method

The various Linux OS families have different ways to set their IP address (on the command line), typically through a configuration file and a restart of a system service.

vi /etc/NetworkManager/system-connections/<IFACE_NAME>.nmconnection
systemctl restart NetworkManager
vi /etc/network/interfaces
systemctl restart networking
vi /etc/network/interfaces
vi /etc/resolv.conf
/etc/init.d/networking restart
vi /etc/systemd/network/10-net0.link
> [Match]
> PermanentMACAddress=<interface MAC address>
>
> [Link]
> Name=net0
>
> [Network]
> Address=<address>
> Gateway=<gateway>
> DNS=<dns1>
> DNS=<dns2>

Ubuntu

In this case, like many others, even though Ubuntu is a Debian derrivative it doesn’t follow Debian’s example, and has to do it it’s own ‘special’ way:

vi /etc/netplan/<interface>_config.yaml
netplan apply

NetworkManager Method

Many distributions support various network managers - command line tools to consolidate and simplify the commands to manage IP address settings. Several distributions support NetworkManager and I’ve made notes here to show how to install NetworkManager and the commands to set static IP values

dnf -y install NetworkManager
systemctl enable --now NetworkManager
nmcli connection modify <iface name> ipv4.gateway <gateway ip>
nmcli connection modify <iface name> ipv4.address <ip address>
nmcli connection modify <iface name> ipv4.dns <dns ip address>
nmcli connection up <iface name>
apt-get install network-manager
systemctl enable --now NetworkManager
nmtui
apk add networkmanager
rc-service networkmanager start
rc-update add networkmanager default
adduser <your username> plugdev #you will need to relog to apply the new group membership
nmtui
pacman -Syu networkmanager
systemctl --now enable NetworkManager.service
nmtui

Linux: Show IP Address

Note

The net-tools linux package is an old one, and provides a large number of standard commands:

  • arp
  • ifconfig
  • iptunnel
  • iwconfig
  • nameif
  • netstat

net-tools was deprecated in 2011. While is it still available it is advisable to move on to more modern commands.

Another standard command needed for common troubleshooting in every OS - in Linux you show IP addresses with:

ip a
ip a
ip a
ip a

If missing from your distribution or container, install with:

dnf -y install iproute2
apt -y install iproute2
apk add iproute2
pacman -Syu iproute2

Linux: Vi Command

An essential function for any computer system is file editing. In Linux Vi (or Vim being the enhanced version) is the standard command line text editor.

vim
vim
vim
vim

If missing from your distribution or container, install with:

dnf -y install vim
apt -y install vim
apk add vim
pacman -Syu vim

Linux: View DNS Server

While Linux provides straight forward command for viewing the ip address configured on your host (ip or ifconfig), none of these commands include information about your DNS servers. To view your DNS servers use:

cat /etc/resolv.conf
cat /etc/resolv.conf
cat /etc/resolv.conf
cat /etc/resolv.conf

DNS servers are typically managed through the network manager for your distribution, or directly through network configuration files

Red Hat Family OS's: Extending Root Partition

Windows easily allows extending a partition, while the disk is in use or “online”. Extending a partition is a common task when managing guests in a virtual environment. This process has always been straight forward in Windows, but the Linux commands had eluded me for some time.

Fedora 30 maintains the root partitions as a volume group - a composition from several partitions made into one logical volume. So the solution is to create a new partition, empty and of the size you wish to exapand your root partition by, then add it to the volume group. xvda is the designation for the virtual disk attached to my guest that I’ll be adding the new partition to.

Tip

if your virtual disk needs to be expanded to make free space for the new partition, then do that first using your hypervisor’s controls

Creating the new partition

fdisk /dev/xvda
n  #new partition
p  #primary type partition
[enter]  #default option
[enter]  #default option
[enter]  #default option
w  #write partition table
q  #quit fdisk
Note

The next code section uses the vgextend command to modify a volume group. To list your volume groups use vgdisplay

Adding the new partition xvda3 to the volume group

partprobe
pvcreate /dev/xvda3
vgextend /dev/fedora /dev/xvda3
lvextend -l+100%FREE /dev/fedora/root
fsadm resize /dev/fedora/root
partprobe
pvcreate /dev/xvda3
vgextend /dev/cl /dev/xvda3
lvextend -l+100%FREE /dev/cl/root
fsadm resize /dev/cl/root
partprobe
pvcreate /dev/xvda3
vgextend /dev/rl /dev/xvda3
lvextend -l+100%FREE /dev/rl/root
fsadm resize /dev/rl/root

Check that your free space has increased with:

df -h

The volume group will appear in the listing as:

/dev/mapper/fedora-root
/dev/mapper/cl-root
/dev/mapper/rl-root

Windows: Modifying the Command Line Path

For command line environments the default locations where the terminal will look for executable programs is called the Path. The path may have several directories noted in it, meaning that all of the noted locations will be searched for executable files to match the command that you’re issuing. The purpose of the path is to save system users the time it would take to type a full file path to each executable file they want to run. Path is the reason why, when you type ‘cmd’ or ‘ipconfig’ or any other command, your command runs even though you did not specify C:\Windows\System32 in front of it (which is the directory where many commands live).

When you create your own programs or commands certainly you would like to run them without needing to specify the file path to them, and you can do so by modifying the path variable to include the directory where you store your own programs. Certainly you could also just place your programs in the C:\Windows\System32 folder, however that is not recommended for several reasons:

  • Modifying the contents of this folder (including adding or removing files) requires system administrator permission. Modifying the path variable allows normal system users to enjoy the benefits of path too (though non-admins should modify the non-system version of the path variable instead).
  • The C:\Windows\System32 folder is meant for Windows system files - files that are provided with an installation of Windows. Including your own programs breaks that file organization. Windows updates will also assume that the Windows folder does not contain user files, so future updates or restores could overwrite your files stored in this location.
  • Other file system locations are more suitable, such as: C:\Program Files or C:\Users\<my user>\Programs

As an example, let’s add the C:\Program Files\Scripts directory to the system path variable, by using Windows Powershell. C:\Program Files\Scripts is not located in a user accessible area - you must be a system administrator to modify the contents of the C:\Program Files directory. However, I am the system administrator and I also want to ensure that the script that I am including in this directory is available to all users. If the script were placed in C:\Users\<my user>\Programs (or similar) then only I would be able to access it. However I would use C:\Users\<my user>\Programs or similar if I were working on a system that I did not have administrator permissions to.

Note

The command below comes in two parts: setting the value of the path variable in my local Powershell session, then committing that value to the system path variable permanently

$env:path += ";C:\Program Files\Scripts\"
[Environment]::SetEnvironmentVariable('Path',$env:path,[System.EnvironmentVariableTarget]::Machine)

That’s it! Note the use of the += operator which tells Powershell to add the string that I specified to the existing value of $env:path, rather than removing the previous value of $env:path and replacing it. This preserves what was in the path before (things like C:\Windows\System32) so that the other programs in the system don’t break. That is also the point of the ; at the start of the string - each file path stored in the path variable is separated by a semicolon, but typically there isn’t one already on the end of the existing path, so we add that first.

System Properties GUI

Also keep in mind that you can edit the system path variable through the System Properties window, on the Advanced tab, in the Environment Variables window. You can get to these windows through the File Explorer, by right-clicking on ‘This PC’ and then select Properties, then find ‘Advanced system settings’ at the bottom of the page. Alternatively you can open a run box and run the command systempropertiesadvanced to jump right there.

Windows: Verify File Checksums

Windows doesn’t provide a brilliant utility for verifying the checksum value on files (typically a task I want to perform on files downloaded from the internet). Well… in fact there is a utility, however the biggest drawn back is how it only generates the hash of the file while stopping short of comparing the file hash to the checksum provided by the website. That’s a feature I believe deserves support in a checksum utility, so I’ve created my own in a Powershell script.

## File Checksum Verifier
## Calculate a file's checksum, chosen from a list of supported hashes, then compare to a provided checksum value

param ($path, $hash, $checksum)

$HelpKeywords = @("-h", "-help", "-?")
if ($HelpKeywords -contains $args[0])
{
  Write-Output "verify-checksum -path <path to target file> -hash <SHA1|SHA256|SHA512|MD5> -checksum <provided checksum>"
  exit
}

Write-Output "File Checksum Verifier"

if (!$path) {$TargetFilePath = $(read-host "Path to file").Replace("`"","")} else { $TargetFilePath = $path }

$SupportedHashes = @("SHA1", "SHA256", "SHA512", "MD5")
if ($SupportedHashes -notcontains $hash) {
  $HashSelectionMenu = "
[1] SHA1
[2] SHA256
[3] SHA512
[4] MD5
"
  $HashSelectionPrompt = "Select hash to use (type ? for help)[MD5]"
  $HashSelectionMenu
  do {
    try {
      $HashMenuSelection = read-host "$HashSelectionPrompt"
	  if ($HashMenuSelection -eq "") {$HashMenuSelection=4}
	  if (($HashMenuSelection -ge 1 -and $HashMenuSelection -le 4) -and $HashMenuSelection -as [int])
	  {
  	    $NoError=$true
	  }
	  else
	  {
	    if ($HashMenuSelection -eq "?")
	    {
	      $HashSelectionMenu
	    }
	    else
	    {
  	      "Not a menu selection!`n"
	    }
	    throw "bad value"
	  }
    }
    catch {$NoError = $false}
  }
  until ($NoError)
  $SelectedHash = switch($HashMenuSelection)
  {
    1 {"SHA1"}
    2 {"SHA256"}
    3 {"SHA512"}
    4 {"MD5"}
    default {"MD5"}
  }
}
else
{
  $SelectedHash = $hash
}

if (!$checksum) {$ProvidedChecksum = read-host "Provided Checksum"} else { $ProvidedChecksum = $checksum }

$FileChecksum = $(get-filehash $TargetFilePath -algorithm $SelectedHash).hash
Write-Output "File Checksum: $FileChecksum"

$ChecksumChecksOut = "$FileChecksum" -eq "$ProvidedChecksum"

Write-Output "Checksum matches: $ChecksumChecksOut"

I’ve then placed this file in C:\Program Files\Scripts and added that directory to my system Path variable (see how in this article). Now I simply call the script with verify-checksum. The script accepts the following options: -path for the full path to the file you want a checksum of, -hash to specify which hash to use on the file (one of: SHA1, SHA256, SHA512, or MD5), and -checksum for the checksum value supplied by the website you got the file from. Or, instead of passing everything to the command during the call, it will recognize any and all options missing and prompt you for each one that is still needed (or that did not match an accepted value).