└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Linux 🐧 2 | 3 | #### What is Linux? 4 | - Linux is a free, open source operating system that enables the communication between computer hardware and software. The Linux OS was developed by Linus Torvalds in 1991. 5 | 6 | #### Why use Linux? 7 | - Open source 8 | - community Support 9 | - Support wide variety of hardware 10 | - Most servers runs on Linux 11 | - Secure compare to Windows 12 | 13 | #### Linux distributions 14 | There are many Linux distributions available in the market. It provides a different flavor of the Linux operating system to the users. Popular distros are : 15 | 16 | - Ubuntu. 17 | - Fedora. 18 | - Debian. 19 | - Linux Mintand so on. 20 | 21 | ## Linux File System 22 | Linux provides a dedicated set of file systems that stores every bit of data required for booting up the Linux systems. The file system holds the collection of data or files stored within the computer's hard disk or storage device. 23 | 24 | *Use "man heir" to find more insights on linux files system* 25 | 26 | ``` 27 | ls /etc/ 28 | ``` 29 | - This directory contains all the configuration file of your application. 30 | if something goes wrong you should be looking in this directory. 31 | 32 | ``` 33 | ls /bin 34 | ``` 35 | - This directory contains binaries for use by all users and also contains executable files, Linux commands that are used in single user mode, and common commands that are used by all the users, like cat, cp, cd, ls, etc 36 | 37 | ``` 38 | ls /sbin 39 | ``` 40 | - This directory contains binaries to configure the operating system and executable files. It only contains system binaries which require root privilege to perform certain tasks and are helpful for system maintenance purpose. e.g. fsck, root, init, ifconfig, etc 41 | 42 | ``` 43 | ls /lib 44 | ``` 45 | - This directory contains shared libraries which are often used by the ‘/bin’ and ‘/sbin’ directories. It also contains kernel module. These filenames are identable as ld* or lib*.so.* 46 | 47 | ``` 48 | ls /opt 49 | ``` 50 | - This directory's main purpose is to store optional application software packages. In many cases this is software from outside the distribution repository. Add-on applications from individual vendors should be installed in ‘/opt’. In some systems ‘/opt’ is empty as they may not have any add-on application. 51 | 52 | ``` 53 | ls /tmp 54 | ``` 55 | - This directory in Linux based systems contains necessary files that are temporarily required by the system as well as other software and applications running on the machine. 56 | 57 | For example, when you are writing a document, all the content inside that document is saved as a temporary file inside the /tmp directory. After you have saved it, it gets stored in your preferred location, and the temporary file gets removed once you exit the document. 58 | 59 | ``` 60 | ls /boot 61 | ``` 62 | - This directory contains contains the linux boot configuration files. This is one of the MOST important folder. Removing anything from this directory or a file getting corrupted will result in a OS crash after reboot. You system won't be able to boot without files in the /boot directory. 63 | 64 | ``` 65 | ls /dev 66 | ``` 67 | - This directory contain files that represent devices that are attached to the local system. However, these are not regular files that a user can read and write to; these files are called devices files or special files. 68 | *Device files are abstractions of standard devices that applications interact with via I/O system calls.* 69 | 70 | ``` 71 | ls /media 72 | ``` 73 | - The /media directory contains subdirectories where removable media devices inserted into the computer are mounted. For example, when you insert a CD into your Linux system, a directory will automatically be created inside the /media directory. You can access the contents of the CD inside this directory. 74 | 75 | ``` 76 | ls /mnt 77 | ``` 78 | - This directory and its subdirectories are intended for use as the temporary mount points for mounting storage devices, such as CDROMs, floppy disks and USB (universal serial bus) key drives. /mnt is a standard subdirectory of the root directory on Linux 79 | 80 | ``` 81 | ls /proc 82 | ``` 83 | - This directory is for each processes running on our system. It also contains some configuration files 84 | 85 | ``` 86 | ls /var 87 | ``` 88 | - This directory contains variable data files. This includes spool directories and files, administrative and logging data, and transient and temporary files. 89 | 90 | 91 | ### There are 7 types of files in Linux 92 | 93 | - Normal files 94 | - Directories 95 | - References to files 96 | - Character device files 97 | - Block device files 98 | - Symbolic links 99 | - Local domain sockets/named pipes) 100 | 101 | Find more info about files in the directory using below command 102 | ``` 103 | ls -l 104 | ``` 105 | Sort files based on timestamp 106 | ``` 107 | ls -lt 108 | ``` 109 | Sort files based on timestamp in decending order 110 | ``` 111 | ls -ltr 112 | ``` 113 | 114 | ## Monitoring in Linux 115 | 116 | Configured in /etc/monitrc 117 | 118 | #### Install Monit 119 | - apt-get update 120 | - apt-get install monit 121 | - systemctl enable monit 122 | - systemctl start monit 123 | 124 | Edit /etc/monit/monitrc 125 | 126 | #### systemd 127 | 128 | systemd is monitoring init system used to manage services 129 | ``` 130 | systemctl 131 | ``` 132 | 133 | - ## enable 134 | Make sure this unit always starts at boot. 135 | - ## disable 136 | Opposite of enable 137 | - ## start 138 | Start this unit now(will not automatically starts at next boot) 139 | - ## stop 140 | Stop a running unit(will not prevent starting at boot, if enabled) 141 | - ## reload 142 | Reread the program configuration files 143 | - ## restart 144 | Kill the process and start again, rereading the configuration files 145 | - ## status 146 | Check status of unit, show last few lines of log output 147 | 148 | ### Log mangaement commands 149 | 150 | - Journalctl 151 | - journactl -u 152 | - journactl --since "2 min ago" 153 | 154 | ## Linux Commands 155 | 156 | Find current working directory: 157 | ``` 158 | pwd 159 | ``` 160 | Find currently logged-in user: 161 | ``` 162 | whoami 163 | ``` 164 | List things in directory: 165 | ``` 166 | ls 167 | ``` 168 | Change directory: 169 | ``` 170 | cd /Dir_name 171 | ``` 172 | Clear the terminal: 173 | ``` 174 | clear 175 | ``` 176 | List hidden files: 177 | ``` 178 | ls -a 179 | ``` 180 | Create new file in terminal: 181 | ``` 182 | touch file.txt 183 | ``` 184 | Read file content: 185 | ``` 186 | cat file.txt 187 | ``` 188 | Create Directory: 189 | ``` 190 | mkdir directory_name 191 | ``` 192 | Create nested Directory using -p flag: 193 | ``` 194 | mkdir -p dir3/dir2/dir1/directory_name 195 | ``` 196 | Move File: 197 | ``` 198 | mv file_name directory 199 | mv file.txt Downloads/files 200 | ``` 201 | Remove File: 202 | ``` 203 | rm file_name 204 | ``` 205 | Remove Directory: 206 | ``` 207 | rm directory_name 208 | ``` 209 | To find use of specific command: 210 | ``` 211 | man command E.g. man rm 212 | ``` 213 | Remove directory and their contents recursively: 214 | ``` 215 | rm -r downloads/ 216 | ``` 217 | Copy file: 218 | ``` 219 | cp source destination 220 | ``` 221 | Switch to root user: 222 | ``` 223 | sudo -i 224 | ``` 225 | use w/who to find uptime, how many users has logged in: 226 | ``` 227 | w 228 | who 229 | ``` 230 | To find and monitor running processes: 231 | ``` 232 | top 233 | ``` 234 | Network- To find out what ports are open and listening: 235 | ``` 236 | sudo netstat -tupln 237 | ``` 238 | Create new file using VI editor: 239 | ``` 240 | vi test.file 241 | ``` 242 | For inserting data press i and write content: 243 | ``` 244 | i 245 | ``` 246 | To save and quit from vi press esc and then : 247 | ``` 248 | :wq 249 | ``` 250 | Print "Hello World" message in terminal : 251 | ``` 252 | echo "Hello World" 253 | ``` 254 | Print std Output to some file : 255 | ``` 256 | echo "Hello World" 1> somefile.txt 257 | ``` 258 | Print std Output to some file but this override the existing contents : 259 | ``` 260 | echo "Hello World" > somefile.txt 261 | ``` 262 | Redirecting standart output and append data in existing file use ">>" : 263 | ``` 264 | echo "Hello World" >> somefile.txt 265 | ``` 266 | To Redirect standard error use 2> : 267 | ``` 268 | cat nonExistingFile.txt 2> somefile.txt 269 | ``` 270 | Redirect input using <, Here we are mailing error.txt to the user palak: 271 | ``` 272 | mail -s This is errror file" palak < error.txt 273 | ``` 274 | Use 275 | for searching patterns in files 276 | ``` 277 | cat somefile.txt | grep Thanks 278 | ``` 279 | Cut file using delimiter and find fields 280 | ``` 281 | cat somefile.txt | cut -d: f1 282 | ``` 283 | Sort the file contents in alphabatical orders, ignore the leading whitespace using b case-insesitive f 284 | ``` 285 | cat somefile.txt | sort -bf 286 | ``` 287 | Search somethig in directory 288 | ``` 289 | grep search_term ./* 290 | ``` 291 | Install software 292 | ``` 293 | sudo apt-get install software-name 294 | ``` 295 | Used for searching apt packages on a Ubuntu or Debian based systems 296 | ``` 297 | sudo apt-cache search software-name 298 | ``` 299 | Change file mode - Read-4, Write-2, Execute-1, no-permissiom-0 300 | ``` 301 | chmod 777 testfile.txt - 302 | ``` 303 | Change file mode automatically- /etc/login.defs - search UMASK 304 | ``` 305 | edit umask 022 --- owner none permissomiom taken, from group read execute permission taken 306 | ``` 307 | Show first 10 lines of the file 308 | ``` 309 | head somefile.txt 310 | ``` 311 | Show last 10 lines of the file 312 | ``` 313 | tail somefile.txt 314 | ``` 315 | Find user details and user password 316 | ``` 317 | sudo -i 318 | tail /etc/passwd 319 | tail /etc/shadow 320 | ``` 321 | Add user and create set home directory 322 | ``` 323 | useradd -m -d /home/user1 -s /bin/bash user1 324 | cat /etc/shadow user1:!:19275:0:99999:7::: 325 | ``` 326 | User can't login as password is not set (you can see the ! mark ) 327 | 328 | Find which kernel version a system is currently running 329 | ``` 330 | uname -a 331 | ``` 332 | Find system's current IP address 333 | ``` 334 | ifconfig 335 | ip addr show 336 | ``` 337 | Check for free disk space 338 | ``` 339 | df -ah 340 | ``` 341 | Check for openports on linux machine 342 | ``` 343 | netstat 344 | netstat -tulpn 345 | ``` 346 | Check CPU usage for a process 347 | ``` 348 | ps aux | grep nginx 349 | top 350 | htop 351 | ``` 352 | #### Permissions 353 | 354 | - Read (r) - 4 355 | - Write (w) - 2 356 | - Execute (x) - 1 357 | 358 | drwxr-xr-x 359 | 360 | rwx - Root user permission 361 | r-x - Group permission 362 | r-x - perimission to others 363 | 364 | Check the permission of the directory 365 | ``` 366 | ls -ld 367 | ``` 368 | Assign all permissiom to root users in this directory 369 | ``` 370 | chmod 700 /opt/directory 371 | ``` 372 | 373 | The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. 374 | 375 | # Crontab 376 | 377 | The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. 378 | 379 | List crontab 380 | ``` 381 | crontab -l 382 | ``` 383 | Edit crontab 384 | ``` 385 | crontab -e 386 | ``` 387 | 388 | MIN HOUR DOM MOY DOW COMMAND 389 | 15 10 * * (3-5) ech0 "$(date): checkin in." >> /var/log/chcking 390 | 391 | sudo less /var/spool/cron/crontab/user 392 | 393 | ls /etc/cron.d/ 394 | 395 | crontab -e -u user 396 | 397 | sudo vi /etc/crotab === system wide crontab 398 | 399 | 400 | Group details 401 | ``` 402 | cat /etc/group 403 | ``` 404 | set password for user 405 | ``` 406 | passwd user1 407 | ``` 408 | Lock user 409 | List crontab 410 | ``` 411 | usermod -L user1 412 | crontab -l 413 | ``` 414 | 415 | To persist aliases, add alias in - 416 | ``` 417 | vi .bashrc 418 | alias ls -la = l 419 | ``` 420 | 421 | List open file 422 | ``` 423 | lsof 424 | ``` 425 | 426 | Find who opens this file at the moment 427 | ``` 428 | lsof /var/log/nginx/access.log 429 | ``` 430 | 431 | Find all the files open up by the process 432 | ``` 433 | ps aux | grep nginx 434 | ``` 435 | 436 | What file user has opened right now 437 | ``` 438 | lsof -u user_name 439 | ``` 440 | 441 | Which process is listening on what port 442 | ``` 443 | lsof -i :port_name 444 | ``` 445 | 446 | Which process is listening to tcp protocol 447 | ``` 448 | lsof -i tcp 449 | ``` 450 | 451 | ## Archiving and Compression in linux 452 | 453 | ## Archieve file 454 | 455 | ``` 456 | tar -zcvf doc.tar.gz Directory/ 457 | ``` 458 | 459 | #### Archiving and Compressing with tar 460 | 461 | - 'f' will specify the filename for the directory (docs.tar.gz in this case) 462 | 463 | - 'v' is verbose, means that we will get one line of output for each file we are compressing 464 | 465 | - 'c' is for create, we are creating a new archive 466 | 467 | - 'z' is for zipping (we are using the g zip program), used to compress archive as well as archiving it 468 | 469 | 470 | #### Unarchieve file 471 | 472 | ``` 473 | tar -zxf doc.tar.gz 474 | ``` 475 | 476 | #### Unzips a tar directory HERE (in this location) 477 | 478 | - 'z' shows that it is g zipped 479 | 480 | - 'x' means to extract it 481 | 482 | - 'f' defines the archive filename (in this case: docs.tar.gz) 483 | 484 | 485 | ### Linux Lifecycle & Processes 486 | 487 | - running 488 | - waiting or sleeping 489 | - stopped 490 | - zombie 491 | 492 | A process in a computer operating system is an executable program in action. The executable program has machine instructions necessary to carry out a specific task. A corresponding process is born when a program is executed for carrying out a task. A programmer writes a software program using a high-level programming language such as C. This is also called ‘code’ and the programmer compiles it to create an executable program. The compilation process converts the code into a set of machine-level instructions, and it becomes intelligible to the operating system. The compiler for Linux systems is GCC or Gnu C Compiler. 493 | 494 | -------------------------------------------------------------------------- 495 | 496 | The executable program remains a passive entity, until it is instructed to run or execute by the user. Then it creates a new entity called a process, which is visible by the command ‘ps’. The process is associated with three identifiers – the Process ID or PID, the Parent Process ID or PPID and the Group ID or GID. 497 | 498 | -------------------------------------------------------------------------- 499 | 500 | In a Linux system, the first process to start is the ‘init’ and it has a PID of 1. All subsequent processes are init’s children, grandchildren and so on. For active process in a Linux system, the command ‘pstree’ will bring up the entire hierarchy, while ‘top’ will show the dynamic view of processes. The Linux kernel uses a scheduler and it controls the execution sequence of all the processes. Linux processes can have one of four states at any given time: running, waiting or sleeping, stopped and zombie. 501 | 502 | -------------------------------------------------------------------------- 503 | 504 | A process achieves a running state when it is actually executing (running) or waiting for execution in the queue of the scheduler, which means it is ready to run or execute. For this reason, the running state is also known as runnable and is represented by R. 505 | 506 | -------------------------------------------------------------------------- 507 | 508 | A process is in a waiting or sleeping state if it must wait for an event to occur or some resource-specific operation needs to complete before the process can continue to run. Therefore, depending on the circumstances, the waiting state is further subcategorized into an interruptible or S state and an uninterruptible or D state. 509 | 510 | -------------------------------------------------------------------------- 511 | 512 | If the scheduler sends a stop signal to a process, the process goes into a stopped state. This might happen, for example, when the process is being debugged or analyzed and this state is represented by T. 513 | 514 | -------------------------------------------------------------------------- 515 | 516 | When a process has completed its execution, but is waiting to retrieve its exit state, the process is said to be in a zombie state, designated by Z. Once it crosses the zombie state or retrieves its exit status, the process dies or ceases to exist. 517 | 518 | --------------------------------------------------------------------------------