๐Ÿ“œ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

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 .."

Last updated