Commands
A random assortment of commands for reference
A random assortment of commands for reference
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 nip nip nip nn for neighbor
Install the ip command with:
dnf -y install iproute2apt -y install iproute2apk add iproute2pacman -Syu iproute2The standard command for showing IP address in Linux is ip a. ip also provides an option to view configured gateways:
ip rip rip rip rHere is an example of adding a default gateway:
ip route add default via 192.168.1.1If your distribution or container is missing this command you can add it with:
dnf -y install iproute2apt -y install iproute2apk add iproute2pacman -Syu iproute2Simple command to show disk space used for all connected file systems, in human readable sizes instead of bytes
df -hdf -hdf -hdf -hTroubleshooting 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 -tulpnss -tulpnss -tulpnss -tulpnss options mean:
| Switch | Meaning |
|---|---|
| t | show TCP connections |
| u | show UDP connections |
| l | show listening sockets only |
| p | include PID for listening processes |
| n | faster output by skipping resolving IP addresses to hostnames |
Install the ss command with:
dnf -y install iproute2apt -y install iproute2apk add iproute2pacman -Syu iproute2Though 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 iputilsapt -y install iputilsapk add iputils-pingpacman -Syu iputilsIn Linux use the ‘ps’ command to list running processes. Sometimes I’ve found this basic command missing from Linux containers.
ps auxps auxps auxps auxIf missing from your distribution or container, install with:
dnf -y install procpsapt -y install procpsapk add procpspacman -Syu procps-ngAnother 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>dig is an older command for doing DNS lookups. It considered legacy and has been replaced with drill in many distributions
If missing from your distribution or container, install with:
dnf -y install ldnsapt -y install ldnsutilsapk add drillpacman -Syu ldnsVirtually 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:
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 NetworkManagervi /etc/network/interfaces
systemctl restart networkingvi /etc/network/interfaces
vi /etc/resolv.conf
/etc/init.d/networking restartvi /etc/systemd/network/10-net0.link
> [Match]
> PermanentMACAddress=<interface MAC address>
>
> [Link]
> Name=net0
>
> [Network]
> Address=<address>
> Gateway=<gateway>
> DNS=<dns1>
> DNS=<dns2>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 applyMany 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
nmtuiapk 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
nmtuipacman -Syu networkmanager
systemctl --now enable NetworkManager.service
nmtuiAnother standard command needed for common troubleshooting in every OS - in Linux you show IP addresses with:
ip aip aip aip aIf missing from your distribution or container, install with:
dnf -y install iproute2apt -y install iproute2apk add iproute2pacman -Syu iproute2An 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.
vimvimvimvimIf missing from your distribution or container, install with:
dnf -y install vimapt -y install vimapk add vimpacman -Syu vimWhile 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.confcat /etc/resolv.confcat /etc/resolv.confcat /etc/resolv.confDNS servers are typically managed through the network manager for your distribution, or directly through network configuration files
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.
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
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 fdiskThe next code section uses the vgextend command to modify a volume group. To list your volume groups use vgdisplay
partprobe
pvcreate /dev/xvda3
vgextend /dev/fedora /dev/xvda3
lvextend -l+100%FREE /dev/fedora/root
fsadm resize /dev/fedora/rootpartprobe
pvcreate /dev/xvda3
vgextend /dev/cl /dev/xvda3
lvextend -l+100%FREE /dev/cl/root
fsadm resize /dev/cl/rootpartprobe
pvcreate /dev/xvda3
vgextend /dev/rl /dev/xvda3
lvextend -l+100%FREE /dev/rl/root
fsadm resize /dev/rl/rootCheck that your free space has increased with:
df -hThe volume group will appear in the listing as:
/dev/mapper/fedora-root/dev/mapper/cl-root/dev/mapper/rl-rootFor 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:
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.C:\Program Files or C:\Users\<my user>\ProgramsAs 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.
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.
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 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).