Skip to content

Latest commit

 

History

History
641 lines (484 loc) · 17.4 KB

THEORY.MD

File metadata and controls

641 lines (484 loc) · 17.4 KB

Linux Fundamentals:

 $ cat /var/log/lastlog

Welcome home :)

resources : Linux Directories Explained in 100 Seconds, Navigating the Linux Filesystem, the Linux File System explained in 1,233 seconds, Linux File System/Structure Explained!, Introduction to Linux – Full Course for Beginners, Linux for Hackers //

Everything in linux is a "file" - "On a UNIX system, everything is a file; if something is not a file, it is a process.". A detailed view of linux file structure is here.

Linux Filesystem

directory description
/ root directory.
# root user.
~ home directory.
/bin Common programs, shared by the system, the system administrator and the users.
/boot The startup files and the kernel, vmlinuz. In some recent distributions also grub data. Grub is the GRand Unified Boot loader and is an attempt to get rid of the many different boot-loaders we know today.
/dev Contains references to all the CPU peripheral hardware, which are represented as files with special properties.
/etc Most important system configuration files are in /etc, this directory contains data similar to those in the Control Panel in Windows
/home Home directories of the common users.
/lib Library files, includes files for all kinds of programs needed by the system and the users.
/net Standard mount point for entire remote file systems
/proc A virtual file system containing information about system resources. More information about the meaning of the files in proc is obtained by entering the command man proc in a terminal window. The file proc.txt discusses the virtual file system in detail.
/root The administrative user's home directory. Mind the difference between /, the root directory and /root, the home directory of the root user.
/sbin Programs for use by the system and the system administrator.
/tmp Temporary space for use by the system, cleaned upon reboot, so don't use this for saving any work!
/usr Programs, libraries, documentation etc. for all user-related programs.
/var Storage for all variable files and temporary files created by users, such as log files, the mail queue, the print spooler area, space for temporary storage of files downloaded from the Internet, or to keep an image of a CD before burning it.
/opt Typically contains extra and third party software.
/mnt Standard mount point for external file systems, e.g. a CD-ROM or a digital camera.
/misc For miscellaneous purposes.
/initrd (on some distributions) Information for booting. Do not remove!
/srv server related files are stored.
/lost+found Every partition has a lost+found in its upper directory. Files that were saved during failures are here.
/sys It is a virtual filesystem for modern Linux distributions to store and allows modification of the devices connected to the system.

[Disks and Filesystem]] [Linux Kernel] [Processes and Resource Utilization] [Compiling Softawre from C Source code] [Network Configuration] [Development tools] [User Environments]

location description
/boot/vmlinux The Linux kernel file.
Device Files:
/dev/hda Device file for the first IDE HDD.
/dev/hdc A pseudo-device that output garbage output is redirected to /dev/null.
System Configuration Files:
/etc/bashrc It is used by bash shell that contains system defaults and aliases.
/etc/crontab A shell script to run specified commands on a predefined time interval.
/etc/exports It contains information on the file system available on the network.
/etc/fstab Information of the Disk Drive and their mount point.
/etc/group It is a text file to define Information of Security Group.
/etc/grub.conf It is the grub bootloader configuration file.
/etc/init.d Service startup Script.
/etc/lilo.conf It contains lilo bootloader configuration file.
/etc/hosts Information of IP and corresponding hostnames.
/etc/hosts.allow It contains a list of hosts allowed accessing services on the local machine.
/etc/host.deny List of hosts denied to access services on the local machine.
/etc/inittab INIT process and their interaction at the various run level.
/etc/issue Allows editing the pre-login message.
/etc/modules.conf It contains the configuration files for the system modules.
/etc/motd It contains the message of the day.
/etc/mtab Currently mounted blocks information.
/etc/passwd It contains username, password of the system, users in a shadow file.
/etc/printcap It contains printer Information.
/etc/profile Bash shell defaults.
/etc/profile.d It contains other scripts like application scripts, executed after login.
/etc/rc.d It avoids script duplication.
/etc/rc.d/init.d Run Level Initialisation Script.
/etc/resolv.conf DNS being used by System.
/etc/security It contains the name of terminals where root login is possible.
/etc/skel Script that initiates new user home directory.
/etc/termcap An ASCII file that defines the behavior of different types of the terminal.
/etc/X11 Directory tree contains all the conf files for the X-window System.
User Related Files:
/usr/bin It contains most of the executable files.
/usr/bin/X11 Symbolic link of /usr/bin.
/usr/include It contains standard include files used by C program.
/usr/share It contains architecture independent shareable text files.
/usr/lib It contains object files and libraries.
/usr/sbin It contains commands for Super User, for System Administration.
Virtual and Pseudo Process Related Files:
/proc/cpuinfo CPU Information
/proc/filesystems It keeps the useful info about the processes that are running currently.
/proc/interrupts it keeps the information about the number of interrupts per IRQ.
/proc/ioports Contains all the Input and Output addresses used by devices on the server.
/proc/meminfo It reports the memory usage information.
/proc/modules Currently using kernel module.
/proc/mount Mounted File-system Information.
/proc/stat It displays the detailed statistics of the current system.
/proc/swaps It contains swap file information.
Version and Log Files:
/version It displays the Linux version information.
/var/log/lastlog It stores user last login info.
/var/log/messages It has all the global system messages.
/var/log/wtmp It keeps a history of login and logout information.

Linux uses a two-part software implementation as a way to improve both system and programmer efficiency.

The first part of this two-part implementation is the Linux virtual filesystem. This virtual filesystem provides a single set of commands for the kernel, and developers, to access all types of filesystems. The virtual filesystem software calls the specific device driver required to interface to the various types of filesystems. The filesystem-specific device drivers are the second part of the implementation. The device driver interprets the standard set of filesystem commands to ones specific to the type of filesystem on the partition or logical volume.

SUDO Power and User Management:

sudo = superuser do

$ adduser saikia # (ain't gonna work without sudo access)
$ sudo adduser saikia
$ cat /etc/passwd # (find all users)

$ sudo useradd skk # (useradd is lazy - no bash & no home)

$ sudo passwd skk  # (add password)
$ cd /home
$ ls              # (skk doesn't has a /home but skk does)

$ usermod -h
$ sudo usermod skk --shell /bin/bash # (now skk has a bash shell)

$ sudo usermod -l ranjan skk # (ranjan is the new name for skk)
$ sudo useradd amartya -m # (-m will create a home directory)

$ su saikia
$ su - # (switch to root user)
$ sudo su -


sudoers file
$ sudo visudo 

$ sudo userdel ranjan

$ sudo group add digi55
$ cat /etc/group
$ groups

$ sudo usermod -aG digi55 amartya # (add user to group, -a equals append)
$ sudo gpasswd -d amartya digi55 # (remove user from group)
$ sudo groupdel digi55 

Package Managers in Linux

dpkg doesn't install dependencies but apt (advanced package tool) does :) snap is another package store.

 $ sudo apt update
 $ sudo apt install packagename
 $ sudo apt edit-sources # (list of sources to pull packages from)
 $ sudo apt -h
 $ sudp apt list
 $ apt list --installed # (find installed packages)
 $ apt list --installed > grep ^nmap
 $ sudo apt show nmap
 $ sudo apt search nmap
 $ sudo apt remove packagename # (config files remains)
 $ sudo apt purge packagename # (removes everything)
 $ sudo apt upgrade # (update packages)
 $ sudo apt update && full-upgrade # (removes old files and does fresh updates)
 $ sudo aptitude

 $ sudo snap install --classic code # (install vscode with snap)

Daemons : start, stop, restart Linux services

Daemons are system services/ processes in linux. All daemons have a 'd' at the end. Master Daemon - 'systemd' with PID 1 and systemctl is used to control daemons. systemd refers other daemons as units.

    $ ps -aux
    $ ps -aux | grep nano # (check processes for nano)
    $ pstree

    $ sudo systemctl stop sshd
    $ sudo systemctl status sshd
    $ sudo systemctl start sshd
    $ sudo systemctl restart sshd
    $ sudo systemctl reload-or-restart sshd # (restart if not reload)
    $ sudo systemctl disable ntp
    $ sudo systemctl enable ntp
    $ sudo systemctl is-active ntp
    $ sudo systemctl is-enabled ntp
    $ sudo systemctl list-units # (list all daemons that systemd knows)
    $ sudo systemctl list-units --all
    $ sudo systemctl list-units -t service # (active services)

    $ sudo systemctl list-unit-files | grep nginx
    $ sudo systemctl list-units | grep journal
    $ sudo systemctl restart systemd-journald
    $ sudo journalctl -xe (systemd log)

Manage processes in linux:

    $ ps -u username # (check all processes for a particular user)
    $ ps -u username | grep firefox
    $ pgrep firefox # (returns PID of firefox)
    $ kill PID
    $ ps --help simple
    $ top # (running processes on linux)
    $ htop # (cooler top)
    $ jobs
    $ bg 1 # (make job 1 a bg process)
    $ fg 1 # (make job 1 a fg process)
    $ kill -l # (list kill signals, SIGTERM is default kill signal)
    $ kill -9 # ((9)SIGKILL is ultimate boss)

    $ sleep 900 & # (sleep and send to bg)
    $ kill -9 PID # (kill no matter what)
    $ pkill -9 ping # (pkill kills multiple processes by name)

Webserver

127.0.0.1 = localhost (no place like 127.0.0.1)

     $ python -m http.server 7600 # (launches a web server from dir)
     $ php -S 127.0.0.1
     $ npx http-server -p 8086
     $ sudo nano /etc/apache2/ports.conf # (to change port)
     $ systemctl start apache2

     $ curl localhost:8086
     $ curl -o dirfoldername localhost:8086 # (download website to dirfoldername)
     $ cat dirfoldername # (to check the downloaded website)
     $ curl -I localhost:8086 # (getting response header)
     $ curl -v localhost:8086 # (verbose - chatty req)
     $ wget localhost:8086

Regular Expression (regex)

Regular Expressions is nothing but a pattern to match for each input line. A pattern is a sequence of characters. You can use ^ and $ to force a regex to match only at the start or end of a line, respectively. You can match specific characters and character ranges using [..] syntax. We need to use the “-E” option with the character/string interval value.

  • . = Matches any single character.
  • ? = The preceding item is optional and will be matched, at most, once. may or may not be present.
  • * = The preceding item will be matched zero or more times.
  • + = The preceding item will be matched one or more times.
  • {N} = The preceding item is matched exactly N times.
  • {N,} = The preceding item is matched N or more times.
  • {N,M} = The preceding item is matched at least N times, but not more than M times.
  • ^ = Matches the empty string at the beginning of a line; also represents the characters not in the range of a list.
  • $ = Matches the empty string at the end of a line.
  • < = Match the empty string at the beginning of word.
  • > = Match the empty string at the end of word.
  • /b = It will match the empty character or string at the edge.
  • /B = It will match the empty character or string at the non-edge.
s grep 'saikia' /etc/passwd
s grep -i -w 'saikia' /etc/passwd # (case insensitive search)
$ grep -E -i -w 'skk|ranjan' /etc/passwd # (extended reg exp to find 'skk' or 'ranjan')
$ egrep -i '^(linux|unix)' filename
$ grep -E -i '^(linux|unix)' filename # (same as above egrep with -E)
$ grep 'word1\|word2' filename

$ grep 'purchase' demo.txt # ( find all filenames starting with purchase )
$ grep 'purchase.db' demo.txt # ( find all filenames starting with purchase and followed by another character )
$ grep 'purchase..db' demo.txt # ( find all filenames starting with purchase but ending with db )

$ grep ^saikia /etc/passwd
$ grep -w ^saikia /etc/passwd # (only saikia and no followup characters)

$ grep 'foo$' filename # (find lines ending with word foo)
$ grep '^foo$' filename # (Match line only containing foo)
$ grep '^$' filename # (search for blank lines)

$ grep '[sS][aA][iI][kK][iI][aA]' filename # (match both saikia or Saikia)
$ grep -w '[sS]aiikia[0-9]' filename # (match saikia1, Saikia2 ...)
$ grep '[A-Za-z]' filename # (match at least one letter)
$ grep [wn] filename # (Display all the lines containing either a “w” or “n” character)
$ grep '[:upper:]' filename 

[[:alnum:]] , [[:alpha:]], [[:blank:]], [[:digit:]], [[:lower:]], [[:space:]], [[:upper:]]

$ grep '[sS]aikia[^0-9]' test # (The ^ negates all ranges in a set)

$ grep '^..$' filename # (print all lines with exactly two characters)
$ grep '^\.[0-9]' filename # (Display any lines starting with a dot and digit)

$ egrep '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}' file # (will only match an IP address)

$ egrep "v{2}" filename # (match a character “v” two times)
$ egrep 'co{1,2}l' filename # (match both “col” and “cool” words)
$ egrep 'c{3,}' filename # ( match any row of at least three letters ‘c’)
$ grep "[[:digit:]]\{2\}[ -]\?[[:digit:]]\{10\}" filename # (match mobile number format 91-1234567890 (i.e TwoDigit-TenDigit))
$ cat file.txt | grep -E e\{2} # (sequence of character “e” coming two times in the string. e.g - three)
$ cat file.txt | grep "n\+e" # (string from starting character as “n” and adjacent character as “e” - eg : one, nine)
$ grep '\bfive\b' file.txt # (find the exact matching string from the input file)