Basics


The shell is an OS-level application that interprets commands. We interact with the shell using the terminal. Here are some basic commands we’ll use on a regular basis

This information is found at Linux commands cheat sheet

browsing directories

  • ~ refers to the Home directory
  • / the root directory
  • .. the parent directory
  • . current directory
  • cd wanted directory - so cd / will take us to the root directory
  • dir will list all the files in the directory
  • pwd provides the working directory
Command Syntax Description Example
List ls [OPTIONS] [FILE/DIRECTORY] List files and directories at path ls /home/user/documents
Print Working Directory pwd Print present working directory pwd
Change Directory cd [DIRECTORY] Change current directory cd /home/user/documents
Super user do sudo [COMMAND] Run command with superuser privileges sudo apt update
Text Editor nano [FILE] Open file with Nano text editor nano myfile.txt

running script

  • to run a python script file:
    • go to the directory where the script file is located
    • enter: python ./filename.py
    • enter
    • the file will execute

Creating & Editing Files

You can either use command-line text editors

  • GNU nano, just type: nano <filename> and you can use the shortcuts on the bottom of the screen
  • vi
  • vim, just
    • type: vim or open a file with vim <filename>.
    • vim is in either insert or edit mode
    • sav example.txt to create a file and write to buffer
    • :w to write to buffer
    • :q to quit
    • :q! quit without saving

GUI-based text editors

  • gedit - pre-installed with Linux
  • emacs

Requirements & Version


Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Administration Commands


man

man displays the relative manual page for the given command

  • -f print a short description of the given command
  • -a display in succession, all of the available manual pages contained within the manual
  • -k displays all the commands on your system that have a manual page
  • man command_name displays a brief description of what that command does
# ___  Prints out the documentation/manual for ls
man ls

tldr

TLDR Pages is a free and open-source collaborative documentation effort. The goal is to create documentation that is more accessible than the traditional man pages, which tend to be quite verbose.

TLDR Pages, short for “Too Long; Didn’t Read” and also known simply as tldr, provide examples for common use cases of various commands. The format of TLDR pages is similar to that of a cheatsheet.

You can install a command-line tool to access TLDR Pages from your terminal. Install it using the following command:

# to install using npm
npm install -g tldr

# once installed use
tldr command_name
Command Description
whoami See which user you are currently logged in as
who See a list of logged in users
id Displays user id and group id of user
sudo Execute a command with root permissions
sudo apt install Install a package on Debian based systems
sudo dnf install Install a package on Red Hat based systems
sudo apt remove Remove a package on Debian based systems
sudo dnf remove Remove a package on Red Hat based systems
reboot Reboot the system
poweroff Shut down the system
# what's user id and group id
$ id
uid=1000(theia) gid=1000(theia) groups=1000(theia),27(sudo),100(users)

Hard Drive & Storage


Command Description
df or df -h See the current storage usage of mounted partitions
sudo fdisk -l See information for all attached storage devices
du See disk usage of a directory’s contents
tree View the directory structure for a path
mount and umount Mount and unmount a storage device or ISO file
# Get available disk space
$ df
# OUTPUT
Filesystem     1K-blocks     Used Available Use% Mounted on
overlay        101986876 35242476  62324164  37% /
tmpfs              65536        0     65536   0% /dev
tmpfs           16361328        0  16361328   0% /sys/fs/cgroup
/dev/vda2      101986876 35242476  62324164  37% /etc/hosts
shm                65536        0     65536   0% /dev/shm
tmpfs           28937956       16  28937940   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs           16361328        0  16361328   0% /proc/acpi
tmpfs           16361328        0  16361328   0% /proc/scsi
tmpfs           16361328        0  16361328   0% /sys/firmware

# returns available disk space in units like gigabytes and terabytes
$ df -h
# OUTPUT
Filesystem      Size  Used Avail Use% Mounted on
overlay          98G   34G   60G  37% /
tmpfs            64M     0   64M   0% /dev
tmpfs            16G     0   16G   0% /sys/fs/cgroup
/dev/vda2        98G   34G   60G  37% /etc/hosts
shm              64M     0   64M   0% /dev/shm
tmpfs            28G   16K   28G   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs            16G     0   16G   0% /proc/acpi
tmpfs            16G     0   16G   0% /proc/scsi
tmpfs            16G     0   16G   0% /sys/firmware

Networking


computer network is a set of computers that are able to communicate with each other and share resources provided by network nodes.

A network resource is any object, such as a file or document, which can be identified by the network.

network node is any device that participates in a network.

A host is a special type of node in a computer network - it is a computer that can function as a server or a client on a network.

A server is a host computer that is able to accept a connection from a client host and fulfill certain resource requests made by the client.

Many hosts can perform either role, acting as both client and server.

A network packet is a formatted chunk of data that can be transmitted over a network.

Today’s computer networks typically use communication protocols that are based on such packets of information. Every packet consists of two types of data: 1. the control information, and 2. the payload. The control information is data about how and where to deliver the payload, such as the source and destination network addresses, while the payload is the message being sent.

The ping command works by sending special ‘echo request’ packets to a host and waiting for a response from the host.

An IP address is a code used to uniquely identify any host on a network.

URL, more commonly known as a web address, stands for Uniform Resource Locator.

Command Description
hostname Prints hostname
-s Drops the dot suffix
-i Prints the IP address
ifconfig Display or configure the system network interfaces
ifconfig eth0 Displays ethernet adapter config info
$ hostname
my-linux-machine.local

# To drop the suffix
$ hostname -s

# Print out Ethernet0 config gives us inet address, total # of packets, bytes received...
$ ifconfig eth0
# you can see the server's IP Address right after inet
Command Description
ip a Show IP address and other information for all active interfaces
ip r Show IP address of default gateway
cat /etc/resolv.conf See what DNS servers your system is configured to use
ping Send a ping request to a network device
traceroute Trace the network path taken to a device
ssh Login to a remote device with SSH
$ ping www.google.com
# gives us the size of the packet sent, the IP sent from...roundtrip time it took to reach continues till abort with CTRL C
 
# TO limit the number of packets sent use -c
$ ping -c 5 www.google.com

User Management


Command Description
useradd Low level utility for adding new user accounts
adduser High level utility for adding new user accounts
deluser Delete a user account
usermod Modify a user account
groupadd Create a new group
delgroup Delete a group

System Resources


Command Description
free -m See how much memory is in use and free
ps Lists each process that’s currently running w/its ID
top See a list of processes and their resource usage
htop A more human readable and interactive version of top
nice Start a new process with a specified priority
renice Change the nice value of a currently running process
ps aux OR ps -ef View all of the currently running processes
kill or killall Terminate a process
kill -9 or killall -9 Terminate a process with SIGKILL signal
bg Send a task to the background
fg Bring a task to the foreground
# List running processes owned by me the user
$ ps
# OUTPUT
   PID TTY          TIME CMD
    96 pts/0    00:00:00 bash
   594 pts/0    00:00:00 ps

# List all running processes on the system
$ ps -e
# OUTPUT
   PID TTY          TIME CMD
     1 ?        00:00:00 sh
     8 ?        00:00:00 entrypoint.sh
    36 ?        00:00:00 cron
    37 ?        00:00:00 node
    48 ?        00:00:00 sh
    49 ?        00:00:00 node
    60 ?        00:00:04 node
    71 ?        00:00:08 node
    86 ?        00:00:00 node
    96 pts/0    00:00:00 bash
   105 pts/1    00:00:00 bash
   524 ?        00:00:00 node
   598 pts/0    00:00:00 ps  

top

TOP or Table of Process provides a dynamic, real-time view of the system

The top command displays a table of detailed information related to the processes or threads which are currently running and managed by the kernel. Additionally, it also provides information related to CPU and memory usage per process.

When you start top, you’ll be presented with the following elements on the main top screen:

  • Summary area - shows information like system uptime, number of users, load average, and overall memory usage
  • Column header - attribute names
  • Task area - displays the data for each process, or PID

The output keeps refreshing until you press q or Ctrl + c.

If you want to exit automatically after a specified number of repetitions, use the -n option as follows: top -n 10

You can press the following keys with Shift while top is running to sort the table:

Key Sorts by
m Memory Usage
p CPU Usage
n Process ID (PID)
t Running Time

For example, you can find out which process is consuming the most memory by entering Shift + m.

$ top
M
# actually top changes to df -h once you hit enter
# OUTPUT
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          98G   34G   60G  37% /
top - 11:38:07 up 2 days,  8:51,  0 users,  load average: 3.02, 2.61, 1.99
Tasks:  13 total,   1 running,  12 sleeping,   0 stopped,   0 zombie
top - 11:40:25 up 2 days,  8:53,  0 users,  load average: 4.61, 3.07, 2.23
Tasks:  13 total,   1 running,  12 sleeping,   0 stopped,   0 zombie
%Cpu(s): 10.8 us,  6.0 sy,  0.3 ni, 82.4 id,  0.0 wa,  0.0 hi,  0.5 si,  0.0 st
KiB Mem : 32722660 total,  5071616 free,  5356180 used, 22294864 buff/cache
KiB Swap:        0 total,        0 free,        0 used. 26880696 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                        
    60 theia     20   0 1035080  90436  37688 S   0.3  0.3   0:04.80 node                           
    71 theia     20   0 1088128  69464  32900 S   0.3  0.2   0:09.69 node                           
     1 theia     20   0    4636   1092    656 S   0.0  0.0   0:00.01 sh                             
     8 theia     20   0   12892   3616   2112 S   0.0  0.0   0:00.00 entrypoint.sh                  
    36 root      20   0   31320   3016   2440 S   0.0  0.0   0:00.00 cron                           
    37 theia     20   0  863632  74820  32936 S   0.0  0.2   0:00.85 node                           
    48 theia     20   0    4640   1080    640 S   0.0  0.0   0:00.00 sh                             
    49 theia     20   0  716048  98384  33140 S   0.0  0.3   0:00.90 node                           
    86 theia     20   0  860436  68168  34388 S   0.0  0.2   0:00.89 node                           
    96 theia     20   0   25316   8120   2584 S   0.0  0.0   0:00.40 bash                           
   105 theia     20   0   25316   8056   2520 S   0.0  0.0   0:00.40 bash                           
   524 theia     20   0  599784  38884  29536 S   0.0  0.1   0:00.21 node                           
   667 theia     20   0   41668   4264   2964 R   0.0  0.0   0:00.01 top   

Environment Variable


Command Description
printenv or printenv variable_name List all environment variables on a Linux system, or a specific one
whereis and which Find where a command in PATH is located
export MY_SITE="linuxconfig.org" Set a temporary environment variable (just an example, but use the same syntax)
echo $VARIABLE Display the value of a variable
unset Remove a variable

Kernel Information


Command Description
uname Prints out the kernel name (u refers to unix-like OS)
uname -a Output detailed information about your kernel version and architecture
lsmod Find what modules are currently loaded
modinfo module_name Get information about any particular module
modprobe --remove module_name Remove a module
modprobe module_name Load a module into the kernel

Hardware Information


Command Description
lspci See general information about host bridge, VGA controller, ethernet controller, USB controller, SATA controller, etc.
dmidecode See some information about BIOS, motherboard, chassis, etc.
cat /proc/cpuinfo Retrieve processor type, socket, speed, configured flags, etc.
x86info or x86info -a See information about the CPU
cat /proc/meminfo See detailed information about system RAM
lshw List all hardware components and see their configuration details
lshw -C memory -short Detect number of RAM slots used, speed, and size
hwinfo List details for all hardware, including their device files and configuration options
biosdecode Get some general information about your system’s BIOS
dmidecode -s bios-vendor Retrieve the name of your BIOS vendor with this simple command
lsusb Get a list of USB devices plugged into your system
ls -la /dev/disk/by-id/usb-* Retrieve a list of USB device files
hdparm -I /dev/sdx Get information about your hard drive’s make, model, serial number, firmware version, and configuration
hdparm -tT /dev/sdx Show the speed of an installed hard drive – including cached reads and buffered disk reads
wodim --devices Locate CD or DVD device file