|
| 1 | +# Linux essentials |
| 2 | + |
| 3 | +## <span id="index"></span>Index |
| 4 | +* [Package management](#package) |
| 5 | + + [RPM based](#rpm) |
| 6 | + + [Debian based: APT and dpkg](#debian) |
| 7 | +* [Command-line basics](#cli-basics) |
| 8 | + + [Command-line syntax](#cli-syntax) |
| 9 | + + [Basic commands](#basic-commands) |
| 10 | + + [Command history and completion](#cmd-history) |
| 11 | + + [shell configuration files](#shell-config-files) |
| 12 | + + [Environment / shell variables](#variables) |
| 13 | + + [User-defined variables](#user-def-vars) |
| 14 | + + [Globbing](#globbing) |
| 15 | + + [Quoting](#quoting) |
| 16 | + + [Formatting commands](#formatting) |
| 17 | + + [Working with options](#options) |
| 18 | + + [Locate, find, whereis](#locate-find-whereis) |
| 19 | +* [Getting CLI help](#cli-help) |
| 20 | + + [Linux MAN pages](#man-pages) |
| 21 | + + [Info pages](#info-pages) |
| 22 | + + [More local documentation](#more-local-docs) |
| 23 | + |
| 24 | +## <span id="package"></span>Package management |
| 25 | + |
| 26 | +### <span id="rpm"></span>RPM based |
| 27 | + |
| 28 | +#### Yum |
| 29 | +YUM repos are located in `/etc/yum.repos.d/`. Unlike APT, YUM has several repo files in the folder. Check out the man pages for YUM and the other package management commands. |
| 30 | + |
| 31 | +| command | purpose | |
| 32 | +| --- | --- | |
| 33 | +| `yum update` | updates the repos and gives you the option of updating the packages pending updates | |
| 34 | +| `yum search httpd` | searches for that package | |
| 35 | +| `yum install $package` | to install it | |
| 36 | +| `yum check-update $package` | to see if a package needs any updates | |
| 37 | +| `yum upgrade` | upgrade package | |
| 38 | +| `yum deplist $package` | check package’s list of dependencies | |
| 39 | +| `yum clean packages` | will remove dependencies that were left behind but are no longer needed | |
| 40 | +| `yum remove $package` | removes the package | |
| 41 | +| `yum list installed` | list all installed packages | |
| 42 | + |
| 43 | +#### RPM |
| 44 | +| command | purpose | |
| 45 | +| --- | --- | |
| 46 | +| `rpm -ipv package.rpm` | `i` means install, `p` means show progress, and `v` means verbose | |
| 47 | +| `rpm -q nano` | query the package for info (true file name) | |
| 48 | +| `rpm -qi nano` | query the package for more info | |
| 49 | +| `rpm -e nano` | uninstall the package | |
| 50 | +| `rpm -qR nano` | uninstall required packages | |
| 51 | + |
| 52 | +### <span id="debian"></span>Debian based: APT and dpkg |
| 53 | + |
| 54 | +#### APT |
| 55 | +Uses a sources list located in `etc/apt/sources.list` |
| 56 | + |
| 57 | +| command | purpose | |
| 58 | +| --- | --- | |
| 59 | +| `apt-get update` | searches the online repos and caches the list of packages for when we do a search via... | |
| 60 | +| `apt-cache search $package` | searches for a package in the APT cache | |
| 61 | +| `apt-get install nginx` | install package | |
| 62 | +| `apt-get remove nginx` | remove package | |
| 63 | +| `apt-get remove --purge nginx` | to get rid of config files and such | |
| 64 | +| `apt-get autoremove [$package]` | to remove unneeded packages. | |
| 65 | +| `apt-get upgrade` | upgrades packages | |
| 66 | +| `apt-get -f upgrade` | Imstalls dependencies that we’re flagged while attempting to install a Debian package | |
| 67 | +| `apt-get dist-upgrade` | upgrades the kernel and distribution packages | |
| 68 | + |
| 69 | +#### dpkg |
| 70 | + |
| 71 | +| command | purpose | |
| 72 | +| --- | --- | |
| 73 | +| `dpkg -i name.deb` | Installs Debian package | |
| 74 | +| `dpkg --get-selections` | shows all installed packages | |
| 75 | +| `dpkg --remove $package_name` | Removes Debian package | |
| 76 | +| `dpkg --purge $package_name` | Removes dependencies | |
| 77 | + |
| 78 | +[Back to table of contents](#toc) |
| 79 | + |
| 80 | +## <span id="cli-basics"></span>Command-line basics |
| 81 | +Shells are command-line interpreters that accept commands that are then sent to the OS kernel for processing. See list of popular shells I saved as an img on my iPad. You can use any shell installed on the computer by typing its name on the CLI. |
| 82 | + |
| 83 | +- An interactive shell is one in which you issue commands. A non-interactive shell is one that runs in the background. |
| 84 | +- You can have several CLI sessions, accessible via `Alt + F1` through `Alt + F6`. |
| 85 | +- You can `echo $SHELL` |
| 86 | +- You can set each user’s shell and see it in /etc/password |
| 87 | +- If using bash, You can see your command-line history in .bash_history and complete commands with `tab` |
| 88 | + + `history` |
| 89 | + + `history 20` show the last 20 commands. |
| 90 | + + `!20` execute command # 20 |
| 91 | + + `!-2` execute the second-to-last command |
| 92 | + + `!!` execute the previous command |
| 93 | + + `!ssh` execute the last SSH command |
| 94 | + + `!?search?` execute the last command with "search" somewhere in it |
| 95 | + + `^original^replacement^` find latest command with `original` and replace that string with `replacement` on execution. |
| 96 | + * e.g.: `cat /etc/hots`, then `^hots^hosts^` |
| 97 | + |
| 98 | +### <span id="cli-syntax"></span>Command-line syntax |
| 99 | +Programs only run from inside folders indicated in the $PATH variable and not the working directory. |
| 100 | + |
| 101 | +- The letters after `-` are called flags. |
| 102 | +- `ls`: |
| 103 | + + `-a` = all (show hidden files) |
| 104 | + + `-l` = long listing (type and permissions, number of links the file has, owner, group, size in bytes, date modified) |
| 105 | + + `-F` = Display a slash after a directory, an asterisk after an executable, |
| 106 | + + `-S` = sort by file size, descending |
| 107 | + + `-r` = reverse the sorting to ascending. E.g., `ls -lrS` |
| 108 | +an @ after each symbolic link. |
| 109 | + + `-R` = recursively display contents of directories. |
| 110 | + + `-t` = sort by date modified, desc |
| 111 | + |
| 112 | +### <span id="basic-commands"></span>Basic commands |
| 113 | + |
| 114 | +| Command | Purpose | |
| 115 | +| :--- | :--- | |
| 116 | +| `cd` | change directory. By itself, takes you to your home directory. | |
| 117 | +| `env` | display current user’s environment variables | |
| 118 | +| `halt` or `init 0` | shutdown. Note that `init` works but is deprecated | |
| 119 | +| `ifconfig` or `ip addr` | shows NIC configs | |
| 120 | +| `netstat` | status of the network | |
| 121 | +| `reboot` or `init 1` | restart. Note that `init` works but is deprecated | |
| 122 | +| `route` | view routing table | |
| 123 | +| `shutdown` | `-H`= halt; `-P`= poweroff; `c` = cancel pending shutdown; `r` = reboot | |
| 124 | +| `su` | substitute user or super user. E.g., `su josue` or `su -` to become root | |
| 125 | +| `top` | list of running apps/processes; `top -h` gives usage info | |
| 126 | +| `uname` | print OS name. `-n` = hostname; `-r` = kernel’s release; `-v` = kernel’s version number; `-m` 32- or 64-bit; `-p` = processor info; `-o` = full official name of the OS; `-a` = all info above | |
| 127 | +| `which $program` | full path of the application | |
| 128 | +| `whoami` | current user | |
| 129 | + |
| 130 | +### <span id="cmd-history"></span>Command history and completion |
| 131 | +- A user's command history is kept in **~/.bash_history** |
| 132 | +- The `$HISTFILESIZE` env variable shows how many lines will be saved in the history file. a value of 0 means save nothing. |
| 133 | +- The `$HISTCONTROL` env variable shows Bash's history behavior |
| 134 | +- `history` shows a numbered list of the commands. Rerun a command with `!<num>` |
| 135 | +- You can use the `tab` key to complete a partially-typed command |
| 136 | + |
| 137 | +### <span id="shell-config-files"></span>shell configuration files |
| 138 | +Different shell use different configuration files. Make sure you know which files your Linux distro uses. A system without a GUI puts you in the login shell. It’s important to know which shell your in so you know which configuration file will be used for it. |
| 139 | + |
| 140 | +- login shell: Shell you're presented with when you log in remotely (e.g., SSH) |
| 141 | + + First file executed is `/etc/profile`. This file sets default variables for all users. |
| 142 | + + Then these files are executed in order. Once there's a match the others are ignored even if they exist: |
| 143 | + * `~/.bash_profile` |
| 144 | + * `~/.bash_login` |
| 145 | + * `~/.profile` |
| 146 | + * `~/.bashrc` |
| 147 | + * `/etc/bashrc` |
| 148 | + + `~/.bash_logout` is executed when the user logs out |
| 149 | +- non-login shell: Shell you're presented with when you use the terminal application or when you run a script |
| 150 | + + Executes `~/.bashrc`, which calls `/etc/bashrc` |
| 151 | +- bash_profile: Login shell that stores user-specific shell preferences |
| 152 | +- bashrc: Non-login shell that stores user-specific functions and aliases |
| 153 | +- /etc/profile: (check exact name) affects all users |
| 154 | + |
| 155 | +### <span id="variables"></span>Environment / shell variables |
| 156 | +- Variables are placeholders for another value. They can be used in scripts. |
| 157 | +- User defined variables: Created by the user |
| 158 | +- Environment variables: Created by the OS to configure the system environment. E.g., `echo $HOME` |
| 159 | +- You can view all environment variables with `env` (not alphabetized) and `set` (alphabetized) |
| 160 | +- Change or create a variable (note there is no space on either side of equal sign: |
| 161 | + + via `VAR=VALUE`. Example, `PATH=$PATH:/var/opt/`. |
| 162 | + + `export $PATH` to make that new value available to users in other shells |
| 163 | + + Configure your bash config files to make this happen every time you start your system. |
| 164 | + |
| 165 | +#### Common environment variables |
| 166 | + |
| 167 | +| variable | description | |
| 168 | +| --- | --- | |
| 169 | +| LOGNAME | username of current user | |
| 170 | +| OLDPWD | previous working directory | |
| 171 | +| OSTYPE | duh | |
| 172 | +| PATH | distro dependent | |
| 173 | +| USER and USERNAME | username of current user | |
| 174 | +| HOST and HOSTNAME | system hostname | |
| 175 | +| ENV | you can type `env` or `set` | |
| 176 | +| EUID | UID number of current user | |
| 177 | +| HISTFILE | full path of file | |
| 178 | +| HISTSIZE | size history can grow to | |
| 179 | + |
| 180 | +### <span id="user-def-vars"></span>User-defined variables |
| 181 | +- Variables cannot start with a number. They can contain `-` amd `_` |
| 182 | +- Convention is to make variables upper-case. |
| 183 | +- Example: `THEDUDE="Jeff Bridges" ; export THEDUDE` |
| 184 | + |
| 185 | +### <span id="globbing"></span>Globbing |
| 186 | +Globbing is the process of using wildcards to expand a search. Globbing stands for **global command**. |
| 187 | + |
| 188 | +- `*` = match __0+__ of any character |
| 189 | +- `?` = match __1__ of any character |
| 190 | +- `[Aabc]` = match any single character in list |
| 191 | +- `[^abc]` = exclude characters in list |
| 192 | +- Examples: |
| 193 | + + `ls -l ????.txt` search for a four-character text file |
| 194 | + + `ls -l [F]*.txt` search for all text files beginning with capital F |
| 195 | + + `ls -l f[igh][lfz]e*.txt` what you'd expect from regex, except that `*` matches anything 0+ times |
| 196 | + + `ls -l [Rr]eport201[0-9]` |
| 197 | + |
| 198 | +### <span id="quoting"></span>Quoting |
| 199 | + |
| 200 | +| Character | Description | Example | |
| 201 | +| :--- | :--- | :--- | |
| 202 | +| `"` | allows variable interpolation. | `echo "The path is $PATH"` | |
| 203 | +| `'` | __does not__ allow variable interpolation | `echo 'The path is $PATH'` | |
| 204 | +| `\` | Escapes special chars | `echo "You owe \$5.00"` | |
| 205 | + |
| 206 | +### <span id="formatting"></span>Formatting commands |
| 207 | +- Commands tend to be lower-case |
| 208 | +- Spacing doesn't matter (2 spaces or a tab is OK) |
| 209 | +- You can wrap long commands along several lines but will need to escape it. E.g., `ls \{enter key} -lah` |
| 210 | + |
| 211 | +### <span id="options"></span>Working with options |
| 212 | +- Command = what to do; options = how to do it; arguments = what to do it with. |
| 213 | +- Parameters with a leading `-` are called __options__ and switch certain parts of the command on/off. `ls -la` = `ls -l -a` |
| 214 | +- Paramters with no leading `-` are called arguments. |
| 215 | + |
| 216 | +### <span id="locate-find-whereis"></span>Locate, find, whereis |
| 217 | + |
| 218 | +#### Locate |
| 219 | +Searches its file database for files or directories the user has access to. Faster than `find` but doesn't allow you to indicate the directory. |
| 220 | + |
| 221 | + locate passwd |
| 222 | + |
| 223 | +#### Find |
| 224 | +- Syntax is `find $dir [$dir2] {-name | -iname | -size | -mtime | -atime | -ctime}` |
| 225 | +- You can glob using `*`, `?`, and `[]`. |
| 226 | + - __If you glob, you must use single quotes.__ |
| 227 | +- This command will search the directory recursively. |
| 228 | + + You can exclude a directory with `-or -iname "$dir_name" prune` |
| 229 | + |
| 230 | +Examples: |
| 231 | + |
| 232 | + find . -iname '*keyword*' # Match keyword |
| 233 | + find / -size +1024 # greater than size in bytes |
| 234 | + find . -mtime -1 # modified time less than 1 day |
| 235 | + find . -atime -1 # accessed time less than 1 day |
| 236 | + find . -ctime -1 # created time less than 1 day |
| 237 | + find . -iname '*.txt' -or -iname "implementations" -prune |
| 238 | + |
| 239 | +#### whereis |
| 240 | +Searches for executables and man page files |
| 241 | + |
| 242 | + whereis cd |
| 243 | + |
| 244 | +[Back to table of contents](#toc) |
| 245 | + |
| 246 | +## <span id="cli-help"></span>Getting CLI help |
| 247 | + |
| 248 | +### <span id="man-pages"></span>Linux MAN pages |
| 249 | +- Meant as a quick reference for people who already know a command and need to learn certain options. Not meant to be a tutorial. |
| 250 | +- Quality can vary significantly from one page to the next |
| 251 | +- Man pages have 9 sections. You'll mostly use section 1, executable programs and shell commands. To see section 5, for example, type `man 5 $command` |
| 252 | +- Use `whatis $command` to search for man page entries matching that command. E.g., `whatis passwd`. |
| 253 | +- `apropos $keyword` search man pages for entries containing the keyword. |
| 254 | +- You can use `/` inside a man page to search forward or `?` to search backwards |
| 255 | +- Man pages are organized like this: |
| 256 | + + Name |
| 257 | + + Synopsis: Brief description of how the command is used, incl. optional parameters (in brackets) and required paramters (underlined). `...` means multiple parameters of that type. E.g., `ls -la file1 file2` |
| 258 | + + Description |
| 259 | + + Options |
| 260 | + + Files |
| 261 | + + See Also |
| 262 | + + Bugs |
| 263 | + + History |
| 264 | + + Author |
| 265 | + |
| 266 | +### <span id="info-pages"></span>Info pages |
| 267 | +- Similar to man pages, but has hyperlinks (denoted by asterisk. |
| 268 | +- Programs from the Free Software Foundation use info pages instead of man pages. |
| 269 | +- `info $topic` |
| 270 | + |
| 271 | +### <span id="more-local-docs"></span>More local documentation |
| 272 | +- other ways to get help include an application's readme file. |
| 273 | +- Locations for readme files include: |
| 274 | + + `/usr/doc/packagename` |
| 275 | + + `/usr/share/doc/packagename` |
| 276 | + + `/usr/share/doc/packages/packagename` |
| 277 | +- Many programs have help files in PostScript, PDF, or HTML format. |
| 278 | +- Configuration files are typically in the `/etc` directory. |
| 279 | +- For RPM packages, try `rpm -ql passwd | grep doc` or `rpm -ql yum | grep README` |
| 280 | + |
| 281 | +#### Reading different file formats |
| 282 | + |
| 283 | +| file ext | program used to read them | |
| 284 | +| :--- | :--- | |
| 285 | +| `.1` - `.9` | man, info, less | |
| 286 | +| `.gz` or `.bz2` | gunzip or bunzip2 to decompress, then less to read | |
| 287 | +| `.txt` | any text editor | |
| 288 | +| `.htm`, `.html` | any web browser, often less | |
| 289 | +| `.odt` | LibreOffice, OpenOffice.org, any word processor | |
| 290 | +| `.pdf` | `.xpdf`, Adobe Reader | |
| 291 | +| `.tif`, `.png`., `.jpg` | Gimp |
0 commit comments