Linux

Overview

  • Two main flavors in use - Debian-based (Ubuntu, Debian, Mint, etc) & RHEL-based (Red Hat, CentOS, Fedora etc)
    • Debian uses apt-get to install packages whereas RHEL uses yum
    • For Debian, apt-get update, apt-get upgrade and apt-get install <app> are all that’s needed
    • /etc/network/interfaces used for permanent interface modification in Debian distros
    • /etc/resolvconf/resolv.conf.d/ stores files for generating resolv.conf file in Debian distros, which determines DNS resolution
    • To restart Debian networking after making changes, use sudo systemctl restart networking
  • Bash script files start with #!/bin/bash, called a shebang which points to the location of the bash interpreter
    • Similarly, Python scripts would have a shebang of #!/usr/bin/env python3

Environment Variables

  • Dynamic variables stored in the system and used by the shell and its applications
  • .env extension files typically used to store variables, like below:
#.env file
# IP Address
export HOST="192.168.1.10"
  • export is used to set environment variables, and set is used to view them
  • By storing credentials in environment variables and not including these variables in your git repo, you can prevent accidentally pushing credentials to git in a script

Linux Filesystem

  • /bin - contains binaries, applications and programs such as ‘ls’
  • /boot - system boot files - do not modify
  • /dev - device files such as USB drives, webcams etc
  • /etc - mostly contains configuration files
  • /home - user personal directories
  • /lib - contains libraries, kernel modules, drivers
  • /media - mount point for external storage
  • /mnt - mostly legacy, for manually mounting storage partitions
  • /opt - compiled software lives here
  • /proc - contains files for the core operating system
  • /root - superuser home directory
  • /sbin - like bin but for programs only superusers can run
  • /usr - original home directory, now used for applications, documents, libraries and other files
  • /srv - configuration files for Linux servers
  • /sys - similar to /proc and /dev, contains information about connected devices
  • /tmp - temporary files
  • /var - mostly log files

Vi

  • Ctrl-f - Page down or ‘forward’
  • Ctrl-b - Page up or ‘back’
  • :q! to exit without making changes, :wq to write and exit
  • Press ‘i’ to enter insert mode

Useful Commands

  • ls, cd, pwd, mkdir, rmdir, rm, cp, mv, uptime - Basic commands
  • cd ~ will bring you to your home directory, cd / for the root directory
  • find <directory> -name <name> can be used to find files, accepts wildcards
  • grep can be used for searching within files
  • less can be used to view files
  • chmod - Used for modifying file permissions
  • echo will print a command to the terminal with stdout, can be redirected to a file with >
  • read can take user input from the shell, stdin
  • htop, ps aux - Different commands for displaying system processes, can use kill <pid> to stop process
  • df -h - Shows disk usage for each mounted filesystem
  • free -h - Displays memory usage
  • route - Displays route table
  • ifconfig - Displays interfaces & IP addresses
  • history - Displays past commands used - can also use CTRL+R to search history
  • set - Displays shell and environment variables
  • export - Set an environment variable
  • vi ~/.bashrc, source ~/.bashrc can be used to modify aliases - alias will show all existing aliases