└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # OS LAB cheat sheet 2 | 3 | ### Table of Contents 4 | | Section | Topic | 5 | |---- | :---------: 6 | | [**Lesson 1**](#lesson-1) | basic terminal commands | 7 | | [**Lesson 2**](#lesson-2) | file permissions & groups | 8 | | [**Lesson 3**](#lesson-3) | ps command | 9 | | [**Lesson 4**](#lesson-4) | kill & ping | 10 | | [**Lesson 5**](#lesson-5) | foreground and background | 11 | | [**Research 1**](#research-1) | NI vs PRI & daemon | 12 | | [**Lesson 6**](#lesson-6) | nice and renice | 13 | 14 | ------------------------------------------------------------------- 15 | 16 | ### Lesson 1 17 | 1. **ls** - list directory contents \ 18 | ls [OPTION]... [FILE]... \ 19 | List information about the FILEs (the current directory by default). 20 | 1. **ls**: displays files in a bare format 21 | ```sh 22 | $ ls 23 | README.md 24 | ``` 25 | 2. **ls -l**: long format, displaying Unix file types, permissions, number of hard links, owner, group, size, last-modified date and filename 26 | ```sh 27 | $ ls -l 28 | total 0 29 | -rw-r--r-- 1 hassan hassan 0 Aug 13 23:55 README.md 30 | ``` 31 | 2. **pwd** - print name of current/working directory \ 32 | pwd [OPTION]... \ 33 | Print the full filename of the current working directory. 34 | ```sh 35 | $ pwd 36 | /home/hassan/os-lab-cheatsheet 37 | ``` 38 | 3. **cd** - change directory \ 39 | cd DIRECTORY... \ 40 | Change the current working directory to given directory (home directory by default). 41 | 4. **mkdir** - make directories \ 42 | mkdir [OPTION]... DIRECTORY... \ 43 | Create the DIRECTORY(ies), if they do not already exist. 44 | ```sh 45 | $ mkdir test 46 | $ ls 47 | README.md test 48 | ``` 49 | 5. **rm** - remove files or directories \ 50 | rm [OPTION]... [FILE]... \ 51 | rm removes each specified file. By default, it does not remove directories. 52 | 1. **rm -r**: remove directories and their contents recursively 53 | 2. **rm -f**: ignore nonexistent files and arguments, never prompt 54 | 3. **rm -rf**: remove directories and their contents recursively, by force 55 | 6. **touch** - change file timestamps \ 56 | touch [OPTION]... FILE... 57 | 1. **touch**: a FILE argument that does not exist is created empty 58 | ```sh 59 | $ touch file.txt 60 | ``` 61 | 7. **cp** - copy files and directories \ 62 | cp [OPTION]... [-T] SOURCE DEST \ 63 | Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. 64 | ```sh 65 | $ cp README.md test 66 | ``` 67 | ##### Note: 68 | 1. You should use `sudo` before your commad when you want to run the command under the root permission. Also switch to root user with `sudo -i`. Use `exit` or `CTRL+D` to exit from root shell. 69 | 2. In terminal, `.` means current working directory and `..` means parent of current directory. 70 | ```sh 71 | $ pwd 72 | /home/hassan/os-lab-cheatsheet 73 | $ cd ../.. 74 | $ pwd 75 | /home 76 | ``` 77 | ### Lesson 2 78 | 1. File permissions in Linux/Unix: \ 79 | The first set of three permissions, after the place for d, applies to the **owner** of the file \ 80 | The second set of three permissions applies to the all users who are members of the **group** of the file \ 81 | The third set of three permissions applies to **others** \ 82 | ![alt tag](https://helpdeskgeek.com/wp-content/pictures/2017/02/file-permissions-explanation.png.webp) 83 | 1. **Read**: This permission give you the authority to open and read a file. Read permission on a directory gives you the ability to lists its content. 84 | 2. **Write**: The write permission gives you the authority to modify the contents of a file. The write permission on a directory gives you the authority to add, remove and rename files stored in the directory. 85 | 3. **Execute**: In Unix/Linux, you cannot run a program unless the execute permission is set. If the execute permission is not set, you might still be able to see/modify the program code(provided read & write permissions are set), but not run it. 86 | 2. Number of each permission: 87 | | Number | Permission Type | Symbol | 88 | |:-------------:|:-------------:|:-----:| 89 | | 0 | No Permission | --- | 90 | | 1 | Execute | --x | 91 | | 2 | Write | -w- | 92 | | 3 | Execute + Write | -wx | 93 | | 4 | Read | r-- | 94 | | 5 | Read + Execute | r-x | 95 | | 6 | Read + Write | rw- | 96 | | 7 | Read + Write + Execute | rwx | 97 | 98 | 3. **chmod** - change file mode bits \ 99 | chmod [OPTION]... MODE[,MODE]... FILE... \ 100 | chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits. 101 | ```sh 102 | $ chmod 755 README.md 103 | ``` 104 | ![alt tag](https://files.virgool.io/upload/users/57785/posts/ci1wifitikyz/emwv6xwxrd74.png) 105 | 4. **groupmod** - modify a group definition on the system \ 106 | groupmod [options] GROUP \ 107 | The groupmod command modifies the definition of the specified GROUP by modifying the appropriate entry in the group database. 108 | ```sh 109 | $ groupadd new_group 110 | ``` 111 | 5. **groups** - print the groups a user is in \ 112 | groups [OPTION]... [USERNAME]... \ 113 | Print group memberships for each USERNAME or, if no USERNAME is specified, for the current process (which may differ if the groups database has changed). 114 | ```sh 115 | $ groups hassan 116 | hassan : hassan adm cdrom sudo dip plugdev lpadmin sambashare 117 | ``` 118 | 6. **useradd** - create a new user or update default new user information \ 119 | useradd [options] LOGIN 120 | useradd is a low level utility for adding users. On Debian, administrators should usually use adduser(8) instead. 121 | 7. **adduser**, **addgroup** - add a user or group to the system \ 122 | adduser [options] user group \ 123 | adduser and addgroup add users and groups to the system according to command line options and configuration information in /etc/adduser.conf. They are friendlier front ends to the low level tools like **useradd**, **groupadd** and **usermod** programs. 124 | ```sh 125 | $ adduser new_user 126 | ``` 127 | 7. **usermod** - modify a user account \ 128 | usermod [options] LOGIN \ 129 | The usermod command modifies the system account files to reflect the changes that are specified on the command line. 130 | 1. **usermod -G GROUP1[,GROUP2,...[,GROUPN]]]**: new list of supplementary GROUPS 131 | 2. **usermod -a USER**: add the USER to the supplementary group(s). Use only with the -G option 132 | ```sh 133 | $ usermod -G new_group -a new_user 134 | ``` 135 | 136 | ### Lesson 3 137 | 1. **ps** - report a snapshot of the current processes. \ 138 | ps [options] \ 139 | ps displays information about a selection of the active processes. 140 | ```sh 141 | $ ps 142 | PID TTY TIME CMD 143 | 18920 pts/3 00:00:00 zsh 144 | 18968 pts/3 00:00:00 ps 145 | ``` 146 | 1. **ps -l**: long format 147 | ```sh 148 | F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 149 | 0 S 1000 19749 19738 0 80 0 - 6679 sigsus pts/2 00:00:00 zsh 150 | 4 R 1000 19962 19749 0 80 0 - 5709 - pts/2 00:00:00 ps 151 | ``` 152 | 2. **ps -f**: full-format, including command lines 153 | ```sh 154 | $ ps -f 155 | UID PID PPID C STIME TTY TIME CMD 156 | hassan 18920 18534 7 16:04 pts/3 00:00:00 zsh 157 | hassan 18956 18920 0 16:04 pts/3 00:00:00 ps -f 158 | ``` 159 | 3. **ps -t**: all processes on this terminal 160 | ```sh 161 | $ ps -t 162 | PID TTY STAT TIME COMMAND 163 | 18920 pts/3 Ss 0:00 zsh 164 | 19193 pts/3 R+ 0:00 ps -t 165 | ``` 166 | 4. **ps -r**: only running processes 167 | ```sh 168 | $ ps -r 169 | PID TTY STAT TIME COMMAND 170 | 19245 pts/3 R+ 0:00 ps -r 171 | ``` 172 | 5. **ps -e**: all processes 173 | 6. **ps aux**: \ 174 | a = show processes for all users \ 175 | u = display the process's user/owner \ 176 | x = also show processes not attached to a terminal \ 177 | use **grep** for searching from command output 178 | ```sh 179 | $ ps aux | grep zoom 180 | hassan 13232 33.9 3.9 5151328 484560 tty2 SLl+ 14:41 43:40 /opt/zoom/zoom 181 | hassan 22290 0.0 0.0 20476 2900 pts/3 S+ 16:50 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn zoom 182 | ``` 183 | 184 | ### Lesson 4 185 | 1. **kill** - send a signal to a process \ 186 | kill [options] [...] \ 187 | The default signal for kill is TERM. Use -l or -L to list available signals. 188 | ```sh 189 | $ kill -l 190 | HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS 191 | ``` 192 | 1. **kill PID**: terminate a process with PID 193 | ```sh 194 | $ kill 13232 195 | ``` 196 | 2. **killall PNAME**: terminate all processes with PNAME 197 | ```sh 198 | $ killall zoom 199 | ``` 200 | 2. **xkill** - kill a client by its X resource \ 201 | xkill is a utility used for force-quitting GUI apps. It is handy when some app isn't responding or is causing your system to work abnormally. 202 | 3. **pstree** - display a tree of processes \ 203 | pstree shows running processes as a tree. 204 | 4. **ping** - send ICMP ECHO_REQUEST to network hosts \ 205 | ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway. 206 | ```sh 207 | $ ping api.telegram.org 208 | PING api.telegram.org (149.154.167.220) 56(84) bytes of data. 209 | 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=1 ttl=50 time=12.9 ms 210 | 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=2 ttl=50 time=13.6 ms 211 | 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=3 ttl=50 time=13.1 ms 212 | 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=4 ttl=50 time=12.10 ms 213 | 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=5 ttl=50 time=13.1 ms 214 | 64 bytes from 149.154.167.220 (149.154.167.220): icmp_seq=6 ttl=50 time=12.10 ms 215 | ``` 216 | 217 | ### Lesson 5 218 | 1. **Foreground** and **Background** Processes: \ 219 | Foreground processes refer to applications you are running that you are currently interacting with, and which applies equally to graphical user interfaces as it does to the command line. Background processes refer to applications that are running but not being interacted with by the user. 220 | 2. **nano** - Nano's ANOther editor, an enhanced free Pico clone \ 221 | nano is a small and friendly editor. 222 | 1. **nano FILE**: open FILE with nano (create the FILE if is not exists) 223 | ```sh 224 | $ nano script.sh 225 | ``` 226 | 3. **cat** - concatenate files and print on the standard output \ 227 | cat [OPTION]... [FILE]... \ 228 | Concatenate FILE(s) to standard output. 229 | ```sh 230 | $ cat script.sh 231 | ping google.com > res.out 232 | ``` 233 | 4. **jobs** - display the status of jobs in the current shell 234 | ```sh 235 | $ jobs 236 | [1] + suspended ./script.sh 237 | ``` 238 | 5. **bg** - move jobs to the background 239 | 1. **bg %jobID** 240 | ```sh 241 | $ bg %1 242 | [1] + 28975 continued ./script.sh 243 | ``` 244 | 6. **fg** - move jobs to the foreground 245 | 1. **fg %jobID** 246 | ```sh 247 | $ fg %1 248 | [1] + 28975 running ./script.sh 249 | ``` 250 | #### Note: 251 | 1. You can suspend a job with `CTRL+Z` 252 | 2. You can end a job with `CTRL+C` 253 | 254 | ### Research 1 255 | - **Nice Value** and **Priority** \ 256 | PRI is the priority level. The lower the PRI, the higher the priority of the process will be. \ 257 | PRI is calculated as follows: 258 | - for normal processes: `PRI = 20 + NI` (NI is nice and ranges from -20 to 19) 259 | - for real time processes: `PRI = - 1 - real_time_priority` (real_time_priority ranges from 1 to 99) \ 260 | The nice value is a "global" mechanism, whereas priority is relevant for the task switcher _right now_. 261 | - **Daemon Process** \ 262 | A daemon process is a background process that is not under the direct control of the user. This process is usually started when the system is bootstrapped and it terminated with the system shut down. \ 263 | In Unix, the names of daemons conventionally end with the letter _d_, for clarification that the process is in fact a daemon, and for differentiation between a daemon and a normal program. 264 | 265 | ### Lesson 6 266 | 1. **top** - display Linux processes \ 267 | The top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel. \ 268 | Use `q` or `CTRL+C` to exit from top. 269 | 2. **nice** - run a program with modified scheduling priority \ 270 | nice [OPTION] [COMMAND [ARG]...] 271 | 1. **nice -n**: add integer N to the niceness (default 10) 272 | ```sh 273 | $ nice -n 15 nautilus 274 | ``` 275 | #### Note: You should have root permission for negative nice values. 276 | ```sh 277 | $ sudo nice -n -8 nautilus 278 | ``` 279 | 280 | 3. **renice** - alter priority of running processes \ 281 | renice [-n] priority [-g|-p|-u] identifier... \ 282 | renice alters the scheduling priority of one or more running processes. The first argument is the priority value to be used. The other arguments are interpreted as process IDs (by default), process group IDs, user IDs, or user names. 283 | ```sh 284 | $ renice 2 14862 285 | 14862 (process ID) old priority 0, new priority 2 286 | ``` 287 | 288 | 289 | --------------------------------------------------------------------------------