Linux Cheat Sheet
Notable commands, variables and files for administering linux.
Paths ending with / is a directory, and those without it is a file. Example: ~/test is a file and ~/test/ is a folder.
Environment Variables
Environment variables are variables accessible by a specific instance of a process, usually within a shell session.
Commands:
VARIABLE=value- AssignsvaluetoVARIABLE. IfVARIABLEalready exxists, this overwrites its previous value.$VARIABLE- Accesses the value ofVARIABLE. When a command is executed,$VARIABLEis expanded to its values.echo $VARIABLE- Displays the value ofVARIABLE.set- Lists all currently set environment variables.unset VARIABLE- Removes the environment variableVARIABLE.export VARIABLE- MakesVARIABLEaccessible to child processes spawned by the current process.export VARIABLE=valueis equivalent toVARIABLE=value && export VARIABLE.
GRUB (Bootloader)
UEFI system and GRUB 2 is assumed in this section.
Commands:
grub-install- Installs GRUB to/boot/efi/. Pass the--efi-directoryoption to install to other directory.update-grub- Shortcut to the commandgrub-mkconfig -o /boot/grub/grub.cfg. Updates GRUB configuration file (commonly located in/boot/grub/grub.cfg) from the definitions in/etc/default/gruband/etc/grub.d/- Press
Cwhen on GRUB menu screen to access GRUB shell. - Press
Ewhen on GRUB menu to edit kernel parameter for that particular entry for the current boot. Setsystemd.unit=rescue.targetto enter rescue mode for systemd base systems.
Variables:
$prefix- GRUB configuration path location. (This is a variable for GRUB shell, not linux one)
Files:
/boot/efi/- Mount point of ESP (EFI System Partition) containing the actual bootloader UEFI accesses/boot/grub/grub.cfg- Common location for GRUB configuration file. Located in root partition. Do not edit directly, edit/etc/default/gruband runupdate-grubinstead./etc/grub.d/-update-grubchanges/boot/grub/grub.cfgbased on this folder. Contents are executed in numerical order. Custom entries are usualy added to40_customfile. Example entry is shown below.menuentry "Default OS" {
set root=(hd0,1)
linux /vmlinuz root=/dev/sda1 ro quiet splash
initrd /initrd.imgset root=(hd0,1)- Sets device and partition of root folder of OS.hd0,hd1,... corresponds to/dev/sda,dev/sdb,... in linux. Partition numbering starts at 1. So, this configuration in particular equates to/dev/sda1/vmlinuz- Location of linux kernel. Because the kernel is located directly inside root folder, we can assume that this configuration has a seperate boot partition and it is not located at/dev/sda1(hd0,1). If there is no boot partition and the boot folder is located within root parition, something like/boot/vmlinuzwill instead be shown instead.
systemd (Service Manager)
Commands:
General
systemctl list-unit-files- Lists all available units and show if they are enabled. Pass the option--type=TYPEto only show units ofTYPEtype.systemctl list-units- Lists all active units. Pass the option--type=TYPEto only show units ofTYPEtype.systemctl reboot- Reboot the system. Same effect assystemctl isolate reboot.targetsystemctl poweroff- Power off the system. Same effect assystemctl isolate shutdown.targetsystemctl suspend- Sleeps the system. Data in memory is saved as is.systemctl hibernate- Hibernates the system. Data in memory is moved to disk.systemctl rescue- Puts the system in rescue mode (similar to "safe mode" on Windows). Same effect assystemctl isolate rescue.target
Services
systemctl start [<service>]- Starts unitsystemctl stop [<service>]- Stops unitsystemctl restart [<service>]- Restarts unitsystemctl status [<service>]- Shows the state of unitsystemctl is-active [<service>]- Showsactiveif unit is running,inactiveotherwise.systemctl enable [<service>]- Makes unit start from the next system initialization. Does NOT start unit immediately.systemctl disable [<service>]- Unit will not start from the next system initialization.systemctl is-enabled [<service>]- Outputsenabledordisabled.
Targets
systemctl isolate [<target>]- Alternate to the target unit.systemctl set-default [<target>]- Set the default initialization target for subsequent boots. Usually defaults tomulti-user.targetorgraphical.target.systemctl get-default- Get the default initialization target.
Files:
/lib/systemd/system/- Location of unit configuration file
Shared Libraries
Commands:
ldconfig- Reads/etc/ld.so.confand/etc/ld.so.conf.d/*to configure shared library location and caches them into/etc/ld.so.cache.-p- Shows currently cached shared library (do not update the cache).-v- Updates the cache and show the cache while doing so.
ldd [program || so]- Shows shared library dependencies of a program or shared object.objdump&readelf- Examines contents of object, binary, and shared library files
Variables:
$LD_LIBRARY_PATH- Colon (:) separated set of directories to look for libraries. User configured. Empty by default.
Files:
/etc/ld.so.conf.d/-ld(dynamic linker) looks for shared library paths defined in files in this folder.
Last updated: July 28, 2023