├── LinuxNetworkingCheatSheet.pdf ├── LinuxCommandLineCheatSheet-JasonCannon.pdf ├── 4-SSH-and-SCP.md ├── 3-Grep-Command.md ├── 1-Redirection-and-Piping.md ├── 2-File-Permissions-and-Sudo.md ├── 0-Linux-Command-Line-Basics.md └── README.md /LinuxNetworkingCheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pushkar100/notes-linux-admin/HEAD/LinuxNetworkingCheatSheet.pdf -------------------------------------------------------------------------------- /LinuxCommandLineCheatSheet-JasonCannon.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pushkar100/notes-linux-admin/HEAD/LinuxCommandLineCheatSheet-JasonCannon.pdf -------------------------------------------------------------------------------- /4-SSH-and-SCP.md: -------------------------------------------------------------------------------- 1 | # SSH & SCP Notes/Reference: 2 | 3 | - [SSH & SCP Notes/Reference:](#ssh---scp-notes-reference-) 4 | * [Secure Shell (SSH):](#secure-shell--ssh--) 5 | + [Connecting:](#connecting-) 6 | * [Secure Copying (SCP):](#secure-copying--scp--) 7 | + [Coyping from Remote to Local:](#coyping-from-remote-to-local-) 8 | + [Coyping from Local to Remote:](#coyping-from-local-to-remote-) 9 | 10 | ## Secure Shell (SSH): 11 | 12 | ### Connecting: 13 | 14 | - `ssh userName@remoteHostAddress` = Connects to remote host via SSH. (Ex: `ssh tester@add1sun.com`) 15 | 16 | If requiring port address: 17 | - `ssh -pXXXX userName@remoteHostAddress` = connects to remote using port number XXXX. (Ex: `ssh -p1234 tester@add1sun.com`) 18 | 19 | **Use lowercase 'p' for port Number** 20 | 21 | Once you login, you may use basic linux commands to move around the directories or manipulate files on the remote system. 22 | 23 | ## Secure Copying (SCP): 24 | 25 | Syntax: 26 | - `scp [options] [portNumber] sourceHostFileOrDirectoryPath destinationDirectoryPath` 27 | 28 | NOTE: Use Uppercase 'P' for port number flag (In SSH, we used lowercase 'p'). 29 | 30 | ### Coyping from Remote to Local: 31 | 32 | 1. Copying a File: `scp -PXXXX userName@remoteHostAddress:filePath localDirectoryPath` 33 | 2. Copying a Directory: `scp -r -PXXXX userName@remoteHostAddress:directoryPath localDirectoryPath` 34 | 35 | Use `-r` flag : Recursively copies directory contents. 36 | 37 | ### Coyping from Local to Remote: 38 | 39 | 1. Copying a File: `scp -PXXXX localDirectoryPath userName@remoteHostAddress:directoryPath` 40 | 2. Copying a Directory: `scp -r -PXXXX localDirectoryPath userName@remoteHostAddress:directoryPath` 41 | 42 | Use `-r` flag : Recursively copies directory contents. 43 | -------------------------------------------------------------------------------- /3-Grep-Command.md: -------------------------------------------------------------------------------- 1 | # `grep` Command: 2 | 3 | ## Introduction: 4 | 5 | Searching for terms within a file or across multiple files? - Use `grep` command. 6 | 7 | ## `grep` Command and Options: 8 | 9 | - `grep 'searchTerm' filePath` = Searches for the search term within the mentioned file. 10 | 11 | - `grep 'searchTerm' *` = Searches for the search term across all the files present in the Current Working Directory. 12 | 13 | (Note: `grep` does **NOT** search for terms within files inside directories that are part of PWD) 14 | 15 | - `grep 'searchTerm' directoryPath/*` = Searches for the search term across all the files present in the Mentioned in the Directory 16 | 17 | - `grep -r 'searchTerm' directoryPath/*` = Searches RECURSIVELY for the term inside specified directory including searching all subdirectory's files 18 | 19 | - `grep -r 'searchTerm' .` = Searches RECURSIVELY for the term inside CURRENT directory including searching all subdirectory's files 20 | 21 | **(OR)** 22 | 23 | - `grep -r 'searchTerm' *` = Searches RECURSIVELY for the term inside CURRENT directory including searching all subdirectory's files 24 | 25 | - `grep -n 'searchTerm' filePath` = Shows matches for the search term along with the Line Number. 26 | 27 | - `grep -l 'searchTerm' filePath` = Shows only matched files for the search term but does not display content where it appears(only file names) 28 | 29 | - `grep -v 'searchTerm' filePath` = Shows Lines(contents of lines) of matched files that DO NOT contain the search term (reverse of the usual grep) 30 | 31 | - `grep -i 'searchTerm' filePath` = Shows matched lines for the search term in matched files by IGNORING CASE. 32 | 33 | Use `-i` option to ignore case of search term while matching. 34 | 35 | ## Source of the Notes: 36 | 37 | Source = https://www.youtube.com/watch?v=3w7xrQWRYrU 38 | -------------------------------------------------------------------------------- /1-Redirection-and-Piping.md: -------------------------------------------------------------------------------- 1 | # Redirection & Pipelining: 2 | 3 | Source: http://linuxcommand.org/lts0060.php 4 | 5 | - [Redirection & Pipelining:](#redirection---pipelining-) 6 | * [Redirection:](#redirection-) 7 | + [Standard Output:](#standard-output-) 8 | + [Standard Input:](#standard-input-) 9 | * [Pipes:](#pipes-) 10 | * [Filters: (Google each filter to learn more about them)](#filters---google-each-filter-to-learn-more-about-them-) 11 | 12 | ## Redirection: 13 | 14 | ### Standard Output: 15 | 16 | Most command line programs that display their results do so by sending their results to a facility called standard output. 17 | 18 | By default, standard output directs its contents to the display. 19 | 20 | To redirect standard output to a file, the `>` character is used like this: 21 | 22 | Ex: 23 | - `ls > file_list.txt` = The output of `ls` command is written to the file mentioned (Contents of file overwritten) 24 | - `ls >> file_list.txt` = Don't want to overwrite existing file contents but APPEND TO file contents. 25 | 26 | If file does NOT exist, it is created. 27 | 28 | ### Standard Input: 29 | 30 | Many commands can accept input from a facility called standard input. 31 | 32 | By default, standard input gets its contents from the keyboard, but like standard output, it can be redirected. 33 | 34 | To redirect standard input from a file instead of the keyboard, the `<` character is used like this: 35 | 36 | Ex: 37 | - `sort < sample.txt` = The input for the sort command is taken from the sample.txt (contents of the file is input) 38 | 39 | **Redirection of both input and output:** 40 | Ex: 41 | - `sort < file_list.txt > sorted_file_list.txt` = Input for sort command is from file_list.txt and output of the sort command is redirected to the file sorted_file_list.txt (new or existing). 42 | 43 | ## Pipes: 44 | 45 | By far, the most useful and powerful thing you can do with I/O redirection is to connect multiple commands together with what are called pipes. 46 | 47 | With pipes, the Standard Output of one command is fed into the Standard Input of another. Pipes are represented with `|`. 48 | 49 | Good Ex: 50 | - `ls -l | less` = The output of the ls command is fed into the less command(as standard input). 51 | 52 | By using this ` | less` trick, you can make any command have scrolling output. We can use this technique all the time. 53 | 54 | **Example: Outputting to multiple Files:** 55 | 56 | Use `tee` command: Copy standard input to each FILE, and also to standard output. 57 | 58 | Syntax: `tee [OPTION]... [FILE(s)]...` 59 | 60 | Ex: 61 | - `tee file1 file2 file3` = Waits for user input (ended with CTRL-C) and puts the input to `file1`, `file2`, and `file3`. 62 | - `ls -l | tee file1 file2` = Takes output of `ls -l` and puts it into the files `file1` and `file2`. 63 | 64 | ## Filters: (Google each filter to learn more about them) 65 | 66 | One class of programs you can use with pipes is called `filters`. 67 | 68 | Filters take standard input and perform an operation upon it and send the results to standard output. In this way, they can be used to process information in powerful ways. Here are some of the common programs that can act as filters: 69 | 70 | **Common filters** 71 | 72 | - `sort` = Sorts standard input then outputs the sorted result on standard output. 73 | - `uniq` = Given a sorted stream of data from standard input, it removes duplicate lines of data (i.e., it makes sure that every line is unique). 74 | - `grep` = Examines each line of data it receives from standard input and outputs every line that contains a specified pattern of characters. 75 | - `fmt` = Reads text from standard input, then outputs formatted text on standard output. 76 | - `pr` = Takes text input from standard input and splits the data into pages with page breaks, headers and footers in preparation for printing. 77 | - `head` = Outputs the first few lines of its input. Useful for getting the header of a file. 78 | - `tail` = Outputs the last few lines of its input. Useful for things like getting the most recent entries from a log file. 79 | - `tr` = Translates characters. Can be used to perform tasks such as upper/lowercase conversions or changing line termination characters from one type to another (for example, converting DOS text files into Unix style text files). 80 | - `sed` = Stream editor. Can perform more sophisticated text translations than tr. 81 | - `awk` = An entire programming language designed for constructing filters. Extremely powerful. 82 | 83 | USE `man` to find out more about the filters. 84 | -------------------------------------------------------------------------------- /2-File-Permissions-and-Sudo.md: -------------------------------------------------------------------------------- 1 | # File Permissions & `sudo` Command: 2 | 3 | - [File Permissions & `sudo` Command:](#file-permissions----sudo--command-) 4 | + [Checking Permissions:](#checking-permissions-) 5 | + [Changing permissions:](#changing-permissions-) 6 | - [chmod 'octal' mode:](#chmod--octal--mode-) 7 | + [Changing Owner (of a file or directory):](#changing-owner--of-a-file-or-directory--) 8 | + [Changing Owner & Group (of a file or directory):](#changing-owner---group--of-a-file-or-directory--) 9 | 10 | 11 | File Permissions set for 3 sets of users: 12 | 1. User (current/owner) 13 | 2. Group 14 | 3. Others 15 | 16 | Each type of user has: 17 | 1. Read(r) 18 | 2. Write(w) 19 | 3. Execute(x) permissions 20 | 21 | - Execute for a folder means we can go into/access the folder. 22 | - Execute for a file means we can run the program/execute the script/etc. 23 | 24 | Denial of a permission is indicated by a `-`. Ex: 25 | - `drwxr-xr--` => means that it is a directory(d), user has all 3 permissions(rwx), group has only read and execute permission(r-x), and others have only read permission(r--). 26 | 27 | ### Checking Permissions: 28 | - `ls -l` => longlisting of files in a directory will show us the permissions it has. 29 | 30 | Format Displayed: 31 | 1. Is Directory? and File permissions (user, group, others) 32 | 2. Owner of the file/dir (A user on the sytem) 33 | 34 | ### Changing permissions: 35 | 36 | Use `chmod` which changes or modifies permissions. 37 | 38 | - `chmod o-rx fileOrDirectoryPath` = Changes permissions for 'others' by removing read and execute permissions '-rx' (for others) on the mentioned File. 39 | 40 | Above might not work sometimes. (Ex: chmod: `abc.txt`: Operation not permitted) 41 | 42 | .. AND THAT'S BECAUSE WE DON'T HAVE THE AUTHORITY TO CHANGE OWNERSHIP. 43 | 44 | IN THIS CASE, WE WILL HAVE TO RUN THE COMMAND AS A 'SUPERUSER' (Therefore, prefix command with `sudo` which will run it as from the superuser - requires the password to superuser login.) 45 | 46 | **Therefore:(Solution)** 47 | - `sudo chmod o-rx fileOrDirectoryPath` = Changes permissions for 'others' by removing read and execute permissions '-rx' (for others) on the mentioned File. 48 | 49 | Another Usage: 50 | - `sudo chmod a=rx fileOrDirectoryPath` = Changes permissions for 'all(u,g,o)' by having only read and execute permissions(no write access). 51 | 52 | Combining it for user, group and others. Ex: `chmod u=rwx,g=rx,o=r myfile` 53 | 54 | #### chmod 'octal' mode: 55 | 56 | Ex: `chmod 754 myfile` 57 | 58 | Here the digits 7, 5, and 4 each individually represent the permissions for the user, group, and others, in that order. Each digit is a combination of the numbers 4, 2, 1, and 0: 59 | 60 | - 4 stands for "read", 61 | - 2 stands for "write", 62 | - 1 stands for "execute", and 63 | - 0 stands for "no permission." 64 | 65 | So 7 is the combination of permissions 4+2+1 (read, write, and execute), 5 is 4+0+1 (read, no write, and execute), and 4 is 4+0+0 (read, no write, and no execute). 66 | 67 | NOTE: 68 | - `+` = Add permissions (Ex: `+rx` => add read and execute permissions) 69 | - `-` = Remove permissions (Ex: `-wx` => remove write and execute permissions) 70 | - `=` = Set permissions to only what's mentioned(on RHS of `=`) and remove un-mentioned permissions 71 | 72 | NOTE: 73 | - u = user 74 | - g = group 75 | - o = others 76 | - a = all (u,g,o) 77 | 78 | We may even combine them. Ex: 'ug' => user and group 79 | 80 | NOTE: 81 | - r = read 82 | - w = write 83 | - x = execute. 84 | 85 | ### Changing Owner (of a file or directory): 86 | 87 | Refers to a user on the system with a login name. (Use `chown`) 88 | 89 | Every file/dir is owned by some user on the system. The default user on the system id ID'ed as `root`. 90 | - `chown userName fileOrDirectoryPath` = Changes the Ownership of mentioned file(s)/dir(s) to the mentioned User. 91 | 92 | ABOVE MIGHT NOT WORK SOMETIMES. Ex: chown: changing ownership of `abc.txt`: Operation not permitted 93 | 94 | .. AND THAT'S BECAUSE WE DON'T HAVE THE AUTHORITY TO CHANGE OWNERSHIP. 95 | 96 | IN THIS CASE, WE WILL HAVE TO RUN THE COMMAND AS A 'SUPERUSER' (Therefore, prefix command with `sudo` which will run it as from the superuser - requires the password to superuser login.) 97 | 98 | **Therefore:(Solution)** 99 | - `sudo chown userName fileOrDirectoryPath` = Changes the Ownership of mentioned file(s)/dir(s) to the mentioned User. 100 | 101 | ### Changing Owner & Group (of a file or directory): 102 | 103 | Refers to a user on the system with a login name. (Again, use `chown`) 104 | 105 | - `chown userName:groupName fileOrDirectoryPath` = Changes the Ownership of mentioned file(s)/dir(s) to the mentioned User and the mentioned Group. 106 | 107 | IF NOT PERMITTED THEN USE `sudo`: 108 | - `sudo chown userName:groupName fileOrDirectoryPath` = Changes the Ownership of mentioned file(s)/dir(s) to the mentioned User and the mentioned Group. 109 | 110 | NOTE: CHANGING USER OR GROUP OWNERSHIP => THEY (USER, GROUP) MUST EXIST! 111 | 112 | NOTE: We can also use `chgrp` command to change group: 113 | - `chgrp groupName fileOrdirectoryPath` = changes group of the file(s)/dir(s) to the mentioned group 114 | 115 | If not permitted, use `sudo`: 116 | - `sudo chgrp groupName fileOrdirectoryPath` = changes group of the file(s)/dir(s) to the mentioned group 117 | 118 | `sudo` prefix can be used to run any command for which we don't have permissions (And not just for file commands, but any command) 119 | -------------------------------------------------------------------------------- /0-Linux-Command-Line-Basics.md: -------------------------------------------------------------------------------- 1 | # Basic CLI Commands: 2 | 3 | Commands are case-sensitive! : All commands are `lowercase`. 4 | 5 | - [Basic CLI Commands:](#basic-cli-commands-) 6 | * [Very Simple Commands:](#very-simple-commands-) 7 | * [Linux Directory Structure:](#linux-directory-structure-) 8 | * [Linux Links:](#linux-links-) 9 | + [Inode:](#inode-) 10 | + [Soft Links (or) Symbolic Link: (For both files and directories)](#soft-links--or--symbolic-link----for-both-files-and-directories-) 11 | + [Hard Links: (Only for Files)](#hard-links----only-for-files-) 12 | + [Creating Hard Links: `ln orginalFilePath newFilePath`](#creating-hard-links---ln-orginalfilepath-newfilepath-) 13 | + [Creating Soft Links: `ln -s originalFilePath newFilePath`](#creating-soft-links---ln--s-originalfilepath-newfilepath-) 14 | * [`ls` Command Options (-):](#-ls--command-options-----) 15 | * [`touch` Command:](#-touch--command-) 16 | * [Make and Remove Empty Directories:](#make-and-remove-empty-directories-) 17 | * [Remove Files and Non-Empty/Empty Directories:](#remove-files-and-non-empty-empty-directories-) 18 | + [With `-i` Option:](#with---i--option-) 19 | + [With `-f` Option:](#with---f--option-) 20 | + [With `-v` option(verbose):](#with---v--option-verbose--) 21 | + [For Deleting Directories:](#for-deleting-directories-) 22 | + [Deleting Files and Directories together:](#deleting-files-and-directories-together-) 23 | * [Copying Files: (Duplicating Files)](#copying-files---duplicating-files-) 24 | + [Copying files into a directory:](#copying-files-into-a-directory-) 25 | + [Copying Directories/Files into a directory:](#copying-directories-files-into-a-directory-) 26 | - [`-i` (Interactive) Flag:](#--i---interactive--flag-) 27 | - [`-v` (Verbose) Flag:](#--v---verbose--flag-) 28 | * [Renaming & Moving(Cut-Paste) Files:](#renaming---moving-cut-paste--files-) 29 | + [Renaming Files:](#renaming-files-) 30 | + [Renaming a Directory:](#renaming-a-directory-) 31 | + [Moving (Cut-Paste) Files:](#moving--cut-paste--files-) 32 | + [Moving (Cut-Paste) Directories:](#moving--cut-paste--directories-) 33 | + [`-i` (Interactive) Flag:](#--i---interactive--flag--1) 34 | * [File Extensions in Linux:](#file-extensions-in-linux-) 35 | + [How to know the TRUE TYPE of a File/Directory?](#how-to-know-the-true-type-of-a-file-directory-) 36 | + [File and Directory Names with Spaces:](#file-and-directory-names-with-spaces-) 37 | * [File and Directory Names with Special Characters:](#file-and-directory-names-with-special-characters-) 38 | + [The TWO characters we can NEVER use in a File or Directory Name:](#the-two-characters-we-can-never-use-in-a-file-or-directory-name-) 39 | * [AUTOCOMPLETION:](#autocompletion-) 40 | * [Keyboard Shortcuts:](#keyboard-shortcuts-) 41 | * [Graphical Text Editor:](#graphical-text-editor-) 42 | * [Terminal Text Editors:](#terminal-text-editors-) 43 | + [`nano` editor:](#-nano--editor-) 44 | * [History of Commands:](#history-of-commands-) 45 | * [Viewing Text Files (Read-only mode):](#viewing-text-files--read-only-mode--) 46 | + [`less` command:](#-less--command-) 47 | + [`cat` command:](#-cat--command-) 48 | + [`tac` command:](#-tac--command-) 49 | + [`head` and `tail` commands:](#-head--and--tail--commands-) 50 | * [Counting Words in a Files:](#counting-words-in-a-files-) 51 | * [Types of Linux Commands:](#types-of-linux-commands-) 52 | + [Finding the Type of a Command:](#finding-the-type-of-a-command-) 53 | + [Finding the Location of an Executable Command:](#finding-the-location-of-an-executable-command-) 54 | * [Description/Information about Commands:](#description-information-about-commands-) 55 | * [Executing Multiple Commands:](#executing-multiple-commands-) 56 | * [Wildcards:](#wildcards-) 57 | * [Aliases:](#aliases-) 58 | + [Deleting an Alias:](#deleting-an-alias-) 59 | + [The `~/.bashrc` file:](#the----bashrc--file-) 60 | 61 | 62 | ## Very Simple Commands: 63 | 64 | - `date` = Returns the current time of the system(day, month, date, time, year) 65 | - `cal` = Displays the calendar for current month(default) [can also use `cal -1`] 66 | - `cal -y` = Displays calendar for whole current year. 67 | - `cal YYYY` = Displays calendar the whole year YYYY. 68 | - `cal X YYYY` (or)`cal XX YYYY` = Displays calendar for month X of year YYYY. 69 | - `cal -3` = Displays calendar for Previous month, Current month, and Next month of current year. 70 | 71 | - `clear` = clears the terminal screen - clears previous commands and outputs (blank slate) 72 | - `exit` = exits the current terminal session 73 | 74 | ## Linux Directory Structure: 75 | 76 | Folders in windows are known as 'Directories' in Linux although both mean the same thing. (Used Interchangeably) 77 | 78 | The Directory structure is Tree-like where `root` (Represented by `/`) forms the top-most node. 79 | 80 | First Level : `root` or `/` 81 | Next Level : 82 | - `bin` = Executable binaries stored here 83 | - `opt` = Stores files NOT installed by default by the OS. Ex: Google Chrome files 84 | - `home` = Every User has a unique directory under the `home` directory under which he can store his own folders and files 85 | - `tmp` = Stores temporary files - do not store imp files here 86 | - `var` = Variables, log files, databases, etc 87 | .... and so on. 88 | 89 | When you login to system as a user, you're Current Working Directory is automatically set to your(user's) home folder. 90 | 91 | - Absolute Paths: Start from `root`. Ex: `/home/pushkar` (Absolute paths start with `/`) 92 | - Relative Paths: Start from Current Working Directory(.) Ex: `./Desktop` or simply `Desktop` if user is in directory containing the Desktop folder 93 | 94 | - `pwd` = Print Working Directory (Displays the absolute pathname of the current working directory). 95 | - `cd` = Changes Current Working Directory to path specified (Ex: cd Desktop) (Can use Absolute/Relative Paths) 96 | - `cd /` = Takes user to Root(/) directory 97 | - `cd ~` (or) `cd` = Takes user to his/her Home(~) directory 98 | - `cd ..` = Takes user to the Parent directory (of the current working directory) 99 | - `cd .` = Takes user to the current working directory (NO CHANGE) [USELESS TO RUN THIS COMMAND] 100 | - `cd -` = Takes user to his/her Previous Working Directory (Ex: user changed directory from home to root, running `cd -` will take him back to home) 101 | 102 | - `ls` = Lists the Current Directory's contents [Default] (ie. Lists files and folders inside current folder) 103 | - `ls /` = Lists contents of Root(/) directory. 104 | - `ls ~` = Lists contents of User's Home(~) directory. 105 | - `ls ..` = Lists contents of Parent Directory (of Current Working Directory) 106 | - `ls .` = Lists contents of Current Working Directory (REDUNDANT -> SAME AS `ls`) 107 | - `ls /absolute/path/name` = Lists contents of Directory listed by the absolute path 108 | - `ls relative/path/name` = List contents of Directory listed by the relative path 109 | 110 | NOTE: `ls -` does NOT list contents of Previous Working Directory (`-` is seen as a flag/option in `ls`) 111 | 112 | Getting the basename of the file/directory: 113 | - `basename fileOrDirPath` = returns just the filename/Directory name after stripping off the path to the file. 114 | 115 | Getting the directory of the file/directory: 116 | - `dirname fileOrDirPath` = Returns directory path to the file, stripping off the file name. (Opp. of basename) 117 | 118 | ## Linux Links: 119 | 120 | ### Inode: 121 | 122 | - Every file in the system has an inode(Index Node) 123 | - It contains all the information except 'file contents' and 'file name' 124 | - Just like a personal ID or a Passport but Without a name! 125 | 126 | Inodes contain the following: 127 | - Inode number 128 | - File Size 129 | - Owner Information 130 | - Permissions 131 | - File Type 132 | - Number of Links (Etc ...) 133 | 134 | ### Soft Links (or) Symbolic Link: (For both files and directories) 135 | 136 | (These are similar to 'Shortcuts' for files on Windows) 137 | 138 | - It's a pointer to the original file 139 | - Just like shortcut in windows 140 | - It has a DIFFERENT Inode Number (Different from the Inode number of the original file) 141 | - Has a SMALLER file size (Significantly smaller file size) 142 | 143 | **Important!:** 144 | - Soft links will contain the SAME data as the Original file 145 | - CHANGING contents of one file will change the contents of the other soft linked files and original file as well. 146 | - IF we "DELETE" the original file, the soft links will become "USELESS" (Since they were pointers to the original file) 147 | 148 | Therefore, we cannot even open/use the softlinks after the original file has been deleted. (Similar to shortcuts in Windows which will not work if we delete the original file) 149 | 150 | ### Hard Links: (Only for Files) 151 | 152 | - Has a different name of the same(original) file 153 | - Has SAME file size 154 | - SAME Inode Number 155 | 156 | Actually, there is nothing like original file since both point to the same file and can't be differentiated. (You can think of a hard link as a copy of the original file but not as a shortcut) 157 | 158 | **Important!:** 159 | - Hard Links will contain the SAME data as the Original File 160 | - CHANGING contents of one file will change the contents of the other hard linked files(incl. original) as well. 161 | - DELETING the original file will NOT affect the contents of the Hard Link files. 162 | 163 | - `ls -i` = Lists contents of directory(CWD b Default) displaying the 'inode number' next to the files/directories listed. (Ex: `ls -i /` lists inode numbers of files/directories contained withing the Root(/) directory) 164 | - `ls -l` = Lists contents of a directory in a 'Long Listing Format'. 165 | 166 | Format: 167 | 1. Is it Directory? 168 | 2. File permissions 169 | 3. Owner of File 170 | 4. Group assigned to File 171 | 5. Number of links 172 | 6. File size in bytes 173 | 7. Date and Time of Last Modification 174 | 8. File name 175 | 176 | 177 | ### Creating Hard Links: `ln orginalFilePath newFilePath` 178 | 179 | Ex: `ln sample.txt newSample.txt` 180 | - Running `ls -i` now will give the same(exact) inode number for both the sample.txt and newSample.txt files 181 | - Running `ls -l` now will give the same(exact) file size(bytes) for both the sample.txt and newSample.txt files 182 | 183 | ### Creating Soft Links: `ln -s originalFilePath newFilePath` 184 | 185 | Pass `-s` option to indicate a soft link. Ex: `ln -s sample.txt newSample.txt` 186 | 187 | Soft links show '-> original file' pointer when we do an `ls -l` 188 | 189 | NOTE: Hard links cannot be created for directories - Throws an error!. But we can create softlinks for directories like: `ln -s originalDirPath newDirPath` 190 | 191 | We can create an Infinite Directory Loop using Soft Links. Ex: Existing folders - '/a' and '/a/b': 192 | - `cd /a/b` 193 | - `ln -s .. c` 194 | Since we used a softlink(c) to b's parent(a) inside b, clicking b and c will repeatedly open new directories in a loop 195 | That is: a/b/c/b/c/b/c/b/c... etc. 196 | 197 | ## `ls` Command Options (-): 198 | 199 | General Syntax: `ls [options] [/path/to/dir]` 200 | 201 | - `ls` = Lists files in Alphabetical Order by default. (Lists from CWD if Directory path is not given.) 202 | - `ls -a` = This will list all the files in your current working directories including hidden files that start with '.' 203 | - `ls -l` = Lists contents of a directory in a 'Long Listing Format'. 204 | (Format: 205 | Is it Directory?, File permissions, Owner of File, Group assigned to File, Number of links, File size in bytes, Date and Time of Last Modification, File name 206 | ) 207 | - `ls -t` = This will list the files sorted by modification date. Newest first. 208 | - `ls -r` = This will list the files in reversed fashion. (Reverse Alphabetical Fashion by default). 209 | 210 | Ex: `ls -tr` lists all the files in the specified directory in reverse time order. Oldest First. 211 | 212 | - `ls -R` = This will RECURSIVELY list all the files and directories from the specified directory (CWD if not specified). 213 | 214 | Ex: `ls -Ra /` will list all the files on your system including hidden files and folders.(HEAVY COMPUTATION) 215 | 216 | - `ls -i` = This will list the index node number of each file in the current working directory. 217 | 218 | Note: We can combine multiple options: Ex: `ls -Rat` = Lists all files/Directories Recursively including hidden files in modification time order(newest first). 219 | 220 | ## `touch` Command: 221 | 222 | To Create a new empty file PLUS to modify time of existing files. 223 | 224 | - `touch existingFilePath` = Used to update last modification time of file to current time. 225 | - `touch existingFilePath1 existingFilePath2 ..` = Used to update last modification time of listed files to current time. 226 | - `touch newFilePath` = Used to Create a new empty file (@specified location) 227 | 228 | ## Make and Remove Empty Directories: 229 | 230 | - `mkdir directoryPath` = Makes a new Empty Directory at specified path. 231 | - `mkdir directoryPath1 directoryPath2 ...` = Makes new Empty Directories at specified paths. 232 | 233 | - `rmdir directoryPath` = Deletes the specified Empty Directory from the system. 234 | - `rmdir directoryPath1 directoryPath2 ...` = Deletes all the specified Empty Directories from system. 235 | 236 | NOTE: `rmdir` command **fails** to **remove non-empty directories** (throws an error message). 237 | 238 | ## Remove Files and Non-Empty/Empty Directories: 239 | 240 | - `rm filePath` = Deletes the specified file from the system. 241 | - `rm filePath1 filePath2 ..` = Deletes files specified from the system. 242 | 243 | ### With `-i` Option: 244 | 245 | Interactive : Ask for confirmation. 246 | 247 | - `rm -i filePath` = Asks for confirmation from user before the removal of specified file. 248 | - `rm -i filePath1 filePath2 ..` = Asks for confirmation from user before the removal of all the specified files. 249 | 250 | ### With `-f` Option: 251 | 252 | Force : Opposite of `-i`, No confirmation/prompt. 253 | 254 | - `rm -f filePath` = forcefully deletes specified file. 255 | - `rm -f filePath1 filePath2 ..` = forcefully deletes all the specified files. 256 | 257 | Ex: Even if you try deleting a non-existent file, which usually throws an error, with `-f` the command gets executed and does not throw an error : Therefore: `-f` ignores non-existent files. 258 | 259 | ### With `-v` option(verbose): 260 | 261 | - `rm -v filePath` = Deletes specified file and prints a summary. 262 | - `rm -v filePath1 filePath2 ..` = Deletes all the specified files and prints a summary. 263 | 264 | ### For Deleting Directories: 265 | 266 | **`-R` Flag is a must!** 267 | 268 | `-R` for this command is NOT case-sensitive!! (`-r` works too!) 269 | 270 | - `rm -R directoryPath` = Deletes the specified directory(empty/not) from the system(along with all its contents) 271 | - `rm -R directoryPath1 directoryPath2 directoryPath3` = Deletes all the specified directories(and their contents) 272 | 273 | - `rm -iR dirPath` = Asks for confirmation from user before the removal of specified dir and for each of its files and sub-directories. 274 | - `rm -iR dirPath1 dirPath2 ..` = Asks for confirmation from user before the removal of all the specified Directories and each of their files and sub-directories. 275 | 276 | The `-R` flag is necessary while deleting Directories. It means delete the directory by deleting everything inside it "recursively". 277 | 278 | - `rm -Rf dirPath` = removes directory and it's files/subdirs without confirmation and ignores non-existent dirs/files. 279 | - `rm -Rf dirPath1 dirPath2 ..` = removes directory and it's files/subdirs without confirmation and ignores non-existent dirs/files. 280 | 281 | - `rm -Rv dirPath` = Deletes specified directory and prints a summary. 282 | - `rm -Rv dirPath1 dirPath2 ..` = Deletes all the specified directories and prints a summary. 283 | 284 | ### Deleting Files and Directories together: 285 | 286 | - `rm -R dirPath1 filePath2 ..` = Since we are deleting directories as well, we need -R flag. 287 | 288 | **Dangerous Command:** 289 | 290 | - `rm -rf /` = Deletes all files on your system without confirmation! (DON'T RUN IT) (Nowadays, some OSes protect against the execution of this command) 291 | 292 | ## Copying Files: (Duplicating Files) 293 | 294 | - `cp sourceFilePath destinationFilePath` = copies contents of source file to destination file 295 | 296 | NOTE: 297 | - If destination does NOT exist = the destination file is CREATED having the contents of the source file. 298 | - If destination does EXISTS = the destination file is MODIFIED to have the contents of the source file. 299 | 300 | ### Copying files into a directory: 301 | 302 | - `cp sourceFilePath1 sourceFilePath2 ... destinationDirectoryPath` = Copies all the specified files into the destination folder. 303 | 304 | ### Copying Directories/Files into a directory: 305 | 306 | Require `-R` option : Recursive. 307 | 308 | - `cp -R sourceDirectoryPath destinationDirectoryPath` = Copies the specified directory into the destination folder. 309 | 310 | NOTE: When copying file/directory into a destination directory, any existing file in the destination that has the same name is overridden/overwritten. 311 | 312 | #### `-i` (Interactive) Flag: 313 | 314 | Used to Confirm/Prompt if same name files/dirs in the destination directory need to be overridden/not! 315 | 316 | - `cp -i sourceFilePath destinationDirectoryPath` 317 | 318 | #### `-v` (Verbose) Flag: 319 | 320 | Same as normal `cp` command but it prints a summary after execution. 321 | 322 | - `cp -v sourceFilePath destinationFilePath` 323 | 324 | ## Renaming & Moving(Cut-Paste) Files: 325 | 326 | ### Renaming Files: 327 | 328 | - `mv oldFileName newFileName` = Renames file with the new name. The newFileName should NOT exist. 329 | 330 | ### Renaming a Directory: 331 | 332 | Same way as renaming a file 333 | 334 | - `mv oldDirName newDirName` = Renames Directory with the new name. The newDirName should NOT exist. 335 | 336 | We can even rename a file/directory into a hidden name by prepending '.' to the new name & vice-versa! 337 | 338 | ### Moving (Cut-Paste) Files: 339 | 340 | - `mv sourceFilePath destinationDirectoryPath` = Move(Cut) source file into destination Directory. 341 | - `mv sourceFilePath1 sourceFilePath2 ... destinationDirectoryPath` = Move(Cut) source file into destination Directory. 342 | 343 | Note: 344 | 1. Destination folder MUST EXIST! 345 | 2. Files with same name existing in destination directory will be Overridden/Overwritten by default 346 | 347 | ### Moving (Cut-Paste) Directories: 348 | 349 | - `mv sourceDirectoryPath destinationDirectoryPath` = Move(Cut) source Directory into destination Directory. 350 | - `mv sourceDirectoryPath1 sourceDirectoryPath2 ... destinationDirectoryPath` = Move(Cut) source directory into destination Folder. 351 | 352 | Note: 353 | - Destination folder MUST EXIST!. 354 | - Directories with same name existing in destination directory will be Overridden/Overwritten by default. 355 | 356 | Note: `mv` Command does NOT require `-R` flag that was required by the copy (`cp`) command!. 357 | 358 | ### `-i` (Interactive) Flag: 359 | 360 | Used to Confirm/Prompt if same name files/dirs in the destination directory need to be overridden/not! 361 | - `mv -i sourceFilePath destinationDirectoryPath` 362 | 363 | ## File Extensions in Linux: 364 | 365 | **File extensions in Linux have no meaning** 366 | 367 | Linux file extensions don't have meaning. If you rename a file and change the extension or remove it, it will still open normally. Ex: 368 | - `rm tux.png penguin` = 'penguin' file will still open/can be opened as an image itself 369 | - `rm a.zip cmpFile` = 'cmpFile' will still open/can be opened as a zip folder itself 370 | 371 | ### How to know the TRUE TYPE of a File/Directory? 372 | 373 | - `file fileName` = Gives information about the type of the file. Ex: 374 | - `file penguin` = penguin: PNG image data, 1979 x 1979, 8-bit/color RGBA, non-interlaced 375 | - `file f` = f: ASCII text 376 | - `file dir1` = dir11: directory 377 | 378 | - `file fileName1 fileName2 ..` = Gives Information about type of all the specified files/directories 379 | 380 | ### File and Directory Names with Spaces: 381 | 382 | Method 1: wrap them in quotes - Either single or double quotes 383 | Ex: 384 | - `mkdir 'my dir'`, 385 | - `rmdir 'my dir'`, 386 | - `touch 'my file'`, etc.. 387 | 388 | Method 2: Prepend one backslash(\) for every space in the file name 389 | Ex: 390 | - `mkdir my\ dir`, 391 | - `rmdir my\ dir`, 392 | - `touch my\ file`, 393 | - `touch my\ \ dog`, (two spaces), etc.. 394 | 395 | ## File and Directory Names with Special Characters: 396 | 397 | The Special Characters are: 398 | ``` 399 | $ 400 | > 401 | < 402 | & 403 | | 404 | ; 405 | " 406 | ' 407 | \ 408 | ``` 409 | These need to be 'escaped' while using them as part of File names. We use backslash(\) to escape these special characters in file names: 410 | Ex: 411 | - `\$file1` = $file1 412 | - `cats\&dogs` = cats&dogs 413 | - `\$\$four\>three` = $$four>three 414 | - `the\ \"quote\"` = the "quote" 415 | - `why\\escape` = why\escape (escaping the backslash character itself) 416 | - `escape\\\\twice` = escape\\twice (to include every 'n' blackslashes, we need '2n' backslashes in the name) 417 | 418 | ### The TWO characters we can NEVER use in a File or Directory Name: 419 | 420 | 1. '/' (forward slash) : Stands for root or as a separator in paths. Therefore, we cannot use it as file/directory name. 421 | 2. The NULL character : (Not so important right now) 422 | 423 | ## AUTOCOMPLETION: 424 | 425 | Use the 'TAB' key to auto-complete: (Write a few characters and Hit Tab). We can autocomplete: 426 | - File Names 427 | - Directory Names 428 | - Paths 429 | - Commands 430 | etc... the files/dirs/cmds must exist for auto-completion. 431 | 432 | If multiple options exist, pressing TAB once more will list all the options below = SUGGESTIONS 433 | 434 | If out of the multiple options, one of them is a substring of the other file, the shorter file name is autocompleted upon TAB. After this, if we type just one character of the bigger filename and hit TAB then it will auto-complete to that. 435 | 436 | ## Keyboard Shortcuts: 437 | 438 | 1. While typing a command on the terminal, if we want to move to the START character of the command then press `CTRL-A` 439 | 2. While typing a command on the terminal, if we want to DELETE the character which CURSOR POINTS to then press `CTRL-D` 440 | 3. While typing a command on the terminal, if we want to move to the END character of the command then press `CTRL-E` 441 | 4. While typing a command on the terminal, if we want to CUT text from CURRENT POSITION to the END of the command then press `CTRL-K` (To PASTE it BACK again, press `CTRL-Y`) 442 | 5. While typing a command on the terminal, if we want to CLEAR THE SCREEN then press `CTRL-L`. 443 | 6. While typing a command on the terminal, if we want to move to ONE WORD FORWARD then press `ALT-F` (DOES NOT WORK ON MAC KEYBOARD) 444 | 7. While typing a command on the terminal, if we want to move to ONE WORD BACKWARDS then press `ALT-B` (DOES NOT WORK ON MAC KEYBOARD) 445 | 8. While typing a command on the terminal, if we want to convert CURRENT WORD to UPPERCASE then press `ALT-U` (DOES NOT WORK ON MAC KEYBOARD) 446 | 9. While typing a command on the terminal, if we want to convert CURRENT WORD to UPPERCASE then press `ALT-L` (DOES NOT WORK ON MAC KEYBOARD) 447 | 448 | ## Graphical Text Editor: 449 | 450 | The default Graphical Text Editor on linux is `gedit`. 451 | - `gedit filePath` = Opens the text file mentioned in gedit.(a GUI program) 452 | 453 | 1. If file exists => gedit opens it for you 454 | 2. If file does NOT exist => gedit creates on and opens it for you 455 | 456 | `gedit` DOES NOT WORK ON MAC by DEFAULT (Needs to be Installed) 457 | 458 | ## Terminal Text Editors: 459 | 460 | There are many: Vi, Vim, Emacs, Nano .. etc 461 | 462 | ### `nano` editor: 463 | 464 | `nano filePath` = Opens the text file mentioned in the terminal. Commands to be used on the editor are displayed inside the editor itself. Ex: ^X => CTRL+X => Exits Editor 465 | 466 | - If file exists => nano opens it for you 467 | - If file does NOT exist => nano creates on and opens it for you 468 | 469 | ## History of Commands: 470 | 471 | Typing will give us the previously type commands (Newest first). Linux could store ~500 commands by default. 472 | 473 | - `history` = This command will list out all the recently used commands by you in a table format: serial number followed by the command. 474 | 475 | - `history xx` = Shows you the last xx number of commands. 476 | 477 | - `!xxx` = Reuse a command that you see in the history table, use: (Exclamation mark(!) followed by the command's serial number). Then the corresponding command gets executed. 478 | 479 | The entire history is stored in a file called `.bash_history` (hidden file) in your Home(`~`) Folder. It contains all the previous commands, one command per line. We can edit this file in order to modify/delete/add to the command history(so that other users don't see it). 480 | 481 | - `history -c` = Clears up your entire history. That is, `.bash_history` is emptied of its contents. 482 | 483 | ## Viewing Text Files (Read-only mode): 484 | 485 | ### `less` command: 486 | - `less filePath` = less is a command that brings up the less text file viewer for the specified file. (less command takes you to a separate/dedicated screen) 487 | 488 | 'less' is a read only mode file viewer. We cannot edit files like we did in nano/gedit. 489 | 490 | Inside less viewer: 491 | - Press `q` to exit less, 492 | - Press `h` to get help on less commands, 493 | - `/searchkey` to search for text, etc.. 494 | 495 | ### `cat` command: 496 | - `cat filePath` = cat command is used to display(print) the contents of a text file on the terminal. (cat does not take you to a separate screen like with less command but shows contents on the terminal itself) 497 | 498 | `cat` stands for 'catenating' 499 | 500 | - `cat filePath1 filePath2 ...` = To view(print on terminal) two or more files by concatenating their contents. (Order of the files matter) 501 | 502 | ### `tac` command: 503 | 504 | - `tac filePath` = Used exactly like `cat` but it reverses the file contents while printing (last line is first) 505 | - `tac filePath1 filePath2 ...` = Used exactly like `cat` but it reverses each of the file contents (last line is first) and then concatenates them and prints content. 506 | 507 | ### `head` and `tail` commands: 508 | 509 | - `head filePath` = Prints/Display the FIRST 10 lines of a file 510 | - `head -n xx filePath` = Prints/Display the FIRST xx lines of a file 511 | 512 | - `tail filePath` = Prints/Display the LAST 10 lines of a file 513 | - `tail -n xx filePath` = Prints/Display the LAST xx lines of a file 514 | 515 | ## Counting Words in a Files: 516 | 517 | - `wc filePath` = Prints the 1. number of lines, 2. number of words, and 3. number of bytes or characters in the specified file. (Any no.of chars (>=1) appearing together and separated from other such blocks by spaces is considered as a 'word') 518 | 519 | - `wc -l filePath` = Prints only the number of lines. 520 | - `wc -w filePath` = Prints only the number of words. 521 | - `wc -c filePath` = Prints only the number of characters or bytes. 522 | - `wc -L filePath` = Prints the Length(number of characters/bytes) of the Longest Line in the file. (capital 'L' flag) 523 | 524 | ## Types of Linux Commands: 525 | 526 | All linux commands can be classified into one of the following types: 527 | 528 | 1. Executable Programs: 529 | - These are normal applications that get executed (Liks any other application on the system). 530 | - They are generally found in the '/bin' or '/usr/bin' folders. 531 | - Common Executable program application exampe => `cp` command 532 | 533 | 2. Shell built-ins: 534 | - These commands are built into the shell. Ex. The bash shell(Bourne Again) Shell . 535 | - A famous shell built-in command is the : `cd` command. 536 | 537 | 3. Shell Scripts: 538 | - These commands exist inside files create by the user and executing this file will execute the commands in it. 539 | - These files are also usually found /bin or /usr/bin although they can be stored anywhere. 540 | 541 | Note: script != executable program (Shell Script is an 'ASCII text executable') 542 | 543 | 4. Aliases: 544 | - Aliases are used to make custom commands. 545 | - They make use of existing commands. 546 | - Even users can make/create aliases. 547 | 548 | Ex: `ls` command is actually an alias command. 549 | 550 | The `shell built-in` commands are the only ones out of the three NOT regarded as executables!. 551 | The other three are `executables`. 552 | 553 | ### Finding the Type of a Command: 554 | 555 | - `type commandName` = Gives you the type of the command. Type can differentiate btw aliases and built-in commands but NOT btween Executable and Shell Scripts -> it displays absolute path of the scripts/executables. Ex: 556 | - `type ls` = ls is aliased to `ls --color=auto' 557 | - `type cd` = cd is a shell builtin 558 | 559 | NOTE: To differentiate btween Executables and Shell scripts: We can do a `type` and then do `file` on the returned absolute path. This gives the type information of the file. Ex: 560 | - `type cp` = cp is /bin/cp 561 | - `file /bin/cp` = Gives output: /bin/cp: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=aac8232e6a5b347942f362c7a27cc3826de39073, stripped 562 | 563 | - Executable Program = 'LSB executable' 564 | - Shell Script = 'ASCII text executable' 565 | 566 | Note: The files could be symbolic links/soft links to other files, which is what is mentioned when we run `type` and then `file` on the command and command file, respectively. So we have to run `file` on those files to gather info about the type of the command. 567 | 568 | THEREFORE, COMMANDS CAN BE LINKS TO OTHER COMMANDS! - REMEMBER. 569 | 570 | ### Finding the Location of an Executable Command: 571 | 572 | The `shell built-in` commands are the only ones out of the three NOT regarded as executables! The other three are executables. 573 | 574 | - `which commandName` = Gives the location of an Executable command in the system (file path). Ex: `which cp` = /bin/cp 575 | 576 | NOTE: `sbin` : Stands for `superuser binary`. Some executables are stored in the `sbin` folder also : Usually contains commands carried out by a system administrator such as networking commands, etc. Ex: `which reboot` = /sbin/reboot 577 | 578 | NOTE: `which` does NOT display anything when used with Shell Built-ins. Ex: `which cd` = **Nothing/No output**. This is because shell built ins are NOT regarded as executables but are commands that are part of the shell itself. Hence, they don't have a path. 579 | 580 | ## Description/Information about Commands: 581 | 582 | - `help commandName` = Gives information about 'SHELL BUILT-IN' commands ONLY!. (Does NOT work on executables. i.e: executable programs, shell scripts, aliases) 583 | 584 | - `man commandName` = Opens a separate Screen displaying Info/Description about an executable command! ('man' stands for manual. Ex: how to use booklet) (Does not work on Shell Built-In commands) 585 | 586 | USE `man` and `help` to learn about new commands you come across by yourself (since learning all the commands in a tutorial at once is impossible) 587 | 588 | - `whatis commandName` = Displays/prints a very short description about the command(what it is and does) (Works only on Executables : Does NOT work on Shell Built-Ins) 589 | 590 | Shell Built-In: use 'help' Executables(shell scripts, executable programs, aliases): 'man', 'whatis' 591 | 592 | ## Executing Multiple Commands: 593 | 594 | **Method 1:** 595 | 596 | We can execute multiple commands at once by separating them out with a `;`. Ex: 597 | - `ls;cal;date` = shows list of files/dirs in cwd THEN shows the calendar for current month THEN current date and time. (The order of commands matter) 598 | 599 | This `;` method IGNORES (i.e shows command not found) all incorrect commands/typos and executes the correct commands! Ex: 600 | - `cal;ls;sfd` = ignores Cal(shows not found msg), prints list of files/dirs, ignores(shows not found msg) sfd. 601 | 602 | Having an `exit` command in the `;` method will IGNORE the remaining commands on the line and Exit/End session. 603 | 604 | **METHOD 2:** 605 | 606 | We can execute multiple commands at once by separating them out with a `&&`. Ex: 607 | - `ls && cal && date` = shows list of files/dirs in cwd THEN shows the calendar for current month THEN current date and time (The order of commands matter) 608 | 609 | The difference btw the `;` and `&&` methods is that the `&&` follows 'Short circuiting' and will not execute after an incorrect/invalid command has been found. (Throws an error message for the incorrect command and stops) 610 | 611 | ## Wildcards: 612 | 613 | Used for matching file or directory names. 614 | 615 | 1. `*` : Matches Any Number of characters (Including 0 chars) Ex: 616 | - `rm *` : Removes EVERY file in the CWD. 617 | - `cp todo* dir1` : Copies every file STARTING with 'todo' to 'dir1' folder. 618 | - `cp *.txt dir1` : Copies every file ENDING with '.txt' to 'dir1' folder.(i.e every text file) 619 | - `cp *app* dir1` : Copies every file CONTAINING the word 'app' to 'dir1' folder. 620 | - `cp n*e dir1` : Copies every file STARTING WITH 'n' and ENDING WITH 'e' to 'dir1' folder. 621 | 622 | 2. `?` : Matches a Single Character. (One '?'' for Each Character required.) Ex: 623 | - `rm ?` : Removes all single character files from the CWD. 624 | - `cp file? dir1` : Copies all files such as file1, file2, filex, etc.. to dir1. 625 | - `cp file??.txt dir1` : Copes all files such as file42.txt, fileab.txt, etc.. to dir1. (One ? for each Char) 626 | 627 | NOTE: We can COMBINE WILDCARDS: Ex: `cp f??e.* dir1` : Copies all files to dir1 that start with 'f' and end with 'e' with any 2 characters in between and then followed by a '.' and any number of characters(any extension). 628 | 629 | 3. `[]` : Specifying Ranges. Ex: 630 | - `rm [abc]` : removes files named either 'a' or 'b' or 'c'. 631 | - `cp [abc]* dir1` : Copies all files to dir1 that begin with either 'a' or 'b' or 'c'. 632 | 633 | NOTE: `!` inside the `[]` does the opposite of the command without the `!`: (COMPLEMENT). Ex: 634 | - `cp [!abc]* dir1` : Copies all files to dir1 that DO NOT begin with either 'a' or 'b' or 'c'. 635 | 636 | NOTE: `-` inside the `[]` represents a RANGE of characters. Ex: 637 | - `cp [0-9]* dir1` : Copies all files to dir1 that begin with a number (0 to 9). 638 | - `cp [0-6]* dir1` : Copies all files to dir1 that begin with a number between 0 and 6 (0 to 6 inclusive) 639 | 640 | NOTE: `[:upper:]` inside a `[]` matches an Uppercase Character. Similarly: 641 | - `[:lower:]` inside a `[]` matches an Lowercase Character. 642 | - '[:digit:]' inside a `[]` matches an Digit. 643 | - '[:alpha:]' inside a `[]` matches a Letter of the Alphabet. 644 | - '[:alnum:]' inside a `[]` matches a Letter of the Alphabet/A Digit (matches an Alphanumeric character). 645 | 646 | Ex: 647 | - `rm [[:upper:]]*` : remove all files beginning with an uppercase character. 648 | - `rm [[:lower:]]*` : remove all files beginning with an lowercase character. 649 | - `rm *[[:digit:]]*` : remove all files containing a digit. 650 | - `rm [![:digit:]]*` : remove all the files that do NOT begin with a digit. 651 | 652 | ## Aliases: 653 | 654 | Creating new command > Very Useful! 655 | 656 | Syntax: `alias newCommandName=""` 657 | 658 | Ex: 659 | - `alias invent="cd Desktop;mkdir newDir"` = The new `invent` command changes directory to Desktop and makes a new directory call newDir. 660 | 661 | We can execute multiple commands on the same line by separating them with `;` therefore, we use it here in aliases. 662 | 663 | How to check if command exists before creating new one? 664 | - Use the `type` command. If error/no output then command does not exist. 665 | 666 | One use case for `aliases` would be that Windows users who work on linux can now create aliases with winodws command name that execute the corresponfing linux instructions. 667 | 668 | ### Deleting an Alias: 669 | 670 | - `unalias aliasedCommandName` = running this will de-recognise the previously aliased command. 671 | 672 | 673 | ### The `~/.bashrc` file: 674 | 675 | Used to permanently save aliases - for all sessions. 676 | 677 | Setting aliases via the alias command on the terminal will not save them once the terminal session is logged out of/ended. That is, they are NOT Saved for the subsequent sessions. 678 | 679 | In order to SAVE aliases permanently(for all subsequent sessions), we must ADD the alias command as a line to the `.bashrc` file inside the Home Directory. That is, Open .bashrc with: 680 | - `cd ~` 681 | - `vim .bashrc` 682 | 683 | (You need not use vim, can use any text editor, including GUI ones). 684 | 685 | Add the alias command (Ex: `alias dir="ls"`) as a line inside the file and SAVE IT. 686 | 687 | We must close the session and restart it for the alias to work. It will work for all subsequent sessions. 688 | 689 | To Delete the permanent alias : Delete that alias's line from the `.bashrc` file inside your home directory. 690 | 691 | NOTE: 692 | - `alias` = Running the alias command WITHOUT ANY ARGUMENTS will print all the aliases that (have been created and) are in existence on the system. (Basically prints all the aliases that the system is using) 693 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Linux Administration Basics Notes: 2 | 3 | Linux Administration Notes & Quick Reference 4 | 5 | - [Linux Administration Basics Notes:](#linux-administration-basics-notes-) 6 | * [What is LINUX?:](#what-is-linux--) 7 | * [Linux Directory Structure](#linux-directory-structure) 8 | + [Common Directories](#common-directories) 9 | + [Application Directory Structure](#application-directory-structure) 10 | * [The Shell](#the-shell) 11 | + [The Prompt](#the-prompt) 12 | * [The Super User](#the-super-user) 13 | * [Basic Linux Commands](#basic-linux-commands) 14 | * [Environment Variables](#environment-variables) 15 | + [PATH Environment Variable](#path-environment-variable) 16 | * [which command](#which-command) 17 | * [help option/flag:](#help-option-flag-) 18 | * [Linux Directories](#linux-directories) 19 | + [How to check the previous working directory](#how-to-check-the-previous-working-directory) 20 | + [Executing commands NOT FOUND in the $PATH](#executing-commands-not-found-in-the--path) 21 | + [Creating & Deleting Directories](#creating---deleting-directories) 22 | + [Listing Files and Directories](#listing-files-and-directories) 23 | + [tree command](#tree-command) 24 | + [Spaces in names](#spaces-in-names) 25 | + [File Permissions](#file-permissions) 26 | - [First character](#first-character) 27 | - [Remaining Characters can be Permission characters](#remaining-characters-can-be-permission-characters) 28 | - [Character meanings for Directories](#character-meanings-for-directories) 29 | - [Permission Categories](#permission-categories) 30 | - [Changing Permissions](#changing-permissions) 31 | * [Modifying permissions](#modifying-permissions) 32 | * [Change group permissions:](#change-group-permissions-) 33 | * [File Creation Mask](#file-creation-mask) 34 | + [umask command](#umask-command) 35 | * [find command](#find-command) 36 | + [The locate command](#the-locate-command) 37 | * [Viewing & Editing Files](#viewing---editing-files) 38 | + [Follow changes to a file in Real Time](#follow-changes-to-a-file-in-real-time) 39 | + [nano editor](#nano-editor) 40 | + [vi editor](#vi-editor) 41 | + [emacs editor](#emacs-editor) 42 | + [Graphical Editors](#graphical-editors) 43 | * [Delete Copy Move and Rename files](#delete-copy-move-and-rename-files) 44 | * [sort command](#sort-command) 45 | * [Create a collection of a group of files](#create-a-collection-of-a-group-of-files) 46 | * [Compress Files](#compress-files) 47 | + [Disk Usage Stats](#disk-usage-stats) 48 | + [tar and gzip](#tar-and-gzip) 49 | * [WildCards](#wildcards) 50 | * [Input Output and Redirection](#input-output-and-redirection) 51 | + [Redirection](#redirection) 52 | + [The Null Device](#the-null-device) 53 | + [Combining input and output redirection](#combining-input-and-output-redirection) 54 | * [Comparing two files](#comparing-two-files) 55 | + [diff Example](#diff-example) 56 | + [sdiff Example](#sdiff-example) 57 | + [vimdiff Example](#vimdiff-example) 58 | * [Searching in files and using pipes](#searching-in-files-and-using-pipes) 59 | + [Finding out the type of a file](#finding-out-the-type-of-a-file) 60 | + [Searching for strings in a binary file](#searching-for-strings-in-a-binary-file) 61 | + [Pipes or Pipelining](#pipes-or-pipelining) 62 | + [`cut` command](#-cut--command) 63 | + [Translating Characters](#translating-characters) 64 | + [Formatting output into columns](#formatting-output-into-columns) 65 | + [more and less commands](#more-and-less-commands) 66 | * [Copying files over the network](#copying-files-over-the-network) 67 | + [SCP](#scp) 68 | + [SFTP:](#sftp-) 69 | * [Customizing the Shell Prompt](#customizing-the-shell-prompt) 70 | * [Shell aliases](#shell-aliases) 71 | * [Environment variables](#environment-variables) 72 | + [Viewing all the environment variables](#viewing-all-the-environment-variables) 73 | + [Creating or modifying environment variables](#creating-or-modifying-environment-variables) 74 | + [Removing environment variables](#removing-environment-variables) 75 | + [Persisting the environment variables settings](#persisting-the-environment-variables-settings) 76 | * [Processes and job control](#processes-and-job-control) 77 | + [Displaying process information](#displaying-process-information) 78 | - [Options for ps](#options-for-ps) 79 | - [Killing a currently running foreground process](#killing-a-currently-running-foreground-process) 80 | - [Suspend a foreground process](#suspend-a-foreground-process) 81 | - [Background processes](#background-processes) 82 | * [Starting a background process](#starting-a-background-process) 83 | + [Listing jobs](#listing-jobs) 84 | + [Forcing processes into the background](#forcing-processes-into-the-background) 85 | * [Killing Processes](#killing-processes) 86 | + [Killing any process using PID](#killing-any-process-using-pid) 87 | + [Killing any process using job number:](#killing-any-process-using-job-number-) 88 | + [Signals have numbers associated with them](#signals-have-numbers-associated-with-them) 89 | + [Summary](#summary) 90 | * [Scheduling repeated jobs with cron](#scheduling-repeated-jobs-with-cron) 91 | + [Redirecting the output of the cron jobs](#redirecting-the-output-of-the-cron-jobs) 92 | + [Using multiple values](#using-multiple-values) 93 | + [crontab command](#crontab-command) 94 | * [Switching users and running commands as others](#switching-users-and-running-commands-as-others) 95 | + [Options for su](#options-for-su) 96 | + [User identification commands](#user-identification-commands) 97 | + [The sudo command](#the-sudo-command) 98 | - [Executing commands with sudo](#executing-commands-with-sudo) 99 | - [Switching users by using sudo su](#switching-users-by-using-sudo-su) 100 | + [Modifying the sudo configuration](#modifying-the-sudo-configuration) 101 | * [Shell history](#shell-history) 102 | + [Viewing history](#viewing-history) 103 | + [Setting the size of the history](#setting-the-size-of-the-history) 104 | + [Repeating commands from history](#repeating-commands-from-history) 105 | + [Reuse or pull out the arguments from the previous command](#reuse-or-pull-out-the-arguments-from-the-previous-command) 106 | + [Searching for commands](#searching-for-commands) 107 | + [Autocompletion](#autocompletion) 108 | * [Installing & managing software](#installing---managing-software) 109 | + [Package manager](#package-manager) 110 | + [The RPM format](#the-rpm-format) 111 | + [Installing packages not included in the package manager](#installing-packages-not-included-in-the-package-manager) 112 | + [General package info commands](#general-package-info-commands) 113 | + [Installing on Debian Distros with the APT package format](#installing-on-debian-distros-with-the-apt-package-format) 114 | + [The dpkg command used in addition to the `apt` utility](#the-dpkg-command-used-in-addition-to-the--apt--utility) 115 | * [The Linux boot process](#the-linux-boot-process) 116 | + [BIOS](#bios) 117 | + [Initial RAM Disk](#initial-ram-disk) 118 | + [The boot directory](#the-boot-directory) 119 | - [Viewing the boot directory](#viewing-the-boot-directory) 120 | + [The kernel ring buffer](#the-kernel-ring-buffer) 121 | - [Location of the kernel messages](#location-of-the-kernel-messages) 122 | * [Linux uses run Levels](#linux-uses-run-levels) 123 | + [Run levels](#run-levels) 124 | + [Setting the run level](#setting-the-run-level) 125 | + [systemd](#systemd) 126 | * [Rebooting](#rebooting) 127 | * [The system log](#the-system-log) 128 | + [Facilities](#facilities) 129 | + [Severities](#severities) 130 | + [rsyslog](#rsyslog) 131 | + [Caching vs non-caching](#caching-vs-non-caching) 132 | * [Disk management](#disk-management) 133 | + [Advantages of partitioning](#advantages-of-partitioning) 134 | + [Master Boot Record](#master-boot-record) 135 | + [GUID Partition Table](#guid-partition-table) 136 | + [Mount points](#mount-points) 137 | + [Mount partitions over existing data](#mount-partitions-over-existing-data) 138 | + [Mount points over other mount points](#mount-points-over-other-mount-points) 139 | + [fdisk to create and modify partitions on a disk](#fdisk-to-create-and-modify-partitions-on-a-disk) 140 | - [CREATE an MBR partition](#create-an-mbr-partition) 141 | - [View all the existing partitions](#view-all-the-existing-partitions) 142 | - [DELETE a partition](#delete-a-partition) 143 | - [SAVING all the partitions added or deleted in the fdisk utility](#saving-all-the-partitions-added-or-deleted-in-the-fdisk-utility) 144 | - [QUITTING without Saving](#quitting-without-saving) 145 | - [CREATING a GPT Partition](#creating-a-gpt-partition) 146 | + [File systems](#file-systems) 147 | - [Create a file system](#create-a-file-system) 148 | + [Mounting a device partition](#mounting-a-device-partition) 149 | - [Viewing the currently mounted file systems](#viewing-the-currently-mounted-file-systems) 150 | - [Unmount a file System](#unmount-a-file-system) 151 | - [Preparing a swap space](#preparing-a-swap-space) 152 | + [The file system table](#the-file-system-table) 153 | - [Viewing labels and UUIDs of file systems](#viewing-labels-and-uuids-of-file-systems) 154 | - [Labelling a file system](#labelling-a-file-system) 155 | * [Managing users and groups](#managing-users-and-groups) 156 | + [The root account](#the-root-account) 157 | + [Passwords are stored in a shadow file](#passwords-are-stored-in-a-shadow-file) 158 | + [UIDs](#uids) 159 | + [GIDs](#gids) 160 | + [Comment field](#comment-field) 161 | + [Home directory](#home-directory) 162 | + [Shell](#shell) 163 | + [The /etc/shadow file](#the--etc-shadow-file) 164 | + [Creating a user account](#creating-a-user-account) 165 | + [Create a password for the created user](#create-a-password-for-the-created-user) 166 | + [System or application accounts](#system-or-application-accounts) 167 | - [The -m option](#the--m-option) 168 | + [Deleting an account](#deleting-an-account) 169 | + [Mpdify an existing account](#mpdify-an-existing-account) 170 | + [Group details and creation](#group-details-and-creation) 171 | + [The /etc/gshadow file](#the--etc-gshadow-file) 172 | - [Create groups](#create-groups) 173 | - [Delete a group](#delete-a-group) 174 | - [Modify a group:](#modify-a-group-) 175 | * [Special permission modes](#special-permission-modes) 176 | + [The setuid bit](#the-setuid-bit) 177 | - [Security measures](#security-measures) 178 | - [Octal permissions](#octal-permissions) 179 | - [Adding the setuid attribute to a file](#adding-the-setuid-attribute-to-a-file) 180 | - [Removing the setuid attribute from a file](#removing-the-setuid-attribute-from-a-file) 181 | - [Find all the files on the system that have setuid set](#find-all-the-files-on-the-system-that-have-setuid-set) 182 | + [The setgid bit](#the-setgid-bit) 183 | - [Examples of commands using this setgid bit](#examples-of-commands-using-this-setgid-bit) 184 | - [Finding setgid files](#finding-setgid-files) 185 | - [Adding setgid permission](#adding-setgid-permission) 186 | - [Removing the setgid attribute from a file](#removing-the-setgid-attribute-from-a-file) 187 | - [Adding both setuid and setgid](#adding-both-setuid-and-setgid) 188 | + [The sticky bit](#the-sticky-bit) 189 | - [Adding the sticky bit](#adding-the-sticky-bit) 190 | - [Removing the sticky bit](#removing-the-sticky-bit) 191 | - [Reading the ls command output](#reading-the-ls-command-output) 192 | * [Networking](#networking) 193 | + [TCP/IP:](#tcp-ip-) 194 | - [IPv4 Classes](#ipv4-classes) 195 | - [Classless Inter-Domain Routing](#classless-inter-domain-routing) 196 | - [Reserved private address space](#reserved-private-address-space) 197 | - [Knowing the host computer IP address](#knowing-the-host-computer-ip-address) 198 | - [Another way to determine the host IP address](#another-way-to-determine-the-host-ip-address) 199 | - [DNS Hostnames](#dns-hostnames) 200 | - [Domains](#domains) 201 | - [Viewing the hostname](#viewing-the-hostname) 202 | - [Setting the hostname](#setting-the-hostname) 203 | - [Resolving DNS names](#resolving-dns-names) 204 | - [The hosts file](#the-hosts-file) 205 | * [DHCP static and dynamic addressing](#dhcp-static-and-dynamic-addressing) 206 | + [Ports](#ports) 207 | + [DHCP](#dhcp) 208 | - [Configuring a DHCP Client](#configuring-a-dhcp-client) 209 | - [Configuring an Ubuntu based System](#configuring-an-ubuntu-based-system) 210 | + [GUI or TUI Tools for networking](#gui-or-tui-tools-for-networking) 211 | * [Network troubleshooting](#network-troubleshooting) 212 | + [Test connectivity to a host with ping](#test-connectivity-to-a-host-with-ping) 213 | + [Testing connectivity over Hops](#testing-connectivity-over-hops) 214 | - [Output of traceroute](#output-of-traceroute) 215 | - [Alternative to traceroute](#alternative-to-traceroute) 216 | + [The netstat command](#the-netstat-command) 217 | + [Packet sniffing with tcpdump](#packet-sniffing-with-tcpdump) 218 | + [The obsolete telnet command](#the-obsolete-telnet-command) 219 | * [Connecting via SSH to a Linux Virtual Machine](#connecting-via-ssh-to-a-linux-virtual-machine) 220 | 221 | ## What is LINUX?: 222 | 223 | Collection of Software that makes up an Operating System. 224 | 225 | Linux OS = Linux Distribution 226 | 227 | A distribution differs from others in the type of software that it contains for a particular application. Ex: Different Linux distributions might have different default browsers, but all have a browser. 228 | 229 | Distros/Flavors = Distributions. Ex: Red Hat Enterprise Linux, Ubuntu (Most Popular). 230 | 231 | Red Hat = Popular in Banks, Airlines, Telecom and Healthcare sectors. (Red Hat - Need to pay for license.) 232 | 233 | Free Version of Red Hat for personal use = CentOS - A free brand of Red Hat Linux 234 | 235 | Ubuntu = Popular with Startups, SaaS, Social Networks, Cloud Based. 236 | 237 | Linux Kernel = Core of the OS (ALL Distros have the SAME Linux Kernel) 238 | 239 | Linux Kernel + Applications = A Linux Distro. 240 | 241 | (Other Distros: Linux Mint, Debian, Mageia, openSUSE, Fedora, ArchLinux, Slackware.) 242 | 243 | NOTE: Distros are only slightly different from each other. Linux kernel is at the core of every distro, so the main concepts are the same for all and learning to do something in one distro is not very different from trying to do the same thing in another distro. 244 | 245 | ## Linux Directory Structure 246 | 247 | Linux Directories === Windows Folders (Folders and Directories are used interchangeably.) 248 | 249 | 250 | ### Common Directories 251 | 252 | **8 'must know' directories!** 253 | 254 | - `/` => "root" (or, just `slash`) is the top level of the file system hierarchy. 255 | - `/bin` => Contains binaries or executable programs. 256 | - `/etc` => System Configuration Files 257 | - `/home` => Home Directories (of the users on the system) 258 | - `/opt` => Optional or Third Party Software (Ex: `Google Earth`s files and executables) 259 | - `/tmp` => Temporary space, usually cleared on reboot(DONT have important stuff that you want to SAVE) 260 | - `/usr` => User related programs. 261 | - `/var` => Variable Data, most notable being the `log files` (system log files.) 262 | 263 | Directories can have important sub-directories too: Example:- 264 | - `/usr` := 265 | - `/usr/bin` = Stores binaries/executables of user programs. 266 | - `usr/lib` = User libraries. 267 | 268 | - `/home` := 269 | - `/home/pushkar` = Refers to the home folder of 'pushkar', a system user. (~) 270 | - `/var/log` = The variables directory containing a subdirectory 'log' that holds system log messages. 271 | 272 | Other Important Directories: 273 | - `/boot` => Contains files needed to boot the operating system. 274 | - `/cdrom` (or) `/mount` (or) `/mnt` => Mount-point for CD-ROMs/removable media/external file systems. 275 | - `/cgroup` => Control groups hierarchy. 276 | - `/dev` => Device Files, typically controlled by Operating Sytem and System Administrators. 277 | - `/export` => Shared file systems. 278 | - `/lib` (or) `/lib64` => System libraries (or) System libraries(64bit). 279 | - `/lost+found` => Used by OS to recover files after a file system check has been performed. 280 | - `/proc` => Provides information about running processes. 281 | - `/sbin` => System administration binaries. 282 | - `/selinux` => Displays information about SELinux. 283 | - `/sys` => Used to display and sometimes configure the devices known to the Linux Kernel. 284 | 285 | Some Server Related Directories: 286 | - /srv => Contains data which is served by the system. 287 | - /srv/www => Contains Web Server files. 288 | - /srv/ftp => Contains FTP files. 289 | 290 | ### Application Directory Structure 291 | 292 | - Third Party Applications can be in `/usr/local` Directory: 293 | Some applications that are NOT BUNDLED(Third Party) with the Linux OS by default are stored in the: `/usr/local` directory. These application directories have their own Linux-Likes sub-directory structure. Ex: 294 | 295 | - `/usr/local//etc` => Application's configuration files(at runtime). 296 | - `/usr/local//bin` => Application's binary(executable) files. 297 | - `/usr/local//log` => Application's log files(execution log messages). 298 | 299 | - Third Party Applications can also be in `/opt` Directory. Ex: 300 | 301 | - `/opt//bin` 302 | - `/opt//etc` 303 | - `/opt//lib` 304 | - `/opt//log`, ... etc. 305 | 306 | 307 | NOTE: 308 | - Sometimes, even though third-party applications get installed in `usr/local` or `/opt`, they can save/install some of their files in Other Directories, too. Ex: 309 | - `/etc/opt/` 310 | - `/var/opt/` 311 | 312 | - Sometimes when third-party applications are installed, they are not given their own directory structure, but instead they are installed in a 'Shared' Manner. Ex: 313 | - `/usr/local/bin/` 314 | - `/usr/local/etc/.conf` 315 | - `/usr/local/lib/.so` 316 | 317 | - We could use organisation/company name and store all the application(s)' files belonging to a particular organization under one folder. Ex: 318 | - `/opt//etc` (or) `/opt///bin` (or) `/opt///lib` (or) `/opt//` => Scrolls down one Line at a time. 367 | - `` => Scrolls down one Page at a time. 368 | - `G` (shift-g) => Scrolls down to the Bottom of the man output. 369 | - `g` => Scrolls down to the Top of the man output. 370 | - `q` => Quits the man pages application. 371 | 372 | ## Environment Variables 373 | 374 | They are storage locations containing `name` and `value` pairs. They are typically in UPPERCASE. 375 | 376 | Access environment variable contents by executing: `echo $VAR_NAME` 377 | 378 | ### PATH Environment Variable 379 | 380 | PATH is an environment variable. It Contains a list of directories, separated by a colon (:). 381 | 382 | It controls the 'command search path' = Means that whenever we enter a command at the command line, the system searches for that command in the command paths existing inside the $PATH variable. If it finds it, executes the command, else moves onto the next path and searches for it there and so on. If command is not found in any of these directories, it returns a command-not-found error. 383 | 384 | Ex: 385 | - `echo $PATH` => /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin:/usr/local/Cellar/mongodb/3.2.4/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin 386 | 387 | (Searches for commands in every directory separated by a colon starting from the first one that is listed). 388 | 389 | Note: If the same command exists inside multiple paths, then the one found in an earlier path is executed for that command. 390 | 391 | ## which command 392 | 393 | **`which`** 394 | 395 | Searches for the path of a command. (Location of the file which runs on the command execution). Ex: 396 | - `which cat` => /bin/cat 397 | - `which cd` => /usr/bin/cd 398 | 399 | ## help option/flag: 400 | 401 | **`--help`** 402 | 403 | Prints a brief description of the command on the terminal. Ex: 404 | - `ls --help` 405 | 406 | (Sometimes, even `-h` works) 407 | 408 | Use `man ` if ` --help` does NOT work. 409 | 410 | ## Linux Directories 411 | 412 | Containers for other files and directories. They give a tree like structure to the file system. Can be accessed by 'name' or a 'shortcut'(symbolic link or soft link) 413 | 414 | - `.` => current working directory. 415 | - `..` => parent working directory. 416 | - `-` => Usually refers to the previous working directory. 417 | 418 | Ex: `cd -` => changes directory to the previous working directory. 419 | 420 | NOTE: `ls -` does NOT work! 421 | 422 | ### How to check the previous working directory 423 | 424 | `$OLDPATH` is an environment variable holding the path of the previous wroking directory. Ex: 425 | - `echo $OLDPATH` => '/home' 426 | 427 | ### Executing commands NOT FOUND in the $PATH 428 | 429 | (Also works for directories found in $PATH) 430 | 431 | - We can use the FULL(ABSOLUTE) PATH of the command (location of the command) to execute it. 432 | 433 | (Can be used to execute shell scripts also) 434 | 435 | Syntax: `/full/path/to/command` 436 | 437 | Ex: 438 | - `/bin/cat datafile.txt` => '/bin/cat' is the full path of the `cat` command. (datafile.txt is in CWD) 439 | 440 | - Use `./command` to execute the command residing in/relative to the current working directory. 441 | 442 | (Can be used to execute shell scripts also) 443 | 444 | Syntax: `./relative/path/to/command` (`.` represents PWD) 445 | 446 | Ex: 447 | - `./myscript.sh` => Executees myscript.sh that is in the current directory(.) . 448 | 449 | ### Creating & Deleting Directories 450 | 451 | - `mkdir directory` => Create an Empty directory. 452 | - `rmdir directoty` => Remove/Delete an Empty directory. 453 | - `rm -rf directory` => Forcibly delete a directory(empty or not, all contents deleted - recursively) 454 | 455 | - `mkdir -p directory` => Create Empty directories along with Parent directories(if not existing). 456 | 457 | Ex: 458 | - `mkdir -p one/two/three` => Creates an empty directory `one` in te CWD containing directory 'two' which contains directory `three` 459 | 460 | - `rmdir -p directory` => Deletes empty directories including the specified empty parent directories. 461 | 462 | Ex: 463 | - `rmdir -p one/two/three` => Deletes the nested empty directories `one`, `two` and `three` 464 | 465 | NOTE: When you delete something from the CLI, it's gone forever. NO TRASH! from which we can retrieve 466 | 467 | ### Listing Files and Directories 468 | 469 | - `ls -l` => Long Listing (Permissions, number of links, owner, group, file size in bytes, last modification time, file name) 470 | - `ls -a` => List Hidden files as well (files that begin with a '.' / period) 471 | - `ls -F` => Reveals file types 472 | 473 | If name "ends" in: `/` => Directory, `@` or `->` => Link, `*` => Executable. 474 | 475 | - `ls -t` => List files by time. (Most recently modified file first) 476 | - `ls -r` =? List files in reverse order. 477 | - `ls -R` => Lists files Recursively. (Files of subdirectories and their subdirectories .. so on) 478 | 479 | Others: 480 | - `ls -d` => List Directory names but not contents (of the listed directories). 481 | - `ls --color` => Colorize the output. 482 | 483 | ### tree command 484 | 485 | **`tree`** 486 | 487 | Similar to `ls -R`. But, it's more of a visual output only. (Using tree-like lines) Ex: 488 | - `tree -d` => List Directories only. 489 | - `tree -C` => Colorize the output. 490 | 491 | (`tree` command may not be available as a command by default. Will need to add it.) 492 | 493 | ### Spaces in names 494 | 495 | **(file or directory names)** 496 | 497 | 1. Spaces have to be escaped(Ex: `cd /home/pushkar/Technical\ Notes`) 498 | 499 | (or) 500 | 501 | 2. Use quotes('' or "") (Ex: `cd 'My Notes.txt'`) 502 | 503 | Instead, try using: '-' (dashes), '_' (underscores), or camelCase. (Try to avoid spaces while naming!) 504 | 505 | ### File Permissions 506 | 507 | Ex: `-rwx-wxr--` => type-of-file(1CHAR) owner-perms(3CHAR) group-perms(3CHAR) other-perms(3CHAR) 508 | 509 | #### First character 510 | 511 | Refers to the **type of file**? 512 | - `-` => Regular File 513 | - `d` => Directory File 514 | - `l` => Symbolic Link 515 | 516 | #### Remaining Characters can be Permission characters 517 | 518 | - `r` => Read Permission (View Contents of File) 519 | - `w` => Write Permission (Modify or Change Contents of File) 520 | - `x` => Execute Permission (Run the File as a Program) 521 | 522 | '-' means that corresponding permission has been denied 523 | 524 | #### Character meanings for Directories 525 | 526 | **'r', 'w', and 'x'** 527 | 528 | - `r` => Allows File Names inside the Directory to be read. 529 | - `w` => Allows Entries to be Modififed within the Directory. 530 | - `x` => Allows access to Contents and MetaData for Entries. 531 | 532 | #### Permission Categories 533 | 534 | - `u` => user 535 | - `g` => group 536 | - `o` => other 537 | - `a` => all 538 | 539 | - Groups(g): Every user -> Belongs to at least one group. A user maybe part of multiple groups. Groups are used to organize users. 540 | 541 | Check all the groups which a user belongs to: `groups` (or) `id -Gn` (Same output for both) 542 | 543 | #### Changing Permissions 544 | 545 | `chmod`, `chgrp`, and `chown` commands. 546 | - ugoa: Category (ctgry) 547 | - +-=: add, subtract or set permissions (oprtr) 548 | - rwx: read, write, & execute (prmssn) 549 | 550 | ##### Modifying permissions 551 | 552 | **`chmod`** 553 | 554 | - Symbolic Notation: `chmod fileOrDirectory`. Ex: 555 | - `chmod g+w data-file.txt`, (Add) 556 | - `chmod a=r data-file.txt`, (Set) 557 | - `chmod g+w,o-x data-file.txt` (Multiple) ... etc. 558 | 559 | - Octal Notation: `chmod fileOrDirectory`. Read(r) = 4, Write(w) = 2, Execute(x) = 1. Therefore: 560 | - 7 (all permissions: read, write and execute) 561 | - 6 (read and write) 562 | - 5 (read and execute) 563 | - 4 (read), 564 | - 3 (write and execute) 565 | - 2 (write) 566 | - 1 (execute)] 567 | 568 | Ex: 569 | - `chmod 761 data-file.txt`, (rwx for user, only rw for group, only x for others) 570 | - `chmod 400 data-file.txt`, (read for user, no permissions for group or others) 571 | 572 | Common octal combos: 700, 755, 664, 660, 644 573 | 574 | Don't give 777 permission => Gives everyone access to everything about that file. (Malicious code can modify permissions once again (removing you, perhaps) or modifying file or directory contents in ways you did expect.) 575 | 576 | ##### Change group permissions: 577 | 578 | **`chgrp`** 579 | 580 | Changes the group that the file belongs to: `chgrp fileOrDirectory` 581 | 582 | Ex: 583 | - `chgrp sales sales-data.txt` (For example, we can even move the file to the shared folder '/usr/local/sales' so that people belonging to the sales group can edit the file there.) 584 | 585 | NOTE: IF FILE PERMISSIONS SEEM CORRECT BUT YOU STILL CAN'T DO WHAT YOU WANT TO DO, CHECK THE DIRECTORY PERMISSIONS, THEN THE PARENT DIRECTORY PERMISSIONS.. AND SO ON UNTIL YOU FIND THE PERMISSION THAT NEEDS TO BE UNBLOCKED OR UNTIL YOU REACH THE ROOT(/) DIRECTORY. 586 | 587 | ## File Creation Mask 588 | 589 | **umask** 590 | 591 | The file creation mask decides what permissions must a file or directory have (by default) when it is created!. 592 | 593 | If no mask is set: 594 | - `777` => For Directories, 595 | - `666` => For Files 596 | 597 | ### umask command 598 | 599 | **`umask`** 600 | 601 | Syntax: `umask [-S] [mode]` (-S stands for 'symbolic notation') 602 | 603 | `umask` "subtracts" permissions (opposite of `chmod`): Ex: If base is '777' and mask is '022', it would subtract 022 from 777. So, new permission = 755. (umask of 002 is ideal for working with groups since it gives your group permission to work with files) 604 | 605 | `umask` sometimes needs to MAKE APPROXIMATIONS: Ex: Base = 666 and umask = 007 then final file permission is '660' (and not 66-1) 606 | 607 | Usage Examples: 608 | - `umask` => View the current umask setting (Ex: 0022) 609 | - `umask -S` => View the current umask setting in Symbolic Notation (Ex: u=rwx,g=rx,o=rx [Displays the allowed permissions]) 610 | 611 | - `umask 002` => Changes the umask to 002 (popular way of changing permissions) 612 | - `umask -S u=rwx,g=rx,o=rx` 613 | 614 | Note: Usually in `umask` and `chmod` the 4th MSbit is ignored. Ex: 0644 = 644, 0022 = 022. But, The 4th MSBit can sometimes denote Special Modes: Ex: 615 | 1. setuid, 616 | 2. setgid, 617 | 3. sticky [Covered Later] 618 | 619 | ## find command 620 | 621 | **`find`** 622 | 623 | Syntax: `find [path...] [expression]` 624 | 625 | Recursively finds files in the path that match the expression. If no arguments are supplied, it finds all files in the current directory. (Ex: `find`) 626 | 627 | Options: 628 | - `-name pattern` => Finds files and Directories that match that pattern. 629 | - `-iname pattern` => Like name, but ingores case. 630 | - `-ls` => Perform an `ls` on each of the found items. 631 | 632 | - `-mtime days` => Finds files that are 'days' old. *('+' => More than, '-' Less than )* 633 | - `-size nums` => Find files that are size of 'num'. *('+' => More than, '-' Less than )* 634 | - `-newer file` => Find files that are newer than 'file' 635 | - `-type d` => Find files that are of type `d` (directory) [@ => links, * => executable] 636 | 637 | - `exec {} \;` => Run 'command' against all the files that are found. 638 | 639 | Examples: 640 | - `find` => Recursively lists all files under the current directory. 641 | - `find /sbin -name makedev` = > Searches for files named 'makedev' inside '/sbin' directory. 642 | - `find /sbin -iname makedev` = > Searches for files named 'makedev' inside '/sbin' directory(IGNORE CASE) 643 | - `find /sbin -name makedev` = > Searches for files named 'makedev' inside '/sbin' directory. 644 | - `find /sbin -name *v` = > Searches for files ending 'v' inside '/sbin' directory. 645 | - `find /sbin -name makedev` = > Searches for files named 'makedev' inside '/sbin' directory. 646 | 647 | - `find . -mtime +10 -mtime +13` = > Searches for files more than 10 days old but less than 13 days old inside current(.) directory. 648 | 649 | - `find . s* -ls` => Recursively find anything that starts with 's' in CWD(.) and perform `ls` on it. 650 | - `find . -size +1M` => Recursively find files in CWD(.) that are 1 MegaByte or larger. (K = kilo, G = giga) 651 | 652 | - `find . -newer file.txt` => Searches for files that are newer than the file.txt file(modif. time-wise) 653 | 654 | - `find . exec file {} \;` => Finds all files in the CWD and executes command 'file' against all of them. 655 | 656 | ### The locate command 657 | 658 | **`locate`** 659 | 660 | Syntax: `locate pattern` 661 | 662 | Faster than find. Queries an index(adv.), but results are NOT in real-time.(disadv.) May NOT be enabled on all systems. 663 | 664 | ## Viewing & Editing Files 665 | 666 | Basic commands: 667 | - `cat file` => Display the contents of a file. 668 | - `more file` => Browse through a text file. 669 | - `less file` => Display the more than the 'more' command (less is actually more!) -> 'q' to exit. 670 | - `head [-x] file` => Output the top portion(x lines) of the file (Default: 10 lines) 671 | - `tail [-x] file` => Output the bottom portion(x lines) of the file (Default: 10 lines) 672 | 673 | ### Follow changes to a file in Real Time 674 | 675 | **`tail -f file` => follow the file** 676 | 677 | To view the changes to a file in real-time, use `tail -f` but not 'cat'(not real-time). 678 | 679 | Ex: log files being written to -> use 'tail'on that log file : tail gets updated as file grows (to exit press 'CTRL-C') 680 | 681 | Browsing through a 'more' or 'less' command screen => Same controls as in 'man' pages (Refer 'man') 682 | 683 | ### nano editor 684 | 685 | **`nano`** 686 | 687 | Small text editor. Easy to learn and use. Control commands appear on the screen itself. Not very powerful. 688 | 689 | Open a text file in `nano`: `nano fileName` 690 | 691 | 692 | ### vi editor 693 | 694 | **`vi`** 695 | 696 | More powerful than 'nano'. Requires a learning curve. Commands are Not intuitive. 697 | 698 | - `vi [file]` => Edit file 699 | - `vim [file]` => Same as `vi` but improved/has more features. 700 | - `view [file]` => Starts vim in 'read-only' mode. 701 | 702 | `vi` has three modes: 703 | 1. Normal(Command) 704 | 2. Insert 705 | 3. Line(Visual?) 706 | 707 | `vi` commands: 708 | - Movement: 709 | - `k` - up one line 710 | - `j` - down one line 711 | - `h` - left one character 712 | - `l` - right one character 713 | - `w` - right one word 714 | - `b` - left one word 715 | - `^` - go to the beginning of line 716 | - `$` - go to the end of line 717 | 718 | - Inserting Text: 719 | - `i` - insert at cursor position 720 | - `I` - insert at the beginning of line 721 | - `a` - append after cursor position 722 | - `A` - append at the end of line 723 | - `o` - appends new(empty) line Below current line and moves cursor to it 724 | - `O` - appends new(empty) line Above current line and moves cursor to it 725 | 726 | - `vi` Line Mode: 727 | - `:w` - Writes(Saves) file 728 | - `:w!` - Forces the file to be saved 729 | - `:q` - Quit(come out of vi) 730 | - `:q!` - Quit without saving changes (force quit) 731 | - `:wq` - Write and quit 732 | - `:wq!` - Write and quit forcefully 733 | - `:x` - same as ':wq' 734 | - `:n` - go to line 'n' (Ex: ':50' goes to line 50) 735 | - `:$` - Positions cursor on the last line 736 | - `:set nu` - Turn On line numbering 737 | - `:set nonu` - Turn Off line numbering 738 | - `:help [subcommand]` - Get Help 739 | 740 | - Deleting Text: 741 | - `x` - delete a character (at cursor position) 742 | - `dw` - delete a word (from cursor position) 743 | - `dd` - delete a line (from cursor position) 744 | - `D` - Delete from the current position to end of line (Delete remaining text on line) 745 | 746 | - Replacing/Changing text: 747 | - `r` - replace the current character 748 | - `cw` - change the current word 749 | - `cc` - change the current line 750 | - `c$` (or) `C` - change text from current position to the end of the line($) 751 | - `~` - reverses the case of the character(upper <=> lower) 752 | 753 | NOTE: We can repeat a command by preceding it with a number. Ex: 754 | - `5k` => Move up 5 lines 755 | - `80i` => Insert entered text 80 times at cursor position 756 | 757 | - Copying and Pasting: 758 | - `yy` - yank(copy) the current line 759 | - `y` - Yank the position 760 | - `p` - paste the most recently deleted or yanked(copied) text. 761 | 762 | 7. Undoing and redoing: 763 | - `u` - Undo 764 | - `` - Redo 765 | 766 | 8. Searching: 767 | - `/` - Forward Search(First match to Last match) 768 | - `?` - Reverse Search(Last match to First match) 769 | - `n` - go to Next match 770 | - `N` - go to Previous match 771 | 772 | 8. `vi` Modes (How to get into them): 773 | - `` - Normal Mode 774 | - `i`, `a`, `o`,.. etc - Insert Mode 775 | - `:` - Line Mode 776 | 777 | Need vim help? Type `vimtutor` and hit enter at the command prompt. 778 | 779 | ### emacs editor 780 | 781 | **`emacs`** 782 | 783 | Also a powerful editor. Some people use vi, some use emacs. => Choose whatever you're comfortable with. 784 | 785 | Opening a file: `emacs [file]` (edit file) 786 | 787 | Emac command guide: 788 | - `C-` : means hold down CTRL while pressing character 789 | - `M-` : means hold down either ALT while pressing character (or) means press ESC key, release it, and then type a character. 790 | 791 | - `C-h` : Help 792 | - `C-x C-c` : Exit 793 | - `C-x C-s` : Save the file 794 | - `C-h t` : Built-in tutorial 795 | - `C-h k ` : Describe the key 796 | - `C-p` : Previous line 797 | - `C-n` : Next line 798 | - `C-b` : Backward one character 799 | - `C-f` : Forward one character 800 | - `M-f` : Forward one word 801 | - `M-b` : Backward one word 802 | - `C-a` : Go to beginning of the line 803 | - `C-e` : Go to end of the line 804 | - `M-<` : Go to beginning of the file 805 | - `M->` : Go to end of the file 806 | - `C-d` : Delete a character 807 | - `M-d` : Delete a word 808 | - `C-k` : Kill(cut) 809 | - `C-y` : Yank(paste) 810 | - `C-x` u : undo 811 | - `C-u N ` : Repeat Command N times 812 | 813 | ### Graphical Editors 814 | 815 | Some of the graphical editors are: 816 | 1. emacs - emacs has a graphical mode too 817 | 2. gedit - The default text editor for GNOME Desktop environment (Simialr to notepad) 818 | 3. gvim - The graphical version of vim 819 | 4. kedit - The default text editor for the KDE Desktop environment (Similar to gedit) 820 | 821 | Note: Microsoft Office alternatives: 822 | 1. AbiWord - Microsoft Word Alternative 823 | 2. LibreOffice - Full Office Suite (Just like Microsoft Office) 824 | 3. Kate | Genie | jEdit | Sublime Text - Source Code Editors 825 | 826 | ## Delete Copy Move and Rename files 827 | 828 | **Delete | Copy | Move | Rename** 829 | 830 | - `rm` command: (remove) 831 | - `rm file` => Remove file 832 | - `rm -r dir` => Remove directory and its contents recursively 833 | - `rm -f file` => Force removal and never prompt for confirmation 834 | 835 | - `cp` command: (copy) 836 | - `cp source_file destination_file` => Copy source file to destination file 837 | - `cp source_file1 [... source_fileN] destination_dir` => Copy source file to destination directory 838 | - `cp -i` => Interactive mode 839 | - `cp -r source_directory destination_directory` => Copy source directory recursively to the destination 840 | 841 | If destination directory does NOT exist, it gets created with the contents of the source directory. 842 | 843 | - `mv` command: (Move or Rename) 844 | - `mv source [..sourceN] destination` => Moves source file(s) and/or Directories to Destination directory. 845 | - `mv -i source destination` => Interactive mode. 846 | 847 | `mv` DOES NOT require `-R` to move Source Directories into Destination Directory 848 | 849 | Ex: `mv subdir1 /subdir2/newFolder` => Moves subdir1 folder to /subdir2/newFolder folder 850 | 851 | `mv file1 file2` => Rename file1 to file2 (Overwrites file2 if it exists) [file1, file2 in same folder] 852 | `mv -i file1 file2` => Rename file1 to file2 (Asks to overwrite file2 if it exists) [file1, file2 in same folder] 853 | 854 | ## sort command 855 | 856 | **`sort`** 857 | 858 | `sort` sorts the text in a text file Alphabetically (by default) line by line. 859 | 860 | Syntax: `sort file` (THE ORIGINAL FILE IS UNAFFECTED - THIS IS ONLY FOR PRINTING TO SCREEN/STDOUT) 861 | 862 | Options: 863 | - `-kF` => Sort by key supplied. F is the field number(column number) (Ex: `sort -k2 file.txt` => Sorts lines alphabetically according to the 2nd column on each line) 864 | - `-r` => Sort in reverse order. Reverse alphabetical order by default. 865 | - `u` => Sort Unique (Removes the duplicate lines) 866 | 867 | 868 | ## Create a collection of a group of files 869 | 870 | **(bundle/archive)** 871 | 872 | Use `tar`: `tar [-] c|x|t f tarfile [pattern]` 873 | 874 | Simpler way to remember: `tar options tarFileName filesToBeArchived` 875 | 876 | `tar` does NOT need the hyphen (-) for options, but including it is optional (no harm!). Create, extract or list contents of a tar archive using pattern, if supplied. 877 | 878 | `tar` options: 879 | - `c` => Create a tar archive 880 | - `x` => Extract files from the archive 881 | - `t` => Display table of contents (List) 882 | - `v` => Be Verbose 883 | - `z` => Use compression 884 | - `f` file => Use this file 885 | 886 | Usage examples: 887 | - `tar cf tps.tar tpsreports` => create(c) an archive (tps.tar) for this file(f) called 'tpsreports' 888 | - `tar xf tps.tar` => Extract(x) this file(f) 'tps.tar' 889 | - `tar xfv taps.tar` => Be verbose(give a listing of all the extracted files) 890 | 891 | ## Compress Files 892 | 893 | `gzip` command. (Compresses supplied file) 894 | 895 | - `gzip file` => Compress files and adds extension '.gz' to it (original file AFFECTED!) 896 | - `gunzip` => Uncompress files 897 | 898 | Viewing contents of a gzipped file: 899 | - `gzcat` => Concatenates compressed files 900 | (OR) 901 | - `zcat` => Concantenates commpressed files 902 | 903 | ### Disk Usage Stats 904 | - `du` => Estimates file usage(Bytes) 905 | - `du -k` => Displays sizes in KiloBytes(KB) 906 | - `du -h` => Display sizes in human-readable format(Ex: 5M for 5 megabytes) 907 | 908 | Combining `du` & `gzip`. Examples: 909 | - `du -k data.txt` => gives how much space data.txt is using. 910 | - `gzip data.txt` => compresses data.txt to data.txt.gz 911 | - `du -k data.txt.gz` => gives how much space data.txt.gz (the compressed file) is using. 912 | - `gunzip data.txt.gz` => uncompresses data.txt.gz to data.txt (original state) 913 | 914 | ### tar and gzip 915 | 916 | **`tar`** 917 | **`gzip`** 918 | 919 | - `-z` option of tar uses gzip for compression while archiving. (Uses '.tgz' or 'tar.gz' extension) 920 | 921 | Ex: 922 | - `tar zcf tps.tgz tpsreports` => Compresses(z) and archives(c) this file(f) tpsreports into tps.tgz. 923 | - `tar ztvf tps.tgz` => Displays contents(t) of compressed(z) archive file(f) tps.tgz in a verbose(v) way. 924 | 925 | ## WildCards 926 | 927 | (Already done in other courses, just SKIPPING them here) 928 | (Learn from notes of other, previous courses) 929 | 930 | ## Input Output and Redirection 931 | 932 | There are 3 different types of input and output: 933 | - Standard Input => stdin => 0 (File Descriptor) 934 | - Standard Output => stdout => 1 (File Descriptor) 935 | - Standard Error => stderr => 2 (File Descriptor) 936 | 937 | File Descriptor number is like the number/id of the inputs or outputs. The machine uses the numbers instead of 'standard input' (human readable form) to recognize input and output 938 | 939 | ### Redirection 940 | - `>` => Redirects standard output to a file. (Overwries(truncates) existing contents) 941 | - `>>` => Redirects standard output to a file. (Appends to any existing contents) 942 | - `<` => Redirects input from a file to a command. 943 | 944 | Ex: 945 | - `echo new line > file.txt` => 'file.txt' contains the output of `echo` command ('new line'). 946 | - `ls -l > file.txt` => 'file.txt' contains the output of `ls -l` command (nothing printed on screen). 947 | - `ls -l >> file.txt` => Appends the output of `ls -l` command to 'file.txt' (nothing printed on screen). 948 | 949 | Ex: 950 | - `sort < files.txt` => sort works on input which is the content of 'file.txt' (In sort's case it is the same as 'sort file.txt') 951 | 952 | Note: Using file descriptors to work with stdin/stdout/stderr: 953 | - `&` => Used with redirection to signal that a file descriptor is being used. 954 | 955 | Ex: 956 | - `2>&1` : Redirecting standard error to standard output (Combines standard error and standard output) 957 | - `2>file` : Redirect standard error to a file. 958 | 959 | ### The Null Device 960 | If you want to 'IGNORE/DISCARD' the output, you can send it to the Null Device('/dev/null'): Ex: 961 | - `ls here not-here 2> /dev/null` [Don't want to see errors on screen nor save them to a file] 962 | 963 | Null device is also known as the 'bit bucket' 964 | 965 | Choosing whether to redirect standard output or standard error to a file. Ex: 966 | - `ls -l 2> file.txt` => Redirects std error to 'file.txt'(No space between 2 and >)[& stdout to screen] 967 | - `ls -l 1> file.txt` => Redirects std output to 'file.txt'(No space between 1 and >)[& stderr to screen] 968 | 969 | Sending standard output to one file and standard error to another(or to the same). Ex: 970 | - `ls existingFile not-here-file 1> out.txt 2> err.txt` => Std. output (for existingFile) goes to out.txt and Std error (for not-here-file) goes to err.txt 971 | 972 | Combining standard output and standard error (redirect to the same file): Ex: 973 | - `ls existingFile not-here-file > out.txt 2>&1` 974 | 975 | The above appends standard error to standard output, so both are saved into out.txt only (not screen) 976 | 977 | Ex: 978 | - `ls here not-here > /dev/null 2>&1` => Appends standard error to standard output, so both are sent to /dev/null (ignored) 979 | 980 | Important: When NO file descriptor is used in redirection, Only the standard output is redirected but the standard input is printed on the screen. Ex: 981 | - `ls -l existingFile not-here-file > lsOutput.txt` = The stdout is saved in lsOutput.txt (not printed) while stderr for 'not-here-file' was not redirected and hence, was printed to the screen. 982 | 983 | Sample Output: 984 | - `ls cannot access not-here: No such file or directory` (but ls of existingFile was saved as content of lsOutput.txt) 985 | 986 | ### Combining input and output redirection 987 | 988 | Syntax: `command < ipFileName > opFileName` 989 | 990 | The command is run with 'ipFileName' as input and the output of the command is saved to 'opFileName' 991 | 992 | Ex: `sort < file1 > file2` 993 | 994 | ## Comparing two files 995 | 996 | - `diff file1 file2` = Compare two files. 997 | - `sdiff file1 file2` = Compare two files Side-by-Side (file1 : left, file2 : right). 998 | - `vimdiff file1 file2` = Highlight differences in vim editor. 999 | 1000 | ### diff Example 1001 | 1002 | **`diff`** 1003 | 1004 | - `diff file1 file2` 1005 | 1006 | Output: 1007 | ``` 1008 | 3c3 1009 | < this is a line in a file 1010 | --- 1011 | > this is a line in a file 1012 | ``` 1013 | 1014 | Here, 3c3 is following the pattern => ``. `` can be Add(a), changes(c) or Delete(d) indicating the kind of difference. 1015 | - `<` => @beginning of a line indicates it is a line from file1 1016 | - `>` => @beginning of a line indicates it is a line from file2 1017 | - `---` => It is just a separator 1018 | 1019 | ### sdiff Example 1020 | 1021 | **`sdiff`** 1022 | 1023 | `sdiff file1 file2` 1024 | Output: 1025 | ``` 1026 | line in file 1 | line in file 2 1027 | > line in a file 2 1028 | ``` 1029 | - `|` => Indicates differing lines (side-by-side lines separated by '|') 1030 | - `<` => @beginning of a line indicates it is a line from file1 (line only exists in file1) 1031 | - `>` => @beginning of a line indicates it is a line from file2 (line only exists in file1) 1032 | 1033 | ### vimdiff Example 1034 | 1035 | **`vimdiff`** 1036 | 1037 | `vimdiff file1 file2` (Both files will be opened in separate windows!) 1038 | 1039 | - ` w` : Go to the next window 1040 | - `:q` : Quit (Close current window) 1041 | - `:qa` : Quit All (Close both files) 1042 | - `:qa!` : Force Quit All (Force close both files!) - changes that you don't want to save. 1043 | 1044 | ## Searching in files and using pipes 1045 | 1046 | Use the `grep` command to search inside files. `grep` displays Lines of a file matching a pattern. 1047 | (If we DON'T supply a file name grep uses the STANDARD INPUT to search against.) 1048 | 1049 | Syntax: `grep pattern file` 1050 | 1051 | Options: 1052 | - `-i` => Perform a search, ignoring case 1053 | - `-c` => Count the number of occurrences of the pattern in a file 1054 | - `-n` => Precede output with Line Numbers 1055 | - `-v` => Invert Match. Print lines that do NOT match. 1056 | 1057 | Ex: 1058 | - `grep o secret.txt` => searches for 'o' in 'secret.txt' and prints the matching lines 1059 | (lines from the file that contain 'o') 1060 | - `grep -v user secret.txt` => Matches all lines that do NOT contain 'user' in 'secret.txt' file. 1061 | - `grep -i User secret.txt` => Matches all lines that contain 'user' in 'secret.txt' file.(IGNORES CASE) 1062 | - `grep -n blah secret.txt` => Matches all lines that contain 'blah' in 'secret.txt' file.(PRINTS LINE NUM) 1063 | 1064 | ### Finding out the type of a file 1065 | 1066 | The `file` command is used. 1067 | 1068 | Syntax: `file file_name` => Displays the file type. 1069 | 1070 | Ex: 1071 | - `file sales.data` (Ex. O/P: 'sales.data: ASCII text') 1072 | - `file jason.tar` (Ex. O/P: 'jason.tar: POSIX tar archive') 1073 | - `file collection` (Ex: O/P: 'collection: directory') 1074 | 1075 | ### Searching for strings in a binary file 1076 | 1077 | To display printable strings contained in a binary file, use the `string` command. 1078 | 1079 | Syntax: `string binaryFileName` 1080 | 1081 | ### Pipes or Pipelining 1082 | 1083 | The pipe symbol is '|'. It's used to chain commands together. 1084 | 1085 | Visualization: 'command-output | command-input' 1086 | 1087 | (The pipe takes the std. output of one command(left) and feeds it as std. input to other command(right)) 1088 | 1089 | Only the standard output is sent as standard input to the next command. (use 2>&1 to send standard error as well - check redirection topic) 1090 | 1091 | Common usage examples: 1092 | - `grep pattern file` <=> `cat file | grep pattern` (Equivalent) 1093 | - `ls -l | cat | grep -i john` (We can chain as many commands as we want) 1094 | 1095 | ### `cut` command 1096 | 1097 | **`cut`** 1098 | 1099 | - `cut [file]` => Cuts out selected portions of the file. (If file is omitted, uses STANDARD INPUT). (Cut does NOT affect the original file.) 1100 | 1101 | - `cut -d` => Use delimiter as the field(column) separator 1102 | - `cut -fN` => Display the Nth field. 1103 | 1104 | Ex: 1105 | - `cut -d' ' -f2 file1.txt` => Selects column 2 from file1.txt using space as delimiter between fields 1106 | - `grep bob /etc/passwd | cut -d: -f1,5` => cuts 1 and 5 ':' separated columns of /etc/passwd. 1107 | 1108 | `/etc/passwd` contains user data such as name, home folder, etc. 1109 | 1110 | ### Translating Characters 1111 | 1112 | **`tr`** 1113 | 1114 | `tr` is used to translate all occurrences of a value/string in a file to another value/string. If NO file is supplied, it takes the STANDARD INPUT. Original file not affected. 1115 | 1116 | Syntax: `tr "" "" file` 1117 | 1118 | Ex: 1119 | - `tr ":" " " file.txt` => Translate all the ':' with spaces(' ') in 'file.txt'. 1120 | 1121 | ### Formatting output into columns 1122 | 1123 | **`column`** 1124 | 1125 | Syntax: `column [options] [file...]` 1126 | 1127 | Ex: 1128 | - `column -t` : Determine the number of columns the input contains and create a table. (**Space** is the delimiter between columns by default) 1129 | 1130 | ### more and less commands 1131 | 1132 | **(Printing out to a pager)** 1133 | 1134 | Already learnt. (Refer earlier or previous notes) Keep in mind that these two commands can also take take redirected inputs as well (STANDARD INPUT). 1135 | Ex: 1136 | - `cat /etc/passwd | less` 1137 | 1138 | (THERE ARE MANY SMALL COMMANDS THAT DO ONE THING VERY WELL. WE CAN CHAIN MANY OF THESE COMMANDS TOGETHER TO EXECUTE SOMETHING COMPLEX AND POWERFUL) 1139 | 1140 | 1141 | ## Copying files over the network 1142 | 1143 | To copy files between 'remote server and local host' (or) 'between two remote servers'. 1144 | 1145 | - SCP - SECURE COPY. 1146 | - SFTP - SSH(or SECURE) FILE TRANSFER PROTOCOL. 1147 | 1148 | Both SCP and SFTP are extensions of the 'SSH' (Secure Shell) Protocol. 1149 | - (In SCP, we need to know what files are to be transferred while writing the command/connecting.) 1150 | - (In SFTP, we need NOT know before connecting, what files are going to be transferred.) 1151 | 1152 | Using SCP/SFTP: 1153 | - Mac & Linux come with scp and sftp command line utilities (openSSH in the case of MAC) 1154 | - For Windows systems, we need to install a tool called 'putty' ['pscp.exe' and 'psftp.exe']. 1155 | 1156 | Graphical SCP/SFTP clients: 1157 | 1. Cyberduck, (Mac and Windows) 1158 | 2. FileZilla, (Mac, Linux and Windows) 1159 | 3. WinSCP. (Only Windows) 1160 | 1161 | ### SCP 1162 | - `scp source destination` => Copy source to destination (Destination is like - 'serverName:directoryPath') 1163 | 1164 | (Full Syntax: `scp source_file_name username@destination_host:destination_folder`) 1165 | 1166 | Ex: 1167 | - `scp sourceFileName host:destinationPath`, 1168 | - `scp z.txt linuxsvr:/tmp/`, 1169 | - `scp z.txt adminuser@linuxsvr:~/` => Transfer files as a different user(adminuser) [password required] 1170 | 1171 | (We can use SSH or SFTP to check if the copied local files exist on the remote server now.) 1172 | 1173 | SCP Options: 1174 | - `-v` => We can use the `-v` parameter to print debug information into the screen. 1175 | - `-p` => An estimated time and the connection speed will appear on the screen. 1176 | - `-r` => Copy directories and their contents recursively. 1177 | - `-C` => The `-C` parameter will compress your files on the go, making the transfer faster. (No further compression if file is already compressed. Ex: .zip, .rar, .iso, ... etc) 1178 | - `-p` => Specify the Specific port to use. Ex: `scp -P 2249 Label.pdf mrarianto@202.x.x.x:.` (we are using port 2249) 1179 | 1180 | By default SCP using `AES-128` to encrypt files. If you want to change to another cipher to encrypt it, you can use `-c` parameter. Take a look of this command. Ex: 1181 | - `scp -c 3des Label.pdf mrarianto@202.x.x.x:.` 1182 | 1183 | The above command uses 3des algorithm to encrypt the file. 1184 | 1185 | Limiting Bandwidth: 1186 | - `-l` => limit the bandwidth to use. (It will be useful if you do an automation script to copy a lot of file, but you don’t want the bandwidth is drained by the SCP process.) Ex: 1187 | - `scp -l 400 Label.pdf mrarianto@202.x.x.x:.` 1188 | 1189 | The 400 value behind “-l” parameter is mean that we limit the bandwidth for SCP process only 50 KB/sec. 1190 | One thing to remember that bandwidth is specified in Kilobits/sec (kbps). It is mean that 8 bits equal with 1 byte. BUT, While SCP counts in KiloByte/sec (KB/s). So if you want to limit your bandwidth for SCP maximum only 50 KB/s, you need to set it into 50 x 8 = 400. 1191 | 1192 | ### SFTP: 1193 | 1194 | `sftp user@host` => Start a secure file transfer session with host (host can be an IP address as well) 1195 | Ex: 1196 | - `sftp jason@host` (or) 1197 | - `sftp tecmint@27.48.137.6` ... etc. 1198 | 1199 | (NOTE: you maybe prompted for a password.) 1200 | 1201 | SFTP Note/Points: 1202 | - Once you successfully connect, you are at the command prompt of the remote server. 1203 | - For example, `pwd` returns CWD on the server, `ls` returns `ls` of CWD on the server. 1204 | - To use the commands for your local host computer while connected, precede commands with an 'l',(stands for 'local'). 1205 | - So, to view the CWD on your local system, type `lpwd`, to list the files in the CWD of your local system, type 'lls', .. and so on. 1206 | 1207 | Therefore: SFTP commands: 1208 | 1. `pwd` => Remote's Working Directory. 1209 | 2. `lpwd` => Local System's Working Directory. 1210 | 3. `ls` => List files on Remote. 1211 | 4. `lls` => List files on the Local System. 1212 | 5. `put localFile` => Puts a local system file onto the remote systems CWD. 1213 | 6. `mput localFile1 [...localFileN]` => Put multiple Local System files onto the REMOTE. 1214 | 7. `get remoteFile [localFileName]` => Get Remote file onto the Local system . 1215 | 8. `mget remoteFile1 [...remoteFileN] [localFileName]` => Get multiple Remote files onto the Local system. 1216 | 1217 | [NOTE: To transfer directories in `put`, `mput`, `get` or `mget`, user `-r` option (recursive)] 1218 | 1219 | 9. `?` (or) `help` => SFTP help command screen shows the commands we can use to accomplish various tasks. 1220 | 10. `cd` => Changes directory on the Remote Server. 1221 | 11. `lcd` => Changes directory on the Local System. 1222 | 12. `mkdir` => Make a directory on the Remote Server. 1223 | 13. `lmkdir` => Make a directory on the Local System. 1224 | 14. `rm` => Remove files and Directories on the Remote Server. 1225 | 15. `rmdir` => Remove empty Directories from the Remote Server. 1226 | 16. `exit` or `bye` => Close/Terminate the SFTP the session. 1227 | 17. `chown`, `chgrp`, `chmod` => All on the Remote Server.(NO command from SFTP to change local permissions!) 1228 | 18. `lumask` => This is the only permission related command in SFTP for the Local System. 1229 | 1230 | (Other utilities): 1231 | - The `ftp` command. (Don't use if possible, use SFTP or SCP.) 1232 | - `ftp host` => Start a file transfer session with host. (Not secured!) 1233 | 1234 | Using `ftp` means that your Login credentials are sent in plain text over the network. The files that you download/upload are NOT encrypted either. 1235 | 1236 | ## Customizing the Shell Prompt 1237 | 1238 | An Environment Variable holds the shell prompt. 1239 | - `$PS1` => for bash, ksh and sh. 1240 | - `$prompt` => Csh, tcsh, and zsh. 1241 | 1242 | (Check `man bash` pages for complete info.) 1243 | 1244 | **Bash Prompt:** 1245 | 1246 | Format Strings: (That can be placed within the prompt environment variable): 1247 | - `\d` => Date in 'Weekday Month Day' format (Ex: Tue May 26) 1248 | - `\h` => Hostname (upto the first period (.)) 1249 | - `\H` => Hostname 1250 | - `\n` => Newline 1251 | - `\t` => Current time in 24 hrs format (HH:MM:SS) 1252 | - `\T` => Current time in 12 hrs format (HH:MM:SS) 1253 | - `\@` (or) `\&` => Current time in 12 hrs am/pm format 1254 | - `\A` => Current time in 24 hrs HH:MM format 1255 | - `\u` => Username of the current user 1256 | - `\w` => Current Working Directory 1257 | - `\W` => Basename of the Current Working Directory 1258 | - `\$` => If the effective UID is 0, a '#', otherwise a '$'[Superuser(Eff.UID=0) gets '#', everyone else: '$'] 1259 | 1260 | Persist the PS1 changes(for subsequent sessions): 1261 | - PS1 changes created on the prompt are gone after we quit the session.(Not available for the next session). 1262 | - So, We must add the PS1 changes as a line to the '~/.bash_profile' file. 1263 | 1264 | We can do this either 1265 | 1. Manually: Insert something like `export PS1="[\u@\h \w]\$"` into '~/.bash_profile' file. 1266 | (OR) 1267 | 2. Append to the file: Like => `echo 'export PS1="[\u@\h \w]\$"' >> ~/.bash_profile`. 1268 | 1269 | (NOTE: personal initialization files, like .bash_profile, are also known as 'Dot Files' since they begin with a '.') 1270 | 1271 | ## Shell aliases 1272 | 1273 | Used for shortening long commands. 1274 | - `alias name=value` => Create a new alias(name) for a command/sequence of commands(;, &, || separated)(value) 1275 | - `alias` => List all of the current aliases that are set. 1276 | 1277 | - Aliases can also be used for adjusting commmon typing errors (ex: 'grpe' alias for 'grep') 1278 | - Aliases can also be used to make Linux behave like another OS (ex: 'cls' alias for 'clear') 1279 | 1280 | Removing Aliases: 1281 | - `unalias name` => removes the alias with name 'name' 1282 | - `unalias -a` => Removes all the aliases 1283 | 1284 | Persist the Aliases(for subsequent sessions): 1285 | - Aliases create on the prompt are gone after we quit the session.(Not available for the next session). 1286 | - So, We need to add the alias command as a line to the '~/.bash_profile' file (just like for shell prompt). 1287 | 1288 | This can be done: 1289 | 1. Manually: Insert something like `alias cls="clear"` into '~/.bash_profile' file. 1290 | (OR) 1291 | 2. Append to file: `echo 'alias cls="clear"' >> ~/.bash_profile`. 1292 | 1293 | (NOTE: Keep your alias usage to a minimum. Because, working on a different system where your aliases don't work might cripple you/slow you down/Need to copy your configuration file to each system you work on) 1294 | 1295 | ## Environment variables 1296 | 1297 | These are 'name=value' pairs. Usually, Environment variables are in UPPERCASE(convention). Ex: `EDITOR=nano` 1298 | 1299 | (Use the `man bash` pages for more info on environment variables) 1300 | 1301 | ### Viewing all the environment variables 1302 | 1303 | (And their values on the command line) 1304 | 1305 | - `printenv` => Prints all the Environment variables to the screen. 1306 | - `printenv ENV_VAR` => Prints the value of the specified Environment Variable. (Case-Sensitive!) 1307 | - `echo $ENV_VAR` => Prints the value of specified Environment Variable. (prepend name with a $). 1308 | 1309 | ### Creating or modifying environment variables 1310 | 1311 | Syntax: `export VAR="value"` 1312 | 1313 | Ex: 1314 | - `export EDITOR="vi"` => Creates EDITOR environment variables to 'vi'.(or modifies value to it, if EV exists) 1315 | - `export TZ="US/Pacific"` => Sets the Default Time Zone to the US Pacific time. 1316 | (Date command would return a different date/time depending on the TZ environment variable value) 1317 | 1318 | ### Removing environment variables 1319 | 1320 | Syntax: `unset VAR` 1321 | 1322 | Ex: 1323 | - `unset TZ` => removes the Time Zone Environment Variable. 1324 | 1325 | ### Persisting the environment variables settings 1326 | 1327 | The act of setting/unsetting the Environment Variables on the command line is NOT persistent. (That is, the changes made to them won't be available for the subsequent sessions). So, we must save the environment variables into the '~/.bash_profile' file. 1328 | 1329 | This is done either: 1330 | 1. Manually: Insert something like `export TZ="US/Central"` into '~/.bash_profile' file. 1331 | (OR) 1332 | 2. Append to file: `echo 'export TZ="US/Central"' >> ~/.bash_profile`. 1333 | 1334 | NOTE: Changing time-zone Environment Variable(TZ) to Indian Standard time: `export TZ="Asia/Calcutta"` => Now \t, \@, etc in the chell prompt will show IST. (Even the `date` command will show the IST only.) 1335 | 1336 | **Important** 1337 | NOTE: 1338 | - Whenever the output of a command is too much/too long, we can pipe the output of that command to a pager utility like 'less' or 'more': Ex: 1339 | - `cat bigBigFile.txt | less` 1340 | 1341 | - Refreshing the terminal to include the changes made to '~/.bash_profile': To see the changes take effect, run: 1342 | - `source ~/.bash_profile` (or) `. ~/.bash_profile` and the terminal is refreshed with the new changes. 1343 | 1344 | (This is a handy command that can be used instead of exiting and restarting the terminal) 1345 | 1346 | - To view users on the system, run: 1347 | - `who` 1348 | 1349 | - To view which user you are on the system(current user), run: 1350 | - `whoami` 1351 | 1352 | ## Processes and job control 1353 | 1354 | Creating and viewing processs, background vs foreground processes, killing a process, etc. 1355 | 1356 | ### Displaying process information 1357 | 1358 | - `ps` => Display Process Status.(No arguments? Displays ps for all processes associated with current session) 1359 | 1360 | #### Options for ps 1361 | 1362 | **`ps`** 1363 | 1364 | - `-e` => Everything, all processes(NOT just limited to your session). 1365 | - `-f` => Full format listing. 1366 | - `-u username` => Display username's processes only. (Ex: `ps -fu joehenderson`) 1367 | - `-p pid` => Display information for process with PID 'pid'. 1368 | 1369 | The full listing `-f` contains: 1370 | 1. UID (User ID), 1371 | 2. PID (Process ID), 1372 | 3. PPID (Parent Process ID), 1373 | 4. Time , 5. Process/Command Name ... etc 1374 | 1375 | 1376 | One of the main reasons for running `ps` is to get the Process ID (PID) 1377 | 1378 | NOTE:: PID != JOB NUMBER 1379 | 1380 | Common `ps` commands: 1381 | - `ps -e` => Display all processes. 1382 | - `ps -ef` => Display all processes, full listing. 1383 | - `ps -eH` => Display a process tree. (IMPORTANT) 1384 | - `ps -e --forest` => Display a process tree. (IMPORTANT) 1385 | - `ps -u username` => Display user's processes. 1386 | 1387 | Other common commands: 1388 | - `pstree` => Display processes in tree format. 1389 | - `top` => Interactive process viewer. (Press 'q' to exit, '?' for help) 1390 | - `htop` => Interactive process viewer. (Less popular, may not be available by default on the system) 1391 | 1392 | The `top` command places the processes using most of the CPU and Memory resources at the TOP of the list. 1393 | It also displays the CPU and Memory usage columns. 1394 | 1395 | #### Killing a currently running foreground process 1396 | 1397 | - Press `` on the CLI while the process is running. (Pressing this kills the foreground process and return the shell prompt to the user) 1398 | 1399 | #### Suspend a foreground process 1400 | 1401 | - Press `` to suspend a foreground process. 1402 | 1403 | Note: A process that is suspended is NOT running in the background! It is actually stopped. A (stopped) process can be then run in the background : type `fg %jobnumber` 1404 | 1405 | #### Background processes 1406 | 1407 | It maybe convenient to keep a long running process in the background. Background processes do NOT block the execution of other processes (esp. on the CLI). 1408 | 1409 | ##### Starting a background process 1410 | 1411 | - ` &` => Start the command in the background. (It Displays two numbers as output => 'Job No.' in brackets[] and 'PID' (Ex: [1] 2373) ) Ex: 1412 | - `./some-long-running-script &` => Starts the script in the background (Ex. O/P: [1] [4232]) (We can view the status of the process by running `ps -p 4232`) 1413 | 1414 | ### Listing jobs 1415 | 1416 | **(Helps list all the currently active jobs => Usually lists the Background Processes)** 1417 | 1418 | We can list all the currently running jobs in the system. 1419 | - `jobs [%num]` => List jobs. (No arguments? List all the active jobs) 1420 | - `jobs -l [%num]` => Gives a long listing of the jobs. 1421 | 1422 | (The '+' sign in the jobs output represents the current job. The '-' sign in the jobs output represents the previous job.) 1423 | 1424 | - `jobs %+` (or) `jobs %%`=> Refers/lists the current job. 1425 | - `jobs %-` => Refers/lists the previous job. 1426 | 1427 | ### Forcing processes into the background 1428 | 1429 | - `bg` => Send a suspended process(current job) to the background. (current job - refers to last job that was stopped in the FG or the last job that was started in the BG) 1430 | - `bg %num` => You can background a process with a specific job number 'num' by preceding it with a '%' sign. Ex: 1431 | - `bg %1` => Forces a suspened process in Foreground to the Background (runs it in the background). 1432 | 1433 | Forcing processes into Foreground: 1434 | 1435 | - `fg`= Send a background process(current job) to the foreground. (Current job - refers to last job that was stopped in the FG or the last job that was started in the BG) 1436 | - `fg %num` (or) `%num` => Foreground a BG process with a specific jobno. 'num' by preceding it with '%' sign. Ex: 1437 | - `fg %1` (or) `%1` => Forces a Background process into the Foreground. 1438 | 1439 | ## Killing Processes 1440 | 1441 | **(Job-wise/Process-wise)** 1442 | 1443 | ### Killing any process using PID 1444 | 1445 | - `` => Kills the foreground process. 1446 | - `kill pid` => Kill a process with Process ID 'pid'. (Default signal used by kill = TERM (termination) | 15) 1447 | - `kill -sig pid` => Send a signal 'sig' to a process (to kill a process). 1448 | 1449 | NOTE: `kill -l` => Displays a list of signals. Ex: 1450 | - `kill 123` => kill process with ID 123. (Default signal used by kill = TERM (termination) | 15) 1451 | 1452 | ### Killing any process using job number: 1453 | 1454 | -`kill %jobnumber` => Kills a process with job number 'jobnumber'. Ex: 1455 | - `kill %1` => Kills process with job number 1. 1456 | 1457 | ### Signals have numbers associated with them 1458 | 1459 | - `kill -l` => Displays a list of signals that can be sent to a process and the numbers associated with them. 1460 | 1461 | For Example: 1462 | - TERM <=> 15 (Terminate signal) 1463 | - SIGKILL <=> 9 (KILL signal) .. etc. 1464 | 1465 | Ex: 1466 | - `kill -15 123` => Same as kill process with ID 123 since default signal is -TERM (or -15) 1467 | - `kill -TERM 123` => Same as kill process with ID 123 since default signal is -TERM (or -15) 1468 | 1469 | NOTE:: 1470 | `kill -9 123` => If a process does NOT terminate with default signal '15' - then USE kill signal '9'. 1471 | 1472 | ### Summary 1473 | 1474 | - To display information about all running processes = Use `ps` command 1475 | - To kill a process in the Foreground = Type `` 1476 | - To suspend a process in the Foreground = Type `` 1477 | - To background a suspended process = Use `bg` command 1478 | - To foreground a process = Use `fg` command 1479 | - To display information about your running jobs = Use `jobs` command 1480 | - To kill jobs using the job number/Kill processes using the PID = Use `kill` command 1481 | 1482 | ## Scheduling repeated jobs with cron 1483 | 1484 | **`cron`** 1485 | 1486 | We can use cron to SCHEDULE and AUTOMATE tasks. The cron service STARTS when the SYSTEM BOOTS and checks for SCHEDULED JOBS to be RUN EVERY MINUTE. 1487 | 1488 | - `cron` => A time based job-scheduling service. 1489 | - `crontab` => A program to create, read, update and delete your job schedules 1490 | 1491 | `crontab` format: (config file that contains information about scheduled jobs) 1492 | 1493 | Each Line in a cron table represents a 'job' and contains 2 parameters: 1494 | 1. When to run 1495 | 2. What to run. 1496 | 1497 | Format: 1498 | ``` 1499 | * * * * * command 1500 | | | | | | 1501 | | | | | + -- Day of the week (0-6) - Starting with 'sunday'(0) 1502 | | | | + ---- Month of the year (1-12) 1503 | | | + ------ Day of the month (1-31) 1504 | | + -------- Hour (0-23) 1505 | + ---------- Minute (0-59) 1506 | ``` 1507 | 1508 | Ex: `0 7 * * 1 /opt/sales/bin/weekly-report` => This script runs every Monday 07 Hrs (or 7:00 AM). 1509 | 1510 | Note:: Asterisk (*) => Matches any/every time (or date). 1511 | 1512 | ### Redirecting the output of the cron jobs 1513 | 1514 | Ex: `0 2 * * * /root/backupdb/ > /tmp/db.log 2>&1` => Backs up database at 2:00 (AM) every day. 1515 | (Here > is used to redirect the std. output to the /tmp/db.log file along with std. error (2>&1)) 1516 | 1517 | ### Using multiple values 1518 | 1519 | - Use commas(,) to represent multiple values (or) 1520 | - Use divider(/) to divide the total time frame for that column. (or) 1521 | - Use ranges(-) to specify a range for time / date. 1522 | 1523 | Ex: Run every 30 minutes: 1524 | - `0,30 * * * * /opt/acme/bin/half-hour-check` 1525 | 1526 | Another way to do the same thing: (Dividing the total minutes(60) by 2 => half hour) 1527 | - `*/2 * * * * /opt/acme/bin/half-hour-check` 1528 | 1529 | Run for the first 5 minutes of the hour: 1530 | - `0-4 * * * * /opt/acme/bin/first-five-mins` 1531 | 1532 | Using keywords/Shortcuts in your crontabs: 1533 | - `@yearly` => `0 0 1 1 *` (Run once a year on the 1st day of the 1st month) (day can be any weekday) 1534 | - `@annually` => `0 0 1 1 *` (Run once a year on the 1st day of the 1st month) (day can e any weekday) 1535 | - `@monthly` => `0 0 1 * *` (Run once every month on the 1st day of the month) (day can be any weekday) 1536 | - `@weekly` => `0 0 * * 0` (Run once every sunday on every month) (sunday can be any day) 1537 | - `@daily` => `0 0 * * *` (Run once every day at 00:00 (12AM), every month, any weekday) 1538 | - `@midnight` => `0 0 * * *` (Run once every day at 00:00 (12AM), every month, any weekday) (12AM = midnight) 1539 | - `@hourly` => `0 * * * *` (Run once every hour at start of hour (0th minute), every day, month, any wkday) 1540 | 1541 | Not all of these shortcuts might work on your linux distribution. (Use `man cron` to check) 1542 | 1543 | ### crontab command 1544 | 1545 | **`crontab`** 1546 | 1547 | - `crontab file` => INSTALL a new crontab from the contents of the file specified. 1548 | - `crontab -l` => LIST your cron jobs. (No cron jobs? -> Ex. o/p = 'no crontab for adminuser') 1549 | - `crontab -e` => EDIT your cron jobs. (Invokes the editor specified in the '$EDITOR' environment variable) 1550 | - `crontab -r` => REMOVE all of your cron jobs. 1551 | 1552 | Ex: 1553 | - `vi my-cron-file` => Make a cron file. 1554 | 1555 | (Ex. contents: `0 7 * * 1 /opt/bin/weekly` -> runs the weekly file every monday) 1556 | 1557 | - `crontab my-cron-file` => Installs the 'my-cron-file' as a cron/scheduled job in th crontab. 1558 | - `crontab -l` => Lists our cron jobs (Ex. o/p in this case: '0 7 * * 1 /opt/bin/weekly') 1559 | 1560 | Once a cron job has been added to the crontab it is run at scheduled times and specified file. 1561 | 1562 | - `crontab -r` => Deletes/Removes all the cron jobs(In this case, 'my-cron-file') 1563 | 1564 | Therefore, cron service runs scheduled jobs and these jobs can be manipulated using the `crontab` command. 1565 | 1566 | ## Switching users and running commands as others 1567 | 1568 | **`su`** 1569 | 1570 | Use `su` to switch users: 1571 | 1572 | - `su` (or) `su root` => Become Superuser (The admin is usually the superuser of the system) 1573 | - `su username` => Switch to account having user name as 'username'. 1574 | 1575 | Ex: 1576 | - `su` : switches to the root user (superuser) 1577 | - `su oracle` : Switches to the user named oracle 1578 | 1579 | ### Options for su 1580 | 1581 | **`su`** 1582 | 1583 | 1. `-` => A hyphen is used to provide an environment similar to what the user would expect had he/she logged in directly. 1584 | 1585 | For example, we end up in the HOME directory of the switched-in user. We can check the environment variables set for the currently-switched-to user using '-'. Otherwise, we can ONLY see environment variables associated with the previous user's account 1586 | 1587 | ``` 1588 | export TEST=1 1589 | su oracle 1590 | echo $TEST # Returns 1 even if TEST was the environment variable of the previous user's session. 1591 | ``` 1592 | ``` 1593 | export TEST=1 1594 | su - oracle # (Using '-' to set the environment similar to direct login to terminal of the switched user) 1595 | echo $TEST # Returns nothing (Because $TEST was not set in this (switched) user's session) 1596 | ``` 1597 | 1598 | 2. `-c ` => Specify a command to be executed. 1599 | 1600 | If command is more than one word in length, surround it with quotes('' or ""). Ex: `su -c "ls -l"`. Ex: 1601 | - `su -c COMMAND anotherusername` : Runs COMMAND as anotherusername [DOES NOT SWITCH USER] 1602 | - `su -c COMMAND - anotherusername` : Runs COMMAND as anotherusername (& has access to his/her environment variables). DOES NOT SWITCH USER. 1603 | 1604 | NOTE:: Alternate way to execute commands as another user account is to use the `sudo` command (later) 1605 | 1606 | ### User identification commands 1607 | 1608 | - `whoami` => To know the effective username. To know what user you are logged in as. Ex: 1609 | - `whoami` => 'jason' 1610 | - `su oracle` 1611 | - `whoami` => 'oracle' 1612 | 1613 | - `who` => Lists all the users currently logged onto the system. 1614 | 1615 | 1616 | ### The sudo command 1617 | 1618 | **`sudo`** 1619 | 1620 | **Execute commands as another user - Important!** 1621 | 1622 | - `sudo` stands for 'Super User do' and typically used to execute commands as another user, usually SUPERUSER. (That is, it allows us to execute commands with the security privileges of another user) 1623 | 1624 | It is commonly used to install, start and stop applications that require the 'root' user privileges. 1625 | 1626 | Note: On running `sudo`, you might be prompted for a password(once in a session), and you have to give the USER'S PASSWORD (& NOT the `root` password) - And if the user has root permissions, the sudo command is executed. 1627 | 1628 | #### Executing commands with sudo 1629 | 1630 | - `sudo -l` => List the available commands 1631 | - `sudo ` => Run command a root(as the superuser) 1632 | - `sudo -u root ` => Same as running command as root(as superuser) 1633 | - `sudo -u user ` => Run the command as user 'user' (`-u` is used to specify the user.) 1634 | 1635 | #### Switching users by using sudo su 1636 | 1637 | - `sudo su` => Switch to the superuser(root) account. 1638 | - `sudo su -` => Switch to the superuser(root) account with root's environment. 1639 | - `sudo su - username` => Switch to username's account with username's environment. 1640 | 1641 | Alternatively: 1642 | - `sudo -s` => Starts a shell with root(superuser) account. 1643 | (or) 1644 | - `sudo -u root -s` => Starts a shell with root(superuser) account. 1645 | - `sudo -u user -s` => Starts a shell as 'user' (from the specified account). 1646 | 1647 | Examples: 1648 | - `sudo /etc/init.d/oracle start` => Starts an application called 'oracle' as the root/superuser. 1649 | - `sudo -u bob /opt/bobapp/bin/start` => Starts bobapp as the user 'bob'. 1650 | - `sudo -s` => Uses `sudo` to switch to the root/superuser account. 1651 | 1652 | ### Modifying the sudo configuration 1653 | 1654 | - `visudo` => Edit the '/etc/sudoers' file. (We need root access to execute `visudo`) 1655 | 1656 | Therefore, switch to root and run `visudo` (OR) run `sudo visudo` from current account 1657 | 1658 | The visudo file format: There are many lines of code in the visudo file but one common type of line encountered is - Syntax: 1659 | - `user host=(users)[NOPASSWD:]commands` 1660 | - user : username of an account, 1661 | - host : system name/ host name, 1662 | - NOPASSWD:: 'Optional' parameter suggesting that this user does NOT need a passwrod to run these commands., 1663 | - commands : Contains all the commands the user can run(Multiple commands are comma(,) separated) 1664 | 1665 | Ex: 1666 | - `adminuser ALL=(ALL)NOPASSWD:ALL`, 1667 | - `jason linuxsvr=(root) /etc/init.d/oracle` 1668 | 1669 | Note: If you ever forget to run a command with `sudo`, you can run the command again with: 1670 | - `sudo !!` => Basically, it runs the previous command 1671 | 1672 | `!!` refers to the last command in the command history and runs that command with superuser/root permission. 1673 | 1674 | Running a previous command with `sudo` provided the command starts with a particular string: 1675 | - `sudo !u` => Runs the most recently executed command in history that starts with 'u' from the root/superuser account. 1676 | 1677 | 1678 | 1679 | Summary: 1680 | - To switch users => Use `su` command. 1681 | - To execute commands with superuser control => Use `sudo` command. 1682 | 1683 | ## Shell history 1684 | 1685 | All executed commands are added to the shell history, which can be displayed and recalled. Shell history is stored in Memory and on Disk. Commands can be stored in one these files (depends on shell): 1686 | - `~/.bash_history`, (BASH SHELL) 1687 | - `~/.history`, 1688 | - `~/.histfile` 1689 | 1690 | ### Viewing history 1691 | 1692 | **`history`** 1693 | 1694 | - `history` => Displays the shell history (each line contains a command along with a serial/command number). 1695 | 1696 | ### Setting the size of the history 1697 | 1698 | **(Number of commands saved)** 1699 | 1700 | The HISTSIZE environment variable controls the number of commands in the history.(500 by default) 1701 | - `export HISTSIZE=1000` => Sets the history size to 1000. (Can place this line in '~/.bash_profile' to persist changes) 1702 | 1703 | ### Repeating commands from history 1704 | 1705 | - `!N` => Repeat command on line number N (in `history`) 1706 | - `!!` => Repeat the previous command. 1707 | - `!string` => Repeat the MOST RECENT comman STARTING with 'string'. (Ex: `!gre` => maybe executes 'grep') 1708 | 1709 | ### Reuse or pull out the arguments from the previous command 1710 | 1711 | - `!:N` => Pulls out Nth argument from the previous command. `:N` - Represents a word on the command line . 0 - command, 1 - first argument, ... etc. Ex: 1712 | - `head file1 file2 file3` 1713 | - `vi !:2` => Pulls out argument 2 from previous command (file2) and opens it in the `vi` Editor 1714 | 1715 | Another example: 1716 | - `echo !:2 !ch:2` => Pulls out 2nd argument to previous command and 2d argument to the most recently used command that started with `ch` (Maybe chown, chgrp, chmod, .. etc). 1717 | 1718 | Shortcuts: 1719 | - `!^` => pulls out FIRST argument to the previous command. (`!^` <=> `!:1`) 1720 | - `!$` => pulls out LAST argument to the previous command. (`!^` <=> `!:N` where N is the Nth among N args) 1721 | - `!*` => Reuse ALL the arguments to the previous command in the current command. (Ex: `grep !*`) 1722 | 1723 | Ex: 1724 | - `head file1 file2 file2` 1725 | - `vi !$` => pulls out file3 and opens it in `vi` Editor. 1726 | 1727 | 1728 | ### Searching for commands 1729 | 1730 | - `` => Reverse search the shell history. (Matches typed pattern with commands in history) 1731 | - `` - Execute the command 1732 | - `` - Change the command 1733 | - `` - Cancel the search 1734 | 1735 | ### Autocompletion 1736 | 1737 | Use the `` key to autocomplete: 1738 | 1. File and Directory paths, Other paths 1739 | 2. Environment Variables 1740 | 3. Usernames(~) [Ex: `~ja` => `~jason`] 1741 | 4. Commands 1742 | 1743 | ## Installing & managing software 1744 | 1745 | Typically, when we want to install a software we do so with a 'Package'. Package is a 'Collection of files'. It contains: 1746 | 1. Data, and 1747 | 2. Metadata: Package Description, Version, & Dependencies. 1748 | 1749 | ### Package manager 1750 | - It is used to - Install, Upgrade or Remove packages. 1751 | - It manages dependencies. (Automatically installs any required dependencies) 1752 | - Keeps track of what is installed. (What files belong to what packages, versions, etc) 1753 | 1754 | 1755 | ### The RPM format 1756 | 1757 | **`RPM`** 1758 | 1759 | The Red Hat Package Manager 1760 | 1761 | For installing Software on RPM Distros: RedHat, CentOS, Fedora, Oracle Linux, Scientific Linux. The `yum` command is a package manager utility for the distros supporting RPM format: 1762 | - `yum search string` => Search for packages (online, included in the pkg mgr) matching the 'string'. 1763 | - `yum info [package]` => Display information. 1764 | - `yum install [-y] package` => Install Package. 1765 | - `yum remove package` => Remove Package. 1766 | 1767 | **Installing or removing software requires Superuser or root privileges** 1768 | 1769 | RPM commands: (An **alternative** command to the `yum` utility) 1770 | - `rpm -qa` => List all installed packages. 1771 | - `rpm -qf /path/to/file` => List the file's packages. 1772 | - `rpm -ql package` => List all the package's files. 1773 | - `rpm -ivh package.rpm` => Install package 1774 | - `rpm -e package` => Erase/uninstall package. 1775 | 1776 | Options: 1777 | - `-i` : Install package, 1778 | - `-v` : Verbose, 1779 | - `h` : Print hash messages 1780 | 1781 | Examples(yum): 1782 | - `yum search inkscape` => searches online for matching 'inkscape' packages (from mirrors) 1783 | - `yum info inkscape-docs.x84_64` => Gets info on a particular package (Ex: one of matched packages in search) (info gives a brief descrption and specs to help understand what the package is) 1784 | 1785 | If installation/removal requires superuser access: (run `su -s` and switch or `sudo` the command) 1786 | 1787 | - `sudo yum install inkscape` => Installs the 'inkscape' package (With prompt asking for yes(y)/no(n)) 1788 | (OR) 1789 | - `sudo yum install -y inkscape` => Installs the 'inkscape' package (Without prompt) 1790 | 1791 | - `sudo yum remove inkscape` => Removes the 'inkscape' package (With prompt asking for yes(y)/no(n)) 1792 | (OR) 1793 | - `sudo yum remove inkscape` => Removes the 'inkscape' package (Without prompt) 1794 | 1795 | ### Installing packages not included in the package manager 1796 | 1797 | These are the applications/software that are not `yum search`able. In that case, we must: 1798 | 1. Goto the website and directly download the .rpm package file for the application(GOES TO '~/Downloads'), 1799 | 2. Run the `rpm` commands to install (NOT the `yum` commands) 1800 | Ex: 1801 | - `rpm -ivh nautilus-dropbox.fedora-i386.rpm` => Installs the package (Provided it exists [Downloaded]) 1802 | 1803 | ### General package info commands 1804 | 1805 | **(Any installed package)** 1806 | 1807 | - `rpm -qa | sort | less` => Displays all the installed package in alphabetical order on the `less` pager. 1808 | - `rpm -qf /usr/bin/which` => Displays to what package a file belongs to(Ex.o/p: `which-2.20-7.el7.x86_64`) 1809 | - `rpm -ql which` => Lists all the files that are part of the 'which' package 1810 | 1811 | Note: 1812 | 1. While installing a package, it also installs All the Other Packages that this Package depends on. 1813 | 2. Use the `which package-name` command to check if the package was installed(returns location of it)] 1814 | 1815 | ### Installing on Debian Distros with the APT package format 1816 | 1817 | **`apt`** 1818 | 1819 | The 'Debian' distros do NOT use '.rpm' packages but uses `.apt` instead. (Debian distros also includes 'Linux Mint' and 'Ubuntu') 1820 | 1821 | Debian based systems use a package manager called `apt`: 1822 | - `apt` is composed of a few smaller utilities, two of the most famous of them being `apt-cache` and `apt-get`. 1823 | 1824 | Commands: 1825 | - `apt-cache search string` => Searches for a package (online, included in the pkg mgr) matching the 'string'. 1826 | - `apt-get install [-y] package` => Installs the package. If `-y` is supplied, it does NOT prompt for a y/n. 1827 | 1828 | - `apt-get remove package` => Remove a package from the system. (Leave any configuration files undeleted) 1829 | - `apt-get purge package` => Remove a package from the system and delete the configuration files also. 1830 | 1831 | - `apt-cache show package` => Displays info about a package. 1832 | 1833 | ### The dpkg command used in addition to the `apt` utility 1834 | 1835 | **`dpkg`** 1836 | 1837 | Installing from '.deb' that was downloaded to the system - similar to directly installing `.rpm` pkgs. 1838 | 1839 | - `dpkg -l` => List installed packages. 1840 | - `dpkg -S /path/to/file` => List the file's package. (Capital S option) 1841 | - `dpkg -L package` => List all the files in the package. (Capital L option) 1842 | - `dpkg -i package.deb` => Installs the package. 1843 | 1844 | ONCE AGAIN, INSTALLING PACKAGES ON DEBIAN DISTROS ALSO REQUIRES `ROOT` ACCESS. `su` command to switch to root (or) `sudo` command to execute as 'root'. 1845 | 1846 | Note: 1847 | 1. While installing a package, it also installs All the Other Packages that this Package depends on. 1848 | 2. Use the `which package-name` command to check if the package was installed(returns location of it)] 1849 | 1850 | ## The Linux boot process 1851 | 1852 | **Watch Udemy Course for full demo of the boot process** 1853 | 1854 | ### BIOS 1855 | 1856 | - The BIOS stands for Basic Input-Output. 1857 | - It is a special firmware - that checks the hardware connected to a system. 1858 | - It is Operating System Independent (Applies to all OSes and not just Linux) 1859 | - Its primary purpose is to find and execute the 'Boot Loader'. 1860 | - BIOS - performs the POST (Power-On Self Test) which basically tests CPU, MEMORY, etc. 1861 | - Only if the POST succeeds does the BIOS load the 'Boot Loader'. 1862 | 1863 | - BIOS - knows about different 'Boot devices' : Like the Hard Drives, USB Drives, DVD Drives, etc. 1864 | - The BIOS searches the above list for a 'bootable device' in the order specified. 1865 | - The boot device search order can be changed (Interrupt the boot sequence and enter into an interactive mode). 1866 | - The key sequence to do this (change boot device search order) varies from one hardware manufacturer to another. 1867 | 1868 | (Ex: `F2` opens the setup in some systems. You might have to press `F12` and then enable `F2` key from the options.) 1869 | 1870 | Once the 'Bootable Device' is found, the BIOS will run the 'Boot Loader'. This is typically the 'GRUB' (Grand Unified Bootloader) is used. But, on older Linux systems you may find 'LILO' (Linux Loader). The primary purpose of the 'Boot Loader' is to START the OPERATING SYSTEM. Boot loaders could start the Operating System with Different Options. (If there are multiple OSes installed, we can tell the Boot loader which OS to load/run.) 1871 | 1872 | ### Initial RAM Disk 1873 | 1874 | - `initrd` or Initial RAM Disk is a temporary file system that is loaded from Disk and Stored in Memory. It contains helpers and kernel modules (sometimes called 'Drivers') required to load the permanent OS file system. 1875 | 1876 | Once the real OS filesystem has been mounted by `initrd`, its job is done and the loading process continues from the real Operating System File System. 1877 | 1878 | ### The boot directory 1879 | 1880 | **`/boot`** 1881 | 1882 | The '/boot' directory contains the files required to boot Linux: 1883 | 1. initrd 1884 | 2. kernel (The Linux Kernel) 1885 | 3. Boot Loader Configuration 1886 | 1887 | #### Viewing the boot directory 1888 | 1889 | - `ls -F /boot` => Lists the directory with visual classification(-F) 1890 | - The kernel is typically named 'vmlinux' or 'vmlinuz' (Name ends in 'z' if the kernel is compressed) 1891 | - The Initial RAM Disk is 'initrd.img' 1892 | 1893 | NOTE: You can use -F (in `ls` command) which classifies the file with different special character for different kind of files: 1894 | - / – directory. 1895 | - nothing – normal file. 1896 | - @ – link file. 1897 | - * – Executable file 1898 | 1899 | ### The kernel ring buffer 1900 | 1901 | The ring buffer is a data structure maintained by the kernel to store messages from the kernel. It is of fixed size and older messages get deleted when new ones are added. Get kernel messages by executing the command: 1902 | - `dmesg` 1903 | (or) 1904 | - `dmesg -T` (Also displays the time of the message in human-readable format). The message log contains even the earliest messages that fly away quickly during boot process 1905 | 1906 | #### Location of the kernel messages 1907 | 1908 | - `var/log/dmesg` => (Viewing this file is equivalent to running the `dmesg` command) This files contains all kernel messages from start to now, unlike the ring buffer's `dmesg` command. 1909 | 1910 | ## Linux uses run Levels 1911 | 1912 | **(To determine what processes and services to start)** 1913 | 1914 | ### Run levels 1915 | 1916 | - 0 = Shuts down the system. 1917 | - 1, S, s = Single user mode. Used for maintenance. 1918 | - 2 = Multi-User mode with GUI(Debian/Ubuntu). 1919 | - 3 = Multi-User text mode(Red Hat/CentOS). 1920 | - 4 = Undefined. 1921 | - 5 = Multi-User with GUI(Red Hat/CentOS). 1922 | - 6 = Reboot. 1923 | 1924 | ### Setting the run level 1925 | 1926 | Traditionally run levels were controlled by the 'init' program. The File containing 'init' configurations is: `/etc/inittab`. 1927 | 1928 | To change the "Default" Run Level: 1929 | 1. Open '/etc/inittab' file 1930 | 2. Go to the 'initdefault' line and change the run level number. Ex: `id:3:initdefault:` line where 3 is the Default Run Level. 1931 | 1932 | To Change run level: (`telinit`) (NOT the default run level) 1933 | - `telinit 5` => Changes run level to 5 [Multi-User with GUI] 1934 | 1935 | NOTE: 'init' is slowly being phased out by other utilities like 'systemd'. 1936 | 1937 | ### systemd 1938 | 1939 | **`systemd`** 1940 | 1941 | Uses 'targets' instead of run levels. (targets are roughly equivalent to run levels). To get the list of available 'targets', look inside: '/lib/systemd/system' (Ex: `ls -l /lib/systemd/system`, `ls -l /lib/systemd/system/runlevel5.target`) 1942 | 1943 | Run level targets are actually 'symlinks' to the real targets being used. 1944 | 1945 | To get/view the System default run-level target: 1946 | - `systemctl get-default` => (Sample output: 'multi-user.target') 1947 | 1948 | To change the "default" run level or target with systemd: Ex: 1949 | - `systemctl set-default graphical.target` ('graphical.target' is equivalent to 'run-level 5' (Multi-User GUI)) 1950 | 1951 | To change the target/run level target: (NOT the default run level target) 1952 | - `systemctl isolate graphical.target` => Changes run level to 'graphical.target'. 1953 | 1954 | ## Rebooting 1955 | 1956 | Even though we can use the: 1957 | - `telinit 6` (or) 1958 | - `systemctl isolate reboot.target` to reboot the system, 1959 | We can also use system command: 1960 | - `reboot` => Reboots the system. 1961 | 1962 | - `Shutdown` command for rebooting: 1963 | 1964 | Even though we can use: `telinit 0` to shutdown, there exists commands to shutdown the system: 1965 | - `shutdown [options] time [message]` 1966 | - `-r` option => Tells the system to reboot after shutdown! 1967 | 1968 | 'time' formats: 1969 | 1. `HH:MM` = Shutdown at HH hrs and MM mins. 1970 | 2. `+N` = Waits for N minutes before performing shutdown. 1971 | 3. `now` = Shuts down immediately. 1972 | 1973 | 'message': This is a broadcast message sent to all users on the system that it is being shutdown/rebooted. (All logged-in users are notified that the system is going down, and login operations are blocked.) 1974 | 1975 | Ex: 1976 | - `shutdown -r 15:30 "Rebooting!"`, 1977 | - `shutdown -r +5 "Rebooting soon!"`, 1978 | - `shutdown -r now` 1979 | 1980 | Power Off a system: (3 main ways) 1981 | 1. `telinit 0` 1982 | 2. `systemctl isolate poweroff.target` (Selects the 'poweroff' target) 1983 | 3. `poweroff` (Simple command that can be executed at the CLI to power off the system) 1984 | 1985 | ## The system log 1986 | 1987 | Aids in the process of messages. (Each process need not have to create its own log files). Allows logging to be centrally controlled. Uses facilities and severities to categorize messages. 1988 | 1989 | ### Facilities 1990 | 1991 | What type of program / what place in the system the message originated from. 1992 | ``` 1993 | 0 kern kernel messages 1994 | 1 user user-level messages 1995 | 2 mail mail system 1996 | 3 daemon system daemons 1997 | 4 auth security/authorization messages 1998 | 5 syslog messages generated by syslogd 1999 | 6 lpr line printer subsystem 2000 | 7 news network news subsystem 2001 | 8 uucp UUCP subsystem 2002 | 9 clock daemon 2003 | 10 authpriv security/authrization messages 2004 | 11 ftp File Transfer Protocol 2005 | ... 2006 | 15 cron clock daemon 2007 | 16 local0 local use 0 2008 | 16 local1 local use 1 2009 | ... 2010 | 16 local7 local use 7 2011 | ``` 2012 | 2013 | We can use local0 to local7 for our own purposes. 2014 | 2015 | ### Severities 2016 | 2017 | ``` 2018 | 0 Emergency emerg(panic) System is unusable 2019 | 1 Alert alert Take action immediately 2020 | 2 Critical crit Critical Conditions 2021 | 3 Error err (error) Error conditions 2022 | 4 Warning warning (warn) Warning conditions 2023 | 5 Notice notice Normal but significant condition 2024 | 6 Info info Informational messages 2025 | 7 Debug debug Debug-level messages 2026 | ``` 2027 | 2028 | ### rsyslog 2029 | 2030 | **`rsyslog`** 2031 | 2032 | `rsyslog` is one the syslog servers in use. 2033 | 2034 | 1. Main configuration file for `rsyslog`: '/etc/rsyslog.conf' 2035 | 2036 | 2. Add additional configuration files: `IncludeConfig /etc/rsyslog.d/*.conf` => The `IncludeConfig` directive asks the rsyslog to add any file ending with '.conf' and existing in the '/etc/rsyslog.d/' directory. 2037 | 2038 | Logging rules: 2039 | 1. Selector field: Syntax: `FACILITY.SEVERITY` 2040 | - '*' severity for all[Ex: 'mail.*' <=> 'mail'] (Wildcards supported for both facilities and severities), 2041 | - 'none' severity for none[Ex: mail.none], 2042 | - 'mail.emerg;ftp.err;cron.info' => Match multiple severities with semicolon(;) 2043 | 2044 | 2. Action Field: How a message is processed. 2045 | 2046 | ### Caching vs non-caching 2047 | 2048 | Caching is used if the path starts with a hyphen(-) Ex: 'mail.*' logs saved to '-/var/log/mail.info' 2049 | 2050 | You may lose some messages during a system crash if you are using the crash mode. Using caching mode can improve I/O performance. 2051 | 2052 | Different severities can have different caching modes: Ex: 2053 | - `mail.info` ===> '-/var/log/mail.info' 2054 | - `mail.warn` ===> '-/var/log/mail.warn' 2055 | - `mail.err` ===> '/var/log/mail.err' (No caching) 2056 | 2057 | Lower severities are cached while higher severities are not cached. 2058 | 2059 | Generate 'syslog' messages: Use the `logger` command. Ex: 2060 | - `logger [options] message`. 2061 | 2062 | Options for `logger`: 2063 | - `-p FACILITY.SEVERITY` => Defaults to 'user.notice' if nothing is specified. 2064 | - `-t TAG` => Tag our messages in the log file. 2065 | 2066 | Ex: 2067 | - `logger -p mail.info -t mailtest "Test."` 2068 | - `sudo tail -1 /var/log/mail.log` => (Sample o/p: 'Apr 4 14:33:16 linuxsvr mailtest: Test.') 2069 | 2070 | NOTE: `logrotate` command => Did not learn (go back to videos if you wish to learn) 2071 | 2072 | NOTE: Removing blank lines and comment lines from a file/stdin: 2073 | - `grep -Ev '^#|^$' fileName` 2074 | - The ^ stands for beginning of line in regular expression pattern. (^# => comments) 2075 | - The $ stands for the end of the line in regular expression pattern. (^$ => Blank lines) 2076 | - | stands for OR (this[left side] or that[right side]) 2077 | 2078 | ## Disk management 2079 | 2080 | Disks can be divided into parts - called Partitions. Partitions allow you to separate data. Participation Schemes: Ex: 2081 | 1. OS | Application | User | Swap, 2082 | 2. OS | User Home Directories | Etc.. 2083 | 2084 | (As a system administrator, you get to decide) 2085 | 2086 | ### Advantages of partitioning 2087 | 2088 | Can protect the overall system. Keep users from creating outages by using a home directory partition. (Ex: If the system runs a web server, we can partition OS and the server on the disk, so damage/outage in one won't affect the other, esp. the OS will still keep running) 2089 | 2090 | ### Master Boot Record 2091 | 2092 | **(MBR)** 2093 | 2094 | - MBR - It's a 'boot sector' that exists at the beginning of partitioned computer mass storage devices like fixed disks or removable drives. 2095 | - MBR = Boot Sector (sectors, tracks, cylinders ...) at the beginning of a storage device 2096 | - MBR contains information about how the 'logical partitions' are 'organized' on the disk. The information is contained in a Partition Table. 2097 | - MBR allows UPTO '4' PRIMARY partitions. 2098 | 2099 | If you want to use more than 4, we need to use an 'Extended Partition'. An Extended Partition is a special kind of primary partition that is used as a 'container' for OTHER partitions. (Hence, create unlimited number of partitions inside the extended partition) 2100 | 2101 | Disadvantage of MBR: Can ONLY address 2TB of disk space. 2102 | 2103 | ### GUID Partition Table 2104 | 2105 | **(GPT)** 2106 | 2107 | - It is slowly replacing MBR as the boot sector of the partitioned disks. 2108 | - GUID = Global Unique Identifier. 2109 | - GUID is actually part of UEFI(Unified Extensible Firmware Interface) that is gradually replacing BIOS 2110 | - GPT has been already used in some BIOS systems because of the MBR's disadvantage(support only 2tb space). 2111 | - GPT: NO Concept of Primary/Extended Partitions 2112 | 2113 | GPT Supports: 2114 | - Upto 128 Partitions. 2115 | - Upto 9.4ZB Disk Sizes. (ZB = Zeta Byte) 2116 | 2117 | (GPT NOT supported by older OSes and May require Newer or Special Tools) 2118 | 2119 | ### Mount points 2120 | 2121 | A mount point is simply a DIRECTORY that is USED to ACCESS THE DATA on a Partition. 2122 | 2123 | - '/' (slash) => It is always a Mount Point.(At least 1 Partition is mounted on the '/' directory). Any other additional partitions are mounted Inside the '/' Directory Tree. 2124 | 2125 | Ex: If we allocated a partition to the '/home' directory (mounted), then all the files and directories inside it can be found under that partition. (Ex: '/home/jason' is on the partition mounted on '/home') 2126 | 2127 | If we, say, umounted (remove) the `/home` partition and instead allocate it to the '/export/home' direcory (mount) then all the files inside '/home' will be available under the mounted partition of '/export/home'. (Ex: '/export/home/jason' available under the mounted partition '/export/home') 2128 | 2129 | ### Mount partitions over existing data 2130 | 2131 | We can mount partitions over existing data. For example, if files(or directories) were create inside '/home' before the '/home' partition was mounted/create, those files will NOT be accessible after '/home' is mounted as a partition. They will exist but you not be able to access them. 2132 | 2133 | Ex: Assume '/home' is not mounted and '/' is the existing mount: 2134 | - `mkdir /home/sarah` 2135 | - `mount /dev/sdb2 /home` ('/home' mounted/partitioned) 2136 | 2137 | You will not be able to access '/home/sarah' now. Data for that folder exists on '/' partition. Therefore, We cannot access the '/home/sarah' folder from the '/home' partition. 2138 | 2139 | - `unmount /home` (Removing the '/home' partition, so files inside belong once again to '/' mount). You can now access '/home/sarah' once again since mount was '/' when 'sarah' directory was created 2140 | 2141 | ### Mount points over other mount points 2142 | 2143 | This is possible. For Example: If '/home' is a mount point, we can create another mount point '/home/jason' over the existing '/home' mount point. (The important thing to note is that '/home' must be mounted BEFORE mounting '/home/jason'!). 2144 | 2145 | 2146 | ### fdisk to create and modify partitions on a disk 2147 | 2148 | **`fdisk`** 2149 | 2150 | `fdisk` is a standard linux tool or a utility that has been traditionally used to CREATE and MODIFY PARTITONS on a Disk. (Alternatives: `gdisk` or `parted`) 2151 | 2152 | Note:: Earlier version of `fdisk` are NOT supported by GPT. 2153 | 2154 | To manage the partitions on a disk using the `fdisk` utility, simply provide the 'path' to the 'device' you wish to manage as an argument to the command, Ex: 2155 | - `fdisk /path/to/device` 2156 | 2157 | - `fdisk -l` => Displays a list of available devices('disks') and all the 'partitions' they contain. You may like to use `fdisk -l | less`. (The above will list the disks and the partitions that they have, if any.) 2158 | 2159 | - `fdisk -l /dev/sda` => Displays a specific disk device's partitions (and its nested partitions). 2160 | 2161 | - `fdisk /dev/sdb` => Opens the command utility for '/dev/sdb' disk device (Use 'm' for commands help) (Once you run this command, the `fdisk` utility opens up, with its own commands:) 2162 | 2163 | Commands inside `fdisk` device manager: 2164 | - p: print the partition table 2165 | - n: create a new partition 2166 | - d: delete a partition 2167 | - q: quit without saving changes 2168 | - w: write the new partition table and exit 2169 | - l: View a list of partitions along with their numbers 2170 | 2171 | #### CREATE an MBR partition 2172 | 2173 | **(Inside a disk using `fdisk` device manager)** 2174 | 2175 | 1. Press `n` to create a partition 2176 | 2177 | - Prompted to press 'p' for primary partition (or) 'e' for extended. (You chose 'p' - primary, say) 2178 | - It will ask you to choose partition number, 1 to 4. (Default is partition 1) (You chose 1, say) 2179 | - It will prompt you to select a start address from X-to-Y. (Default is X) (You chose X, say) 2180 | - It will ask you for the size of the partition. Format to enter: +NS (Ex: You typed '+1G', say) 2181 | 2182 | (S is size = K for KiloBytes, M for MegaBytes, G for GigaBytes) 2183 | (N is the quanity. Ex: '+1G' means you chose 1 GigaByte of data from start address(X) for that partition) 2184 | 2185 | - Partition is Created (Inside the selected disk device) 2186 | 2187 | Example Output: 'Partition 1 of type Linux and of 1 GiB is set' 2188 | 2189 | Once you create a partition 'x' (1 <= x <= 4), the next partition will ask you to select a partition number from '1-4 excluding x'. Suppose you selected 1 initially then next time it will ask you to select a partition number from 2-4. 2190 | 2191 | The default size for any partition is the full remaining size that you have left. For example you may create 3 partitions of sizes 1GB, 2GB, and for the third one just hit `` and the default/remaining size is selected for it. 2192 | 2193 | Note: Default partition type created is 'Linux' and represented by the number '83'. 2194 | 2195 | 2. CHANGE the partition TYPE: 2196 | - Type 't' 2197 | - Output is 'selected partition is 1' 2198 | - You are prompted for the Partition number(A hex number) [Says type 'L' for help with partition types] 2199 | - Type 'l' to see the list of partitions and their numbers. 2200 | 2201 | (Say, Linux is 83 and you want to change to 'linux Swap' type, which is '82') 2202 | 2203 | (Note: The numbers are in hexadecimal format, so even 'fe' is a number) 2204 | 2205 | - You are prompted again for the Partition type Hex number. You Type '82' (say) 2206 | - Sample Output: `Changed type of partition 'Linux' to 'Linux Swap / Solaris'` 2207 | 2208 | Note: You may repeat step (A) and, optionally step (B), for subsequent partitions you may want to create (and change the type of). 2209 | 2210 | #### View all the existing partitions 2211 | 2212 | **(Partition Table)** 2213 | 2214 | - Type `p`. 2215 | 2216 | #### DELETE a partition 2217 | 2218 | **(Inside the chosen disk)** 2219 | 2220 | - Type 'd' 2221 | - As we enter ‘d‘, it will prompt me to enter partition number that we want to delete from disk.(Ex: '4') 2222 | - It will delete that partition number (Ex: '4') on disk and shows free space in partition table. 2223 | 2224 | #### SAVING all the partitions added or deleted in the fdisk utility 2225 | 2226 | **(Also EXITING/QUITTING)** 2227 | 2228 | - Type `w` : It saves all the partitions it showed in the partition table (whatever we added/deleted) and quits the utility back to the command prompt. 2229 | 2230 | #### QUITTING without Saving 2231 | 2232 | - Type `q`. 2233 | 2234 | #### CREATING a GPT Partition 2235 | 2236 | **(Inside a Disk using the `fdisk` utility)** 2237 | 2238 | 1. Type 'g': (Prints message that you 'building a new GPT disklabel') 2239 | - Simialr commands to MBR. (n-create, p-print GPT table, d-delete partition, w-save&quit, q-quit) 2240 | - Only thing to remember is that instead of '1-4' partition numbers, there are '1-128'. 2241 | - No primary/extended partitions like in MBR. (All partitions are equival) 2242 | 2243 | ### File systems 2244 | 2245 | Before a partition can be used by a system, it will need a File System. 2246 | 2247 | - `ext` : Extendeded file system was create specifically for linux and is the default(ext2, ext3, ext4 are later releases) 2248 | 2249 | - Other File Systems: 'ReiserFS', 'JFS', 'XFS', 'ZFS', 'Btrfs' 2250 | 2251 | #### Create a file system 2252 | 2253 | - `mkfs -t TYPE DEVICE` => Creates a file system of specified TYPE on the mentioned disk DEVICE. (DEVICE: path to the partition where you want the file system to reside). (Ex: `mkfs -t ext3 /dev/sdb2`) 2254 | 2255 | Note: We may also use dot(.) notation instead of `-t`: Ex. `mkfs.ext4 /dev/sdb3` 2256 | 2257 | Location of the mkfs files: `ls -l /sbin/mkfs*` 2258 | 2259 | 'mkfs' help: `man mkfs.ext2` to find more info about the ext2 file system creation commands. 2260 | 2261 | ### Mounting a device partition 2262 | 2263 | **(Mount Point is simply a directory which we place a device partition on)** 2264 | 2265 | (After creating and assigning a file system.) 2266 | 2267 | - `mount DEVICE MOUNT_POINT` => Mounts a device partition to the directory specified. 2268 | 2269 | Ex: 2270 | - `mount /dev/sdb3 /opt` 2271 | 2272 | #### Viewing the currently mounted file systems 2273 | 2274 | - `mount` => No Args - Therefore, mount displays all the filesystems (physical as well as virtual file systems.) 2275 | 2276 | Manual mounts do NOT persist!: 2277 | 2278 | In order to makes mounts persist between reboots, add an entry in the '/etc/fstab' file. 2279 | 2280 | #### Unmount a file System 2281 | 2282 | **(`umount` command)** 2283 | 2284 | - `umount DEVICE_OR_MOUNT_POINT` 2285 | 2286 | Ex: 2287 | - `umount /opt` (unmount using mount point) 2288 | - `umount /dev/sdb3` (unmount using device partition) 2289 | 2290 | #### Preparing a swap space 2291 | 2292 | Instead of creating a file system and mounting it, we can create a 'Swap Area' and 'Enable' it. 2293 | - `mkswap DEVICE` => Creates a swap space.(Ex: `mkswap /dev/sdb1`) 2294 | - `swapon DEVICE` => Enables the created swap space.(Ex: `swapon /dev/sdb1`) 2295 | - `swapon -s` => Displays the swap devices in use. 2296 | 2297 | ### The file system table 2298 | 2299 | **`/etc/fstab`** 2300 | 2301 | Controls what devices get mounted and where on boot. Each entry(one line) has 6 fields: 2302 | 1. Device (label/path-to-device (or) UUID) 2303 | 2. Mount point 2304 | 3. File system type 2305 | 4. Mount options (multiple options separated by a comma(,) but No spaces in between) 2306 | 5. Dump Utility (Dump = 0 : Ignore FS, Dump = 1 : Backup the FS) 2307 | 6. fsck order (file system check order - On boot) [0: skip FS check, 1: checked first, 2: checked next] 2308 | (Lines starting with '#' are comments and are ignored) 2309 | 2310 | Example Entries: 2311 | - `/dev/sda2 / xfs defaults 0 1` 2312 | - `/dev/sda1 swap swap defaults 0 0` 2313 | 2314 | Example using UUID: 2315 | - `UUID=dbae4fe7-b06f-4319-85dc-b93ba4a16b17 / xfs defaults 0 1` 2316 | 2317 | You may ignore the dump utility column (leave it at 0) if you do not use it to backup filesystems.Good practice to set `fsck` of '/' to 1 and remaining FSes to '2' 2318 | 2319 | - `man fstab` => Information about the full list of options. 2320 | 2321 | #### Viewing labels and UUIDs of file systems 2322 | 2323 | - `lsblk -f` => Shows label, name, fstype, and UUIDs of devices. 2324 | - `blkid` => 'Shows the path, type and UUIDs of devices' 2325 | 2326 | #### Labelling a file system 2327 | 2328 | **(Changing the name)** 2329 | 2330 | For 'ext' filesystems we can use the `e2label` 2331 | - `e2label DEVICE MOUN_POINT` => Changes label of device(FS) at mount point(directory). 2332 | 2333 | Ex: 2334 | - `e2label /dev/sdb3 opt` 2335 | 2336 | ## Managing users and groups 2337 | 2338 | Linux is a multi-user OS. The multi-users can also use the system at the Same Time! Each user account has the follwoing fields associated: 2339 | 1. Username (or Login ID) 2340 | 2. UID (user ID). This is a unique number. 2341 | 3. Default group (to which user belongs) (GID or group ID) 2342 | 4. Comments 2343 | 5. Home directory location 2344 | 6. Shell (Shell to execute when user logs into the system) 2345 | 2346 | All the user information(above) is stored in the - `/etc/passwd` file: Separated by a colon(:). The FIRST ENTRY in the file is the ROOT/SuperUser account. Format of each user's account(one entry = one line = one user): 2347 | - `username:password:UID:GID:comments:home_dir:shell` 2348 | 2349 | ### The root account 2350 | 2351 | - `root:x:0:0:root:/root:/bin/bash` => (root user with x password, 0 uid, 0 gid, comment 'root', '/root' home dir, '/bin/bash' default shell to execute on login). 2352 | 2353 | UID and GID for the root account are '0'. 2354 | 2355 | Other user account example: 2356 | - `joe:x:1000:1000:Joe Henderson:/home/joe:/bin/bash` 2357 | 2358 | NOTE!: password is 'x' - Exncrypted password is actually stored in the '/etx/shadow' file. 2359 | 2360 | Note: 2361 | 1. Better to have Usernames less than 8 characters or else[convention] we see + sign appended at 8th character position (or UID instead). Ex: Run this command for a long username: `ps -fu joehenderson`. 2362 | 2. Usernames are case-sensitive. (All lowercase by convention). 2363 | 3. Numbers are allowed in usernames. 2364 | 4. Do Not use special characters. 2365 | 2366 | ### Passwords are stored in a shadow file 2367 | 2368 | **'/etc/shadow'** 2369 | 2370 | Encrypted passwords used to be(earlier) stored in 'etc/passwd'. But, '/etc/passwd' is readable by "everyone". Now(current linux), encrypted passwords are stored in '/etc/shadow'. '/etc/shadow' is readable by 'root'/'superuser' alone. This prevents users trying to crack passwords. 2371 | 2372 | ### UIDs 2373 | 2374 | The root/superuser account always has `UID = 0`. UIDs are unique numbers. System accounts typically have UIDs less than 1000 (< 1000). (Configured in '/etc/login.defs') 2375 | 2376 | ### GIDs 2377 | 2378 | The GID listed in the '/etc/passwd' file is the default group for an account. New files belong to a user's Default group. 2379 | Users can switch groups using the `newgrp` command. (This can be done before creating new files for the new group) 2380 | 2381 | Note: Systems or applications also have accounts - viewable inside the '/etc/passwd' file. 2382 | 2383 | ### Comment field 2384 | 2385 | - Typically contains the user's full name. 2386 | - In the case of system or application accounts, it often contains what the account is used for. 2387 | - It may contain additional info, like phone number. 2388 | - Also called the GECOS field. 2389 | 2390 | ### Home directory 2391 | 2392 | Upon login, the user is placed inside his HOME directory (Ex: '/home/jason' for user 'jason'). If this directory does NOT exist then he is placed in the root directory('/'). 2393 | 2394 | ### Shell 2395 | 2396 | The shell will be executed when a user logs in. List of available shells are in '/etc/shells'. The shell does NOT have to be a shell: 2397 | 2398 | Example: To prevent the interactive use of an account, use : '/usr/sbin/nologin' (or) '/bin/false' as the shell. 2399 | (In the above, No one can execute the shell interactively, but only execute a menu-driven application that only gives them access to certain actions). Shells can be command line applications. 2400 | 2401 | ### The /etc/shadow file 2402 | 2403 | Contains the encrypted passwords of the user accounts. 2404 | 2405 | Format: 2406 | `username:encryptedpass:dayssincepasswordchanged:numdaysbeforewhichpasswordmustbechanged:daystochangepass(99999-neverchange):daystowarnusertochangepass:numdaysafterpasswordexpiredtodisableacct:numdayssinceacctdisabled:futureuse` 2407 | 2408 | Ex: 2409 | `root:$@234524#242Dde#$3:16502:0:99999:7:::` 2410 | 2411 | 2412 | ### Creating a user account 2413 | 2414 | **(Requires root account privileges - Ex> use 'sudo')** 2415 | 2416 | Syntax: `useradd [options] username` 2417 | 2418 | Options: 2419 | - `-c "COMMENT"` => Comments for the account. 2420 | - `-m` => Create the home directory 2421 | - `-s /shell/path` => Path to the user's shell 2422 | - `-g GROUP` => Specify the default groud. 2423 | - `-G GROUP2,[...GROUPN]` => Additional groups(no spaces between commas) 2424 | 2425 | Note:: Specify option `-u UID` to explicitly set the UID of the account being created. Ex: `-u 97`. 2426 | 2427 | Ex: 2428 | - `useradd -c "Grant Stewart" -m -s /bin/bash grant`, 2429 | - `useradd -c "Eddie Harris" -m -s /bin/bash -g sales -G projectx harris` (Added to sales and projectX grps) 2430 | 2431 | ### Create a password for the created user 2432 | 2433 | Syntax: `passwd username` 2434 | 2435 | Ex: 2436 | - `passwd grant` => System asks for password for the user 'grant' (and a retype to confirm). 2437 | 2438 | Note: The created user entry and his password are "Appended" to the '/etc/passwd' and '/etc/shadow' files respectively. 2439 | 2440 | ### System or application accounts 2441 | 2442 | Not every account is meant to be for a user. Some accounts exist to run applications or perform system functions. Examples of these accounts include those that run web server processes, database server processes, etc. 2443 | 2444 | Extra Options: 2445 | - `-r` : Requests create an application/system account. (This means that the application receives a UID in the application UIDs range)(As defined in the '/etc/login.defs' file) 2446 | - `-d HOME_DIR` : Specify Home Directory using the `-d` option (instead of the `-m`) - we can give location. (Default home directory, if not specified in -d, is `/home/acctname`) 2447 | 2448 | Ex: 2449 | - `useradd -c "Apache Web Server User" -d /opt/apache -r -s /usr/bin/nologin apache` (We do Not want someone to login to this system using the application account - hence => /usr/bin/nologin) 2450 | 2451 | #### The -m option 2452 | 2453 | **`-m`** 2454 | 2455 | When using the `-m` option, the Home directory for the user is created. The contents of '/etc/skel' (stands for 'skeleton') are copied into the User's Home directory. This '/etc/skel' contains shell "configuration files" ('.profile', '.bashrc', etc) 2456 | 2457 | ### Deleting an account 2458 | 2459 | Syntax: `userdel [-r] username` 2460 | 2461 | Ex: 2462 | - `userdel grant` => Deletes user 'grant' from system but keeps his home folder un-deleted. 2463 | - `userdel -r grant` => Deletes user 'grant' from system and also deletes his home folder. (The `-r` also removes the user's mailspool file if it exists.) 2464 | 2465 | ### Mpdify an existing account 2466 | 2467 | Syntax: `usermod [options] username` 2468 | 2469 | Similar options to `useradd`: 2470 | - `-c "COMMENT"` => Comments for the account. 2471 | - `-s /shell/path` => Path to the user's shell 2472 | - `-g GROUP` => Specify the default groud. 2473 | - `-G GROUP2,[...GROUPN]` => Additional groups(no spaces between commas) 2474 | 2475 | Ex: 2476 | - `usermod -c "MYSQL User" mysql` => Updates comment associated with a MySQL account. 2477 | 2478 | 2479 | ### Group details and creation 2480 | 2481 | The group details are stored in the '/etc/group' file. 2482 | 2483 | Format of the entries in the file: `group_name:password:GID:account1,...,accountN` 2484 | 2485 | (Here too, password is 'x'). 2486 | 2487 | GID is the group ID - A unique ID for the group. 2488 | 2489 | FIRST entry in the '/etc/group' file is the 'Root Group'. Ex: `root:x:0:` 2490 | 2491 | Other group example: `sales:x:1001:john,mary` 2492 | 2493 | IMPORTANT NOTE: Users whose default is a certain group are NOT shown in the entry for that group in '/etc/group' file. 2494 | BUT, we can check the '/etc/passwd' file to find the user's default group (or) run `groups user-name`. 2495 | 2496 | ### The /etc/gshadow file 2497 | 2498 | **`/etc/gshadow`** 2499 | 2500 | The encrypted group passwords(x) are stored in the '/etc/gshadow' file. 2501 | 2502 | Groups that a member belongs to: `groups [USERNAME]` 2503 | 2504 | Ex: 2505 | - `groups root` => Displays all groups that root belongs to. 2506 | - `groups` => Displays your(currently logged in user) groups (groups that you as the user belong to) 2507 | 2508 | #### Create groups 2509 | 2510 | `groupadd [-g GID] GROUP_NAME` 2511 | 2512 | Ex: 2513 | - `groupadd web` => Adds the 'web' group. 2514 | - `groupadd -g 2500 db` => Adss the 'db' group and also explicitly sets the GID to 2500. 2515 | 2516 | #### Delete a group 2517 | 2518 | `groupdel GROUP_NAME` 2519 | 2520 | Ex: 2521 | - `groupdel db` => Deletes the 'db' group. 2522 | 2523 | #### Modify a group: 2524 | 2525 | `groupmod [options] group_name` 2526 | 2527 | Options are: 2528 | - `-g GID` => Change group ID to specified GID. 2529 | - `-n GROUP` => Change group name to specified name 'GROUP'. 2530 | 2531 | ## Special permission modes 2532 | 2533 | When we start a process(execution), it runs using the User's UID and GID (we may have run it as others used 'su'/'sudo' etc for root, doesn't matter.) 2534 | 2535 | 2536 | ### The setuid bit 2537 | 2538 | **`setuid`** 2539 | 2540 | We can explicitly set a UID before execution of a process: 2541 | - `setuid` => Set User ID upon execution. 2542 | - `setuid` FORCES the process to run as THE OWNER of the file regardless of who executes it. 2543 | 2544 | How to check/tell if setuid is enabled?: 2545 | - `ls -l` => `-rwsrw-r-x ..` The 's' in the Owner's execution field(x) tells that setuid is enabled. 2546 | 2547 | Examples of commands and files that run with setuid/as owner of the file: 2548 | 1. '/usr/bin/passwd' (Ex: need to be owner when changing the password) 2549 | 2. `ping` command (Needs root privileges) 2550 | 3. `chsh` command - Allows users to update their shell, etc... 2551 | 2552 | #### Security measures 2553 | 2554 | - It is prone to attack by hackers/malicious users since it always runs on owner(usually root) access. 2555 | - It is not honored on shell scripts - Scripts will execute as user who runs the script even if the setuid bit is set for the script. (Only 'binary executable' files work with setuid bit enabled) 2556 | 2557 | #### Octal permissions 2558 | 2559 | - setuid: 0, setgid: 0, sticky: 0 => Value for OFF (total 0) 2560 | - setuid: 1, setgid: 1, sticky: 1 => Binary Value for ON (total 3) 2561 | - setuid: 4, setgid: 2, sticky: 1 => Base 10 Value for ON (total 7) 2562 | 2563 | Good: 4755 or below, Bad: 4775, Really bad: 4777 (anyone can edit the file!) 2564 | 2565 | (Ex: 4775 or 4777 is what an attacker hopes to find in your system if they break in! - they can do anything they want to that file and maybe get root permissions.) 2566 | 2567 | #### Adding the setuid attribute to a file 2568 | 2569 | We can use the `chmod` command. Ex: 2570 | 1. `chmod u+s /path/to/file` (symbolic notation) 2571 | 2. `chmod 4755 /path/to/file` (octal notation) - the ADD to MSBit 4, the setuid bit/special bit 2572 | 2573 | #### Removing the setuid attribute from a file 2574 | 2575 | Again, we can use the `chmod` command. Ex: 2576 | 1. `chmod u-s /path/to/file` (symbolic notation) 2577 | 2. `chmod 0755 /path/to/file` (octal notation) - '0' => setuid disabled 2578 | 2579 | #### Find all the files on the system that have setuid set 2580 | 2581 | - `find / -perm /4000`, 2582 | (or, older style:) 2583 | - `fidn / -perm +4000` 2584 | 2585 | 2586 | ### The setgid bit 2587 | 2588 | **`setgid`** 2589 | 2590 | `setgid` => Set Group ID upon execution. (Ex: `-rwxr-sr-x ..` => The execution bit(x) of the 'group' is set to 's' - setgid enabled) 2591 | 2592 | #### Examples of commands using this setgid bit 2593 | 2594 | - `/usr/bin/wall` : anybody who can edit this file can write whatever they want to the terminal(check). 2595 | 2596 | #### Finding setgid files 2597 | 2598 | - `find / -perm /2000`, 2599 | (or, older style:) 2600 | - `find / -perm +2000` 2601 | 2602 | #### Adding setgid permission 2603 | 2604 | We can use the `chmod` command. Ex: 2605 | 1. `chmod g+s /path/to/file` (symbolic notation) 2606 | 2. `chmod 2755 /path/to/file` (octal notation) - the ADD to MSBit 2, the setgid bit/special bit 2607 | 2608 | #### Removing the setgid attribute from a file 2609 | 2610 | Again, we can use the `chmod` command. Ex: 2611 | 1. `chmod g-s /path/to/file` (symbolic notation) 2612 | 2. `chmod 0755 /path/to/file` (octal notation) - SUBTRACT 2 from special permissons field 2613 | 2614 | #### Adding both setuid and setgid 2615 | 2616 | 1. `chmod ug+s /path/to/file` (symbolic notation) 2617 | 2. `chmod 6755 /path/to/file` (octal notation) 2618 | 2619 | NOTE: 2620 | 1. Setting the 'setgid' on a Directory causes: 'New' Files & Directories inside the directory to "inherit" the group of the directory. (Pre-existing files/directories within the directory are NOT affected by the setgid.) 2621 | 2. Because of the above point, 'setgid' is "great for working with 'groups'". We can create a folder with a group's GID and appropriate/desired group permissions to the directory. So, whatever is added/deleted/modified inside the directory can be accessed by everyone belonging to the group (Shared folder). 2622 | 2623 | ** THIRD PARTY TOOLS TO CHECK FOR SETUID AND SETGID ON FILES (alternatives to 'find'): ** 2624 | 2625 | Ex: tripwire, AIDE, OSSEC, Samhain, Package managers 2626 | 2627 | ### The sticky bit 2628 | 2629 | Used on a directory to ONLY allow the OWNER of the file/directory to RENAME (or) DELETE the file. Without the sticky bit set, another user to delete a user's files IF the permissions(777, say) allowed for it. Sticky Bit reperesented by 't' on others(o). (Ex: `-rwxr-xr-t ...`) 2630 | 2631 | Example: Used on '/tmp' or '/var/tmp' 2632 | 2633 | #### Adding the sticky bit 2634 | 2635 | We can use the `chmod` command. Ex: 2636 | 1. `chmod o+s /path/to/file` (symbolic notation) 2637 | 2. `chmod 1777 /path/to/file` (octal notation) - the ADD to MSBit 1, the special bit 2638 | 2639 | (You would typically set sticky bit on 777 permissions because that is where it makes sense to use the sticky bit to only allow user to rename/delete the files/directories even when everyone else has permissions for it.) 2640 | 2641 | #### Removing the sticky bit 2642 | 2643 | We can use the `chmod` command. Ex: 2644 | 1. `chmod o-t /path/to/file` (symbolic notation) 2645 | 2. `chmod 0777 /path/to/file` (octal notation) - the SUBTRACT 1 from the special bit 2646 | 2647 | 2648 | #### Reading the ls command output 2649 | 2650 | Capitalized special permission bit => Means underlying normal permissions are NOT set. 2651 | - Ex: `-rwSr-xr-- ..` 2652 | - Ex: `-rwxr-xr-T ..` 2653 | 2654 | Lowercase special permission bit => Means underlying normal permissions are SET. 2655 | - Ex: `-rwsr-xr-- ..` 2656 | - Ex: `-rwxr-xr-t ..` 2657 | 2658 | ## Networking 2659 | 2660 | 2661 | ### TCP/IP: 2662 | 2663 | The defacto standard for communication. 2664 | - TCP - controls data exchange 2665 | - IP - sends data from one device to another 2666 | - Hosts - Devices on a network. 2667 | 2668 | #### IPv4 Classes 2669 | 2670 | ``` 2671 | 1.0- 127.0 = Class A (Subnet Mask: 255.0.0.0) 2672 | 128.0 - 191.255 = Class B (Subnet Mask: 255.255.0.0) 2673 | 192.0.0 - 223.255.255 = Class C (Subnet Mask: 255.255.255.0) 2674 | ``` 2675 | 2676 | #### Classless Inter-Domain Routing 2677 | 2678 | **CIDR** 2679 | 2680 | Dividing networks irrespective of their classes. Division depends on subnet mask. Ex: 2681 | 2682 | CIDR Subnet: 255.255.255.0 (given) 2683 | N/W Address: 121.67.198.0 (According to class A, it would have been 121.0.0.0) 2684 | B/C Address: 121.67.198.255 (According to class A, it would have been 121.255.255.255) 2685 | 2686 | #### Reserved private address space 2687 | 2688 | Ranges of IP addresses reserved for use in private (Non-Routable address spaces): 2689 | - 10.0.0.0 to 10.255.255.255 => Reserved private address space in class A. 2690 | - 172.16.0.0 to 172.31.255.255 => Reserved private address space in class B. 2691 | - 192.168.0.0 to 192.168.255.255 => Reserved private address space in class C. 2692 | 2693 | Any of these IP address entries in the hosts file (/etc/hosts) is considered private and non-routable publicly. 2694 | 2695 | #### Knowing the host computer IP address 2696 | 2697 | **(Or, all IPs associated with your computer)** 2698 | 2699 | Command: `ip address` (or) `ip address show` 2700 | 2701 | (Shortcuts: `ip addr` (or) `ip a` (or) `ip a s`) 2702 | 2703 | (Not available in Unix (Only Linux) - use `ifconfig` for unix.) 2704 | 2705 | This command shows two addresses: 2706 | 1. `lo: inet:127.0.0.1` => Your loopback address. (lo stands for 'loopback') 2707 | 2. `eth0: inet:192.168.1.122/24` => Actually hardware NIC device - has an IP address associated. 2708 | 2709 | (Also, it shows MAC addresses and Subnet Masks) 2710 | 2711 | #### Another way to determine the host IP address 2712 | 2713 | **`ifconfig`** 2714 | 2715 | Another way to determine host's IP addresses. (DEPRECATED, but still very useful-maybe around for sometime) 2716 | 2717 | Command: `ifconfig` => Displays all the IP addresses associated with the computer. 2718 | 2719 | Terms: 2720 | - HOST : A device connected to a Network. 2721 | - HOSTNAME : A human readbale format for the IP address of a host (Ex: webprod1 <=> 10.109.215.14) (Ex: We can give a linux system acting as a server a hostname instead of addressing it by IP all the time.) One word Host name: Short Hostname / Unqualified Hostname (Ex: webprod1) 2722 | - DNS: Maps IP address to the domain name (and vice versa) 2723 | 2724 | #### DNS Hostnames 2725 | 2726 | - FQDN => Fully Qualified Domain Names. (Ex: webprod1.mycompany.com) 2727 | - TLD => Top Level Domain (Ex: .com, .org, .net, ...) 2728 | 2729 | #### Domains 2730 | 2731 | **(To the LEFT of the TLDs (Below the TLDs in the tree) (Ex: 'mycompany' in mycompany.com)** 2732 | 2733 | Domains can be further sub-divided into: Sub-Domain => To the LEFT of the Domains (Below the Domains in the tree) (Ex: 'webprod1' in webprod1.mycompany.com) 2734 | 2735 | An advantage of using sub-domains: Identifying where our server is located: (Ex: webprod1.ny.us.mycompany.com) [NOTE: Sub-Domains need not correspond to geography, can be anything] 2736 | 2737 | #### Viewing the hostname 2738 | 2739 | - `hostname` 2740 | (or) 2741 | - `uname -n` 2742 | (or) 2743 | - `hostname -f` 2744 | 2745 | #### Setting the hostname 2746 | 2747 | - `hostname HOST_NAME` => sets the host name to specified argument(ex: `hostname webprod02`) 2748 | 2749 | To persist the change, (permanently set the hostname btw sessions): 2750 | 1. UBUNTU AND REDHAT SYSTEMS: 2751 | `echo 'webprod02' > /etc/hostname` (or, edit the file and put the hostname as a line) 2752 | (or) 2753 | 2. FOR EARLIER VERSIONS OF REDHAT: 2754 | Save the line 'HOSTNAME=webprod02' in '/etc/sysconfig/network' file 2755 | 2756 | #### Resolving DNS names 2757 | 2758 | Get IP from Hostname and Hostname from IP: 2759 | - `host HOSTNAME` => Displays the IP for the hostname (Ex: for the hostname 'www.mycompany.com') 2760 | - `host IPADDRESS` => Displays the Hostname for the IP (Ex: for the IP '11.2.255.143') 2761 | 2762 | #### The hosts file 2763 | 2764 | **`/etc/hosts`** 2765 | 2766 | Contains a list of IP addresses and Hostnames. We can add hosts as an entry to the file: 2767 | 2768 | Format: `ipaddress FQDN alias(es)` => Maps IP address to hostname (or hostnames) 2769 | 2770 | Ex: 2771 | - `10.11.12.13 webprod02.mycorp.com webprod02` 2772 | 2773 | Now, we can access the IP address using the specified Hostnames. 2774 | 2775 | Points: 2776 | - (THIS CAN BE USEFUL IF YOU WANT TO ACCESS COMPUTERS THAT DON'T HAVE DNS HOSTNAMES(for ex)) 2777 | - (HOSTNAMES IN THE '/etc/hosts' IS USED TO OVERRIDE THE DNS HOSTNAMES FOR THE SYSTEM - Ex. you can have a private network for a cluster of web servers that you own that only they and no one else can access - Create private IP addresses for each of the servers in the '/etc/hosts' file thus forcing each of the servers to go through the private network to communicate with each other.) 2778 | 2779 | Note: '/etc/hosts' file is LOCAL to your Linux System. It does NOT propagate to the Rest of the Network. 2780 | - `127.0.0.1 localhost` entry => Used by system as loopback address. 2781 | 2782 | NOTE:: The '/etc/hosts' file is checked first before the DNS is queried.(for search resolutions). We can change this lookup/search resolution order in the '/etc/nsswitch.conf' file.(controls the search order for resolutions) 2783 | - `hosts: files dns` => (If IP address is found in /etc/hosts, it is used. Search stops. Else, check DNS) 2784 | - `hosts: files nis dns` => (First check in files, then NIS, then DNS) 2785 | 2786 | ## DHCP static and dynamic addressing 2787 | 2788 | ### Ports 2789 | 2790 | Ports identify a service on a host (while IP identifies a host). 2791 | - 0 - 1023 are 'Well-Known'(system) Ports. 2792 | - Ex:Port No. 22 = SSH, 2793 | - 25 = SMTP, 2794 | - 80 = HTTP, 2795 | - 143 = IMAP, 2796 | - 389 = LDAP, 2797 | - 443 = HTTPS (Ex: https://www.mybank.com) 2798 | 2799 | It requires Superuser/Root privileges to open the Well Known Ports(0-1023). (Hence 'Privileged Ports') 2800 | 2801 | Ports above 1023(1024+) can be opened and used by normal users on the system(need not be root/superuser) (1024+ => Unprivileged Ports) 2802 | 2803 | Port Names: '/etc/services'. Maps port names to port numbers (Human readable port names) 2804 | Ex: 2805 | - `ssh 22/tcp # SSH Remote Login Protocol` 2806 | - `smtp 25/tcp # SMTP` 2807 | 2808 | Sometimes, when a third party service is installed, we can ADD a port number and name for the service it provides in the '/etc/services' file. (Therefore, we can also set port numbers for the custom applications/services that we write) 2809 | 2810 | ### DHCP 2811 | 2812 | PRIMARY USE: TO ASSIGN IP ADDRESSES TO HOSTS ON A NETWORK. 2813 | 2814 | Dynamic Host Control Protocol. When a DHCP (host) client wants an IP address to itself, it sends out a B/C msg looking for DHCP Servers to assign it an IP address. 'DHCP Servers' assign IP address to DHCP Clients. 2815 | 2816 | Format of Information provided by DHCP Server to Client: 2817 | 1. IP address 2818 | 2. netmask 2819 | 3. gateway 2820 | 4. DNS servers 2821 | 2822 | The DHCP client then configures itself with this information and communicates with others using the given IP. 2823 | 2824 | Each IP is 'leased' from the pool of IP addresses that the DHCP server manages.(The lease expiration time is configurable on the DHCP server. 1hr, 1day, 1Weeks. The client must renew the Ip address if it wantsto continue using it. Otherwise, the IP address is available to other DHCP clients for use.) 2825 | 2826 | #### Configuring a DHCP Client 2827 | 2828 | **For a RedHat Based System(RHEL)** 2829 | 2830 | To Edit a Red Hat based system as a DHCP Client, edit the configuration file located in: `/etc/sysconfig/network-scripts/ifcfg-DEVICE`. Ex: 2831 | - `/etc/sysconfig/network-scripts/ifcfg-eth0`, 2832 | - `/etc/sysconfig/network-scripts/ifcfg-enp5s2` 2833 | 2834 | To get a list of Network Devices on the system, run: 2835 | - `ifconfig -a` 2836 | (or) 2837 | - `ip link` 2838 | 2839 | Once you have identified the configuration file for the network device: Set the 'BOOTPROTO' variable to 'dhcp': 2840 | - `BOOTPROTO=dhcp` 2841 | 2842 | #### Configuring an Ubuntu based System 2843 | 2844 | Edit the '/etc/network/interfaces' file. Set a network device as a DHCP Client: Add line `iface NETWORK_DEVICE inet dhcp` 2845 | Ex: 2846 | - `iface eth0 inet dhcp` 2847 | 2848 | 1. Setting a STATIC IP address on REDHAT Based system(RHEL): 2849 | 2850 | Edit file: '/etc/sysconfig/network-scripts/ifcfg-NETWORKDEVICENAME'. Ex: 2851 | ``` 2852 | DEVICE=eth0 2853 | BOOTPROTO=static (This is a MUST!!) 2854 | IPADDR=10.109.155.174 (Assign the IP, NW and BC) 2855 | NETMASK=255.255.255.0 2856 | NETWORK=10.109.155.0 2857 | BROADCAST=10.109.155.255 2858 | GATEWAY=10.109.155.1 2859 | ONBOOT=yes (To set the Ip address on boot? yes) 2860 | ``` 2861 | 2862 | 2. Setting a STATIC IP address on UBUNTU Based system(RHEL): 2863 | 2864 | Edit file: '/etc/network/interfaces' Ex: 2865 | ``` 2866 | iface eth0 inet static (static keyword is a must!!) 2867 | address 10.109.155.174 2868 | netmask 255.255.255.0 2869 | gateway 10.109.155.1 2870 | ``` 2871 | 2872 | (OR) 2873 | 2874 | - MANUALLY assign an IP to a Network Device(interface): Use the `ip` command. 2875 | 2876 | Format: `ip address add IP/[NETMASK] dev NETWORK_DEVICE`. Ex: 2877 | - `ip address add 10.11.12.13 dev eth0`, 2878 | - `ip address add 10.11.12.13/255.255.255.0 dev eth0`. 2879 | 2880 | NOTE: 2881 | Bring the interface up(enabled with the given static ip): `ip link set NETWORK_DEVICE up` => Enables/sets up N/w Device with given IP (Ex: `ip link set eth0 up`) 2882 | 2883 | - Use the `ifconfig` command. 2884 | 2885 | Format: `ifconfig NETWORK_DEVICE IP_ADDRESS netmask SUBNET_MASK` 2886 | 2887 | Ex: 2888 | - `ifconfig eth0 10.11.12.13` 2889 | - `ifconfig eth0 10.11.12.13 netmask 255.255.255.0` 2890 | 2891 | NOTE: Bring the interface up(enabled with the given static ip): `ifonfig NETWORK_DEVICE up` => Enables/sets up N/w Device with given IP (Ex: `ifonfig eth0 up`) 2892 | 2893 | Alternatives to `ip` and `ifconfig`: 2894 | - `ifup` and `ifdown` => Quick way to bring a NW device up or down. It takes the network specs(IP, mask, etc) for the NW Device from the "configuration files" and enables/disables it. (/etc/sysconfig/... etc) 2895 | 2896 | Ex: 2897 | - `ifup NW_DEVICE` => brings up the network device (Ex: `ifup eth0`) 2898 | - `ifdown NW_DEVICE` => brings down the network device (Ex: `ifup enp5s02`) 2899 | 2900 | ### GUI or TUI Tools for networking 2901 | 2902 | - RedHat => 'nmtui', 'system-config-network' 2903 | - SUSE => 'YaST' 2904 | - Ubuntu => No official tool available. 2905 | 2906 | ## Network troubleshooting 2907 | 2908 | Some of the common tools for network diagnostics. Cannot rely on only one tool/ use many tool. 2909 | 2910 | ### Test connectivity to a host with ping 2911 | 2912 | **`ping`** 2913 | 2914 | Sends one or more ICMP packets to a host (Hostname (or) IP-ADDRESS) and waits for a reply 2915 | - `ping HOST` => Continuously pings the host until you stop program with `` (ex: `ping google.com`) 2916 | - `ping -c COUNT HOST` => Specifies the number of packets to send with ping (stops after sending these) (Ex: `ping -c 3 google.com`, `ping -c 3 10.1.244.101`, .. etc.) 2917 | 2918 | Ping returns the no of packets sent and Round Trip time(RTT) for each packet( '/' separated ) - In case of no replies from host: `100% packet loss` is displayed in output. 2919 | 2920 | Note: Ping also resolves the Hostname to IP address (If it cant => Unknown host error displayed - In that use IP address of system that you are trying to connect to.) 2921 | 2922 | NOTE: If ping does NOT receive a repsonse from destination host: 2923 | 1. Check if ping works to a local host in the network. If that also does NOT work then maybe there is a problem with OUR SYSTEM(OUR HOST) itself. Ex: Network cables got disconnected, NW drivers didn't get upgraded when Server System was upgraded, ... etc. 2924 | 2. If we can successfully ping a host within our local network: Then the problem lies outside of our network and definitely not on our host(our computer). If we can successfully ping other external hosts, the problem might be with one particular host that w pinged initially. (Ex: google.com ping fails but youtube.com and facebook.com pings are successful). POSSIBLE REASON: The destination host has a 'firewall' that has blocked/discarded icmp requests and responses. In this case, it will require other diagnostic tools other than 'ping'. 2925 | 2926 | 2927 | ### Testing connectivity over Hops 2928 | 2929 | **Hops => Routers** 2930 | 2931 | Use the `traceroute` command. (`ping` only gives you the end to end connectivity info) 2932 | - `traceroute` will require ROOT/SUPERUSER permissions. 2933 | 2934 | - `traceroute IP_ADDRESS` => Goes to DNS and resolves to name(TIME taking) 2935 | 2936 | - `traceroute -n HOST_NAME` => Skips the DNS server and directly to IP of host (Ex:`traceroute -n google.com`) 2937 | 2938 | Advantages: 2939 | - skips DNS - If issue was with DNS server then we will know. 2940 | - Faster. 2941 | 2942 | #### Output of traceroute 2943 | 2944 | Lists all the router IPs along the way(route) along witht the milliseconds it took for the packets to cross that network. Too much time? => Maybe problem is in that network. '*' for time => Either n/w not responding (or, router configured to not show traceroute - use other diagnostic tool) 2945 | 2946 | `traceroute` Produces one line of output per HOP. 2947 | 2948 | #### Alternative to traceroute 2949 | 2950 | **`tracepath`** 2951 | 2952 | Use `tracepath`. Does NOT require root/superuser permissions 2953 | 2954 | Ex: 2955 | - `tracepath google.com` (or) 2956 | - `tracepath -n google.com` => Produces one line of output for Each Response it receives.(unlike traceroute) 2957 | 2958 | ### The netstat command 2959 | 2960 | **`netstat`** 2961 | 2962 | Used to collect a wide variety of network information. 2963 | 2964 | Options: 2965 | - `-n` => Display numerical addresses and ports 2966 | - `-i` => Display list of network interfaces 2967 | - `-r` => Display the route table (Ex: `netstat -rn`) 2968 | - `-p` => Display PID and Program used [Needs root/superuser privileges] 2969 | - `-l` => Display listening sockets(ex:`netstat -nlp`){What servers(nginx,apache) are listeningto what ports} 2970 | - `-t` => Limit output to TCP (ex: `netstat -nltp`) 2971 | - `-u` => Limit output to UDP (ex: `netstat -nulp`) 2972 | 2973 | Ex: 2974 | - `netstat -i` 2975 | - `sudo netstat -nltp` 2976 | 2977 | ### Packet sniffing with tcpdump 2978 | 2979 | **`tcpdump`** 2980 | 2981 | `tcpdump` => Inspect contents of network packets to ensure payloads(data) are actually being delivered. (Requires root/superuser privileges) 2982 | 2983 | Options: 2984 | - `-n` => Display numerical addresses and ports (suppresses DNS queries as well) 2985 | - `-A` => Display ASCII(text) output. 2986 | - `-v` => Verbose mode. Produces more output 2987 | - `-vvv` => Even more verbose output. 2988 | 2989 | (`tcpdump` output: timestamp, nw id, source id, portnos, pkt spec info. etc) 2990 | 2991 | Ex: 2992 | - `sudo tcpdump` => Produces output for all the packets from/to the network devices assoc. with the system. 2993 | 2994 | ### The obsolete telnet command 2995 | 2996 | **`telnet`** 2997 | 2998 | It was originally intended to log onto 'remote systems' but is replaced with with better protocols such as SSH. 2999 | 3000 | 'telnet' can still be used in N/W TROUBLE SHOOTING. (May or may not be installed by default on linux systems - bcoz it is obsolete for connectin to systems) 3001 | 3002 | Usage of telnet: Initiate a TCP Connection to a host (or ip) by specifying the port. 3003 | 3004 | Format: `telnet HOST_OR_IP PORT_NO` 3005 | 3006 | Ex: Check if google.com is accepting requests at the HTTP port?: 3007 | - `telnet google.com 80` => If successfully connected - "Connected to google.com" or similar o/p. (If operation "timed out" - means connection could not be established - either port is not open on the host(firewall) [or] the connection to the host could not be made along the way/route) 3008 | 3009 | About `telnet`: 3010 | - Telnet command prompt: `telnet> ` 3011 | - To put a 'GET' request to, say, root directory: `GET /` 3012 | 3013 | Quit telnet: Press `quit` at the telnet prompt. (Output is: "closed") 3014 | 3015 | 3016 | ## Connecting via SSH to a Linux Virtual Machine 3017 | 3018 | **(Running on VirtualBox)** 3019 | 3020 | 1. Power Off virtual machine from VBOX. 3021 | 2. Goto Settings for that machine in VBOX. 3022 | 3. Change network setting to 'bridge adapter' (from NAT) and save. 3023 | 4. Power on Virtual Machine -> Open Terminal -> `ip addr` -> Get the ip address of the Network Interface Device (Other than local/loopback address) (= VM_IP_ADDRESS) 3024 | 5. `whoami` => to know the username on the VMachine (= VM_USERNAME) (Sometimes, we cannot connect using the root username so switch to another user and get his/her username) 3025 | 6. Open terminal on your Local Machine while your VMachine is running on VBOX. 3026 | 7. Type `ssh VM_USERNAME@VM_IP_ADDRESS` (Ex: `ssh adminuser@192.168.0.1`) 3027 | 8. You will prompted to accept the key(say 'yes') and type the password for that user on the virtual m/c/ 3028 | 9. You are logged into the VMachine via SSH! :) 3029 | 3030 | **THE END** 3031 | 3032 | 3033 | 3034 | 3035 | 3036 | 3037 | 3038 | 3039 | 3040 | 3041 | 3042 | 3043 | 3044 | 3045 | 3046 | 3047 | 3048 | 3049 | 3050 | --------------------------------------------------------------------------------