0xShukruN
Ask or search…
K
Links
📜

Linux Essentials

Quick reference of linux essentials and utilities to the beginner linux user. These notes were written quite a whilte ago, so some inaccurasies might be expected.

Filesystem Hierarchy Standard

Is a Linux filesystem standard, presented in the following screen
/
Primary hierarchy root and root directory of the entire file system hierarchy.
/bin
Essential command binaries that need to be available in single user mode; for all users, e.g., cat, ls, cp.
/boot
Boot loader files, e.g., kernels, initrd.
/dev
Device files, e.g., /dev/null, /dev/disk0, /dev/sda1, /dev/tty, /dev/random.
/etc
Host-specific system-wide configuration files
There has been controversy over the meaning of the name itself. In early versions of the UNIX Implementation Document from Bell labs, /etc is referred to as the etcetera directory,[3] as this directory historically held everything that did not belong elsewhere (however, the FHS restricts /etc to static configuration files and may not contain binaries).[4] Since the publication of early documentation, the directory name has been re-explained in various ways. Recent interpretations include backronyms such as "Editable Text Configuration" or "Extended Tool Chest".[5]
/etc/opt
Configuration files for add-on packages that are stored in /opt.
/etc/sgml
Configuration files, such as catalogs, for software that processes SGML.
/etc/X11
Configuration files for the X Window System, version 11.
/etc/xml
Configuration files, such as catalogs, for software that processes XML.
/home
Users' home directories, containing saved files, personal settings, etc.
/lib
Libraries essential for the binaries in /bin and /sbin.
/lib<qual>
Alternative format essential libraries. Such directories are optional, but if they exist, they have some requirements.
/media
Mount points for removable media such as CD-ROMs (appeared in FHS-2.3 in 2004).
/mnt
Temporarily mounted filesystems.
/opt
Optional application software packages.[6]
/proc
Virtual filesystem providing process and kernel information as files. In Linux, corresponds to a procfs mount. Generally automatically generated and populated by the system, on the fly.
/root
Home directory for the root user.
/run
Run-time variable data: Information about the running system since last boot, e.g., currently logged-in users and running daemons. Files under this directory must be either removed or truncated at the beginning of the boot process; but this is not necessary on systems that provide this directory as a temporary filesystem (tmpfs).
/sbin
Essential system binaries, e.g., fsck, init, route.
/srv
Site-specific data served by this system, such as data and scripts for web servers, data offered by FTP servers, and repositories for version control systems (appeared in FHS-2.3 in 2004).
/sys
Contains information about devices, drivers, and some kernel features.[7]
/tmp
Temporary files (see also /var/tmp). Often not preserved between system reboots, and may be severely size restricted.
/usr
Secondary hierarchy for read-only user data; contains the majority of (multi-)user utilities and applications.[8]
/usr/bin
Non-essential command binaries (not needed in single user mode); for all users.
/usr/include
Standard include files.
/usr/lib
Libraries for the binaries in /usr/bin and /usr/sbin.
/usr/lib<qual>
Alternative format libraries, e.g. /usr/lib32 for 32-bit libraries on a 64-bit machine (optional).
/usr/local
Tertiary hierarchy for local data, specific to this host. Typically has further subdirectories, e.g., bin, lib, share.[9]
/usr/sbin
Non-essential system binaries, e.g., daemons for various network-services.
/usr/share
Architecture-independent (shared) data.
/usr/src
Source code, e.g., the kernel source code with its header files.
/usr/X11R6
X Window System, Version 11, Release 6 (up to FHS-2.3, optional).
/var
Variable files—files whose content is expected to continually change during normal operation of the system—such as logs, spool files, and temporary e-mail files.
/var/cache
Application cache data. Such data are locally generated as a result of time-consuming I/O or calculation. The application must be able to regenerate or restore the data. The cached files can be deleted without loss of data.
/var/lib
State information. Persistent data modified by programs as they run, e.g., databases, packaging system metadata, etc.
/var/lock
Lock files. Files keeping track of resources currently in use.
/var/log
Log files. Various logs.
/var/mail
Mailbox files. In some distributions, these files may be located in the deprecated /var/spool/mail.
/var/opt
Variable data from add-on packages that are stored in /opt.
/var/run
Run-time variable data. This directory contains system information data describing the system since it was booted.[10]
In FHS 3.0, /var/run is replaced by /run; a system should either continue to provide a /var/run directory, or provide a symbolic link from /var/run to /run, for backwards compatibility.[11]
/var/spool
Spool for tasks waiting to be processed, e.g., print queues and outgoing mail queue.
/var/spool/mail
Deprecated location for users' mailboxes.[12]
/var/tmp
Temporary files to be preserved between reboots.

Linux Permission & Groups

Each file in Linux has Three(3) modes of access
  • Owner (section 2)
  • Group (section 3)
  • Others (section 4)
Each "mode of access" or user can possess a combination of Three(3) available permissions
  • Read - R - 4 (octal representation of permission)
  • Write - W - 2
  • Execute - X - 1
The Octal representation can benefit us when using tools like 'chmod' to modify the permissions
  • Section 1 reveals what type the object is:
    • d : directory
      • : regular file
    • l : soft link
  • Section 2,3,4 are detailed permissions for the respective user
  • Section 5 details any alternative access methods
    • '-' : No other method
    • '.' : SELinux
    • '+' : FACLs
    • '+' : Any combination of methods
  • Section 6,7 are names of the Owner and Group respectively

Programs manual - "man" command

The manuals also document and present the type of "command" or tool as the following table displays:
  • 1 - User Commands
  • 2 - Programming interfaces for kernel system calls
  • 3 - Programming interfaces to the C library
  • 4 - Special files such as device nodes and drivers
  • 5 - File formats
  • 6 - Games and amusements such as screen-savers
  • 7 - Miscellaneous
  • 8 - System administration commands
Note:
Can search using keywords with the "-k" flag and also with Regular Expressions, Just apply it inline
Example:
man -k passwd
Output:
chgpasswd (8) - update group passwords in batch mode
chpasswd (8) - update passwords in batch mode
gpasswd (1) - administer /etc/group and /etc/gshadow
grub-mkpasswd-pbkdf2 (1) - generate hashed password for GRUB
pam_localuser (8) - require users to be listed in /etc/passwd
passwd (1) - change user password
passwd (1ssl) - compute password hashes
passwd (5) - the password file
update-passwd (8) - safely update /etc/passwd, /etc/shadow and /etc/group

Descriptive tools search - "apropos" command

A way to search for a given tool by searching keywords from it's description.
Example:
apropos partition
Output:
addpart (8) - simple wrapper around the "add partition" ioctl
all-swaps (7) - event signalling that all swap partitions have been activated
cfdisk (8) - display or manipulate disk partition table
cgdisk (8) - Curses-based GUID partition table (GPT) manipulator
delpart (8) - simple wrapper around the "del partition" ioctl
fdisk (8) - manipulate disk partition table
fixparts (8) - MBR partition table repair utility
gdisk (8) - Interactive GUID partition table (GPT) manipulator
growpart (1) - extend a partition in a partition table to fill available space
partprobe (8) - inform the OS of partition table changes
partx (8) - tell the Linux kernel about the presence and numbering of on-disk partitions
resizepart (8) - simple wrapper around the "resize partition" ioctl
sfdisk (8) - partition table manipulator for Linux
sgdisk (8) - Command-line GUID partition table (GPT) manipulator for Linux and Unix

Find files in Linux

  • find
    # Find all the files whose name is tecmint.txt in a current working directory.
    find /home -name tecmint.txt
    # Find Files Using Name and Ignoring Case
    find /home -iname tecmint.txt
    # Find Directories Using Name
    find / -type d -name Tecmint
    # Find Files With 777 Permissions
    find . -type f -perm 0777 -print
  • locate
  • which

System Services management

  • systemctl
  • ss (viewing)

Advanced Package Tools

Kali linux is a Debian distribution that utilizes the Advanced package tool (APT) software to provide a package management software services in kali linux.
Two main application exists as an interface to managing software packages:
  • apt
    apt is a simplified and user-friendly version of apt-get
    Common command:
    list - list packages based on package names
    search - search in package descriptions
    show - show package details
    install - install packages
    reinstall - reinstall packages
    remove - remove packages
    autoremove - Remove automatically all unused packages
    update - update list of available packages
    upgrade - upgrade the system by installing/upgrading packages
    full-upgrade - upgrade the system by removing/installing/upgrading packages
    edit-sources - edit the source information file
  • apt-get
Note:
apt installs software package and fulfils those software's dependencies recursively

dpkg

dpkg is a core tool used to install packages on linux directly or indirectly through "apt".
dpkg those not resolve and install dependencies recursively according to the software's needs so that's something to take into account
  • dpkg can also install packages offline from a .deb file by using the -i or —install flag

Bash - Tips & Tricks

Environment Variables

Example:
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
View environment variable:
  • env Add environment variable:
  • export

Piping and Redirection

The are 3 streams of information in linux CLI
  • STDIN(0) - program input
  • STDOUT(1) - program output / result output
  • STRERR(2) - error messages stream
the information present in those streams can be redirected in multiple ways:
  • redirect information to be written as a file ( >, >> )
  • redirect information and input to a program ( | )
  • reverse redirection, etc. ( < )
Example:
command: cat some_file.txt
###
some_file.txt # STDIN 'some_file.txt'
"this is the text of 'some_file.txt'" # STDOUT
"file doesn't exist" #STDERR
### Redirection and piping ###
cat some_file.txt > new_file.txt
"appended text" >> new_file.txt
cat some_file.txt | grep -i
grep 'something' < file_to_grep.txt

Bash history & CLI tricks

Bash have a history file named ".bash_history" that can be displayed by the command "history"
kali@kali:~$ history
...
140 sudo apt upgrade
141 apt -g
142 cat some_file.txt
Shortened CLI command execution:
  • ! - will execute the command in the specified numerical row in the history file
  • !! - will execute the last executed command
  • CTRL + R - reverse-i-search will prompt a search bar for search commands in the history file and then executing them.

Text Searching and Manipulation

Common and powerful tools are:
  • grep - searches for regex occurrences in the files
  • awk - like cut but more complex and verbose
  • cut - used to cut text from a line according to specific parameters
  • sed - edits text from a stream

File Editing, Manipulation & Analysis

Text Editors

There are many text editor, some more capable and complicated than others
  • nano
  • vi
    • Good to know flags
    • insert - to begin typing
    • ~ - to enter command mode
    dd # Delete current line
    yy # Copy current line
    p # Pase clipboard contentcom
    • :q! - Quit without saving changes
    • :w - Write file to disk
    • :wq! - Save changes and quit

Comparing Files

  • comm - creates output of 3 columns in this order [ A_UNIQUE_LINES(1), B_UNIQUE_LINES(2), AB_SIMILLAR_LINES]
    • -n <column_num> - a flag to control visibility of columns
    -n 12 (column 1 and 2)
  • diff - ' - ' precedes occurrence in 1st file where not in 2nd file ' + ' precedes occurrence in 2nd file where not in 1st file
    Note: When using the -c format flag, similar lines will be present in the output, when using the -u format flag, only different lines will be printed
  • vimdiff - compares two files against each other visually with color coding

Managing Process

Processes can work in the "background" or in the "foreground", the difference between the two is the terminal focus position.
Also, there is a mechanism that combines multiple processes that work together as a "job" which can also be managed.
  • bg - sending a process / job to the background allowing you to continue working in the terminal
    • Another easy way to background a process is by appending "&" to the end of the execution command.
  • fg - Sending a process or a job to foreground
  • jobs - shows the current active jobs with an ID number for each, you can use bg and fg in combination with that number.
  • ps - shows active process on the machine
    • -ef - select all process with full format listing
    • -C - searches for the process execution command
  • kill - kills a process by getting a PID

File Monitoring

tail

"tail" display the last lines of a given text file
"-n" enables you to choose number of lines to display

watch

"watch" is a utility that executes a command in a given (or default) time interval
"-n" will determine the interval

File Download

wget

download webpages and files.
-O to choose a new name for the downloaded file

curl

download webpages and files.

axel

A download accelerator, downs a file in multiple streams

Bash Environment Customazation

.bashrc

~/.bashrc is the bash configuration file.

export

a utility to export environment variable for global terminal usage
export HISTTIMEFORMAT='%F %T '

alias

A utility for creating aliases
alias .. ="cd .."