Tech Notes

Linux Basics

https://www.youtube.com/watch?v=UCr04qIB7uc

What is Linux?

  • Linux is the kernel which powers a linux operating system
  • Kernels are the program that talk directly to the hardware(hard disk, RAM etc) also known as kernel space.
  • Why we need a separate program to talk to hardware?: For security. We want only 1 program to have access to the hardware.
  • the Operating system will talk to hardware via kernel. OS will make system calls to the the kernel to talk the hardware

Linux Distributions Family:

Linux distribution: When a kernel is bundled with the operating system software and bundled together, that is called Linux distribution.

  • Red Hat: Centos, Centos stream, Fedora etc
    • Fedora is upstream testing
    • Centos is close clone of RHEL
    • YUM package manager
    • RHEL is used by enterprises
    • rpm packages
  • Suse: Suse and OpenSuse
    • RPM based package manager
  • Debian: Ubuntu. Pure open source community project:
    • DPKG based APT package manager
    • used in cloud deployments
    • GNOME-based
    • deb packages

Kernel:

  • Glue between the hardware and the applications
  • For example: Linux Kernel

Distribution:

  • Collection of software making up a linux-based OS
  • For example: Ubuntu, Fedora

Bootloader:

  • Program that boots the OS
  • example: GRUB and ISOLINUX

Services: a program which runs as the background:

  • httpd, ntpd

Filesystem: Method for storing and organising files:

  • ext3, ext4, FAT, NTFS

Shell: Command line interpreter that interprets the command line input and instructs the OS to perform any necessary tasks and commands. eg: bash, zsh

Environment variables: Name:value pair which can be used by the programs.

check the list of environment variables: run command “env”
env

LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
SSH_CONNECTION=10.200.84.30 56150 10.188.32.132 22
DISTCC_HOSTS=–localslots_cpp=64 –randomize 10.8.68.214/16,lzo 10.8.68.215/16,lzo 10.8.68.216/16,lzo 10.8.68.217/16,lzo 10.8.68.218/16,lzo 10.8.68.219/16,lzo 10.8.68.220/16,lzo 10.8.68.221/16,lzo 10.8.68.222/16,lzo 10.8.68.223/16,lzo 10.8.68.224/16,lzo 10.8.68.225/16,lzo 10.8.68.226/16,lzo 10.8.68.227/16,lzo 10.8.68.228/16,lzo 10.8.68.229/16,lzo 10.8.68.230/16,lzo 10.8.68.231/16,lzo 10.8.68.232/16,lzo 10.8.68.233/16,lzo 10.8.68.234/16,lzo 10.8.68.235/16,lzo 10.8.68.236/16,lzo 10.8.68.237/16,lzo 10.8.69.12/16,lzo 10.8.69.13/16,lzo 10.8.69.14/16,lzo 10.8.69.15/16,lzo 10.8.69.16/16,lzo 10.8.69.17/16,lzo 10.8.69.18/16,lzo 10.8.69.19/16,lzo 10.8.69.21/16,lzo 10.8.69.22/16,lzo 10.8.69.23/16,lzo 10.8.69.25/16,lzo 10.8.69.39/16,lzo 10.8.73.199/16,lzo 10.8.73.200/16,lzo 10.8.73.219/16,lzo
LANG=en_US.UTF-8
PATH=/Library/Frameworks/Python.framework/Versions/3.11/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/usr/local/go/bin:/opt/puppetlabs/bin

~$ echo $HOME
/home/tiger

BASH profile:
whenever we login into the system, .bash_profile contents are executed. For example, in below bash_profile, the path for the python is being set. We can see all the paths using echo ${PATH}:

$ cat .bash_profile

eval "$(/opt/homebrew/bin/brew shellenv)"

# Setting PATH for Python 3.11
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.11/bin:${PATH}"
export PATH


$ echo $PATH
/opt/homebrew/bin:/opt/homebrew/sbin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/usr/local/go/bin:/opt/puppetlabs/bin

Linux Boot process:

https://www.youtube.com/watch?v=XpFsMB6FoOs

Storage:

to get the block devices which are connected to your server. For which drive we plug-in, kernel will provide a a name as sda, sdb, sdc etc.

We will have partition for each drive like sdb1 etc.

$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  120G  0 disk
└─sda1   8:1    0  120G  0 part /
sdb      8:16   0  500G  0 disk /data00
sr0     11:0    1  518K  0 rom

In linux, everything is a file. Even the drives:
$ ls -l /dev/sda
brw-rw---- 1 root disk 8, 0 Mar 27 08:51 /dev/sda

We can leverage fdisk command too:

sudo fdisk -l
Disk /dev/sda: 120 GiB, 128849018880 bytes, 251658240 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x230f4083

Device     Boot Start       End   Sectors  Size Id Type
/dev/sda1  *     2048 251658206 251656159  120G 83 Linux


Disk /dev/sdb: 500 GiB, 536870912000 bytes, 1048576000 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

For a new disk, we create below:

  • Create a partition table(fdisk)
    • tells where a partition begins and ends
    • two formats:
      • MBR
        • 32 bit
        • GPT
  • create a partition(fdisk)
  • format the partion and create file system(mkfs)

Linux File system:

https://www.youtube.com/watch?v=bbmWOjuFmgA

This of file system as a closet. Each slot in the closet is used to store jackets, socks, t-shirts etc. this makes the closet clean and not cluttered. Same is the purpose of the file system.

Linux file system hierarchy:

Linux mounting and partitions: https://www.youtube.com/watch?v=2Z6ouBYfZr8

In this tutorial, we will go through basic linux commands on the terminal.

Check if you are running Bash as your default shell:

 echo "${BASH_VERSION}"

In below example, we are not using bash as our shell. I can switch to bash with the command "bash" on the terminal:

~ % echo "${BASH_VERSION}"

~ % bash

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.

bash-3.2$ echo "${BASH_VERSION}"
3.2.57(1)-release

Command Echo: Echo allows us to output text in the terminal. example: we can do: echo “Bash is amazing”

Package management

Package management is centralised way to install software. Our system connects to centralised repository with list of packages and dependencies. Our system will download the packages and dependencies and install them.

If we are using Debian based Linux distributions(Ubuntu), we can use apt tool for our package management.

LINKS

inodes: inode is the data object which contains the metadata about the file object. it contains size, permissions, owner/group, etc (excluding name and content).

every storage medium has its own inodes. so hard disk will have different inodes compared to flash drive. Inodes can be statically created while creating the file system and also dynamically by some of the file systems.

Each storage medium has its number of inodes which are consumed by each file/directory we create:

$ df -hi
Filesystem     Inodes IUsed IFree IUse% Mounted on
udev             1.9M   362  1.9M    1% /dev
tmpfs            1.9M   591  1.9M    1% /run
/dev/sda1        7.5M  121K  7.4M    2% /
tmpfs            1.9M     2  1.9M    1% /dev/shm
tmpfs            1.9M     2  1.9M    1% /run/lock
tmpfs            1.9M    16  1.9M    1% /sys/fs/cgroup
tmpfs            1.9M     2  1.9M    1% /.syskrbonly
tmpfs            1.9M    13  1.9M    1% /run/user/0
/dev/sdb          32M  124K   32M    1% /data00
tmpfs            1.9M    13  1.9M    1% /run/user/1000
tmpfs            1.9M    11  1.9M    1% /run/user/2000
tmpfs            1.9M    11  1.9M    1% /run/user/1001

symbolic links: think of these as the shortcuts. We can create a soft or symbolic link. symbolic link will have its own inode but contents will be of the referred link

hard links: think of this as a copy. hard link will have same inode number as the original file.


Posted

in

by

Tags: