========= Linux OS ========= => Where we will use linux os in real-time => Jenkis Server => Docker Server => K8S cluster => SonarQube server => Nexus Server => ELK stack => Grafana & Promethues servers => Ansible server ============= What is OS ? ============= => It is a software which acts as mediator between user and computer. => Users will communicate with computers using OS => Without OS we can't use any computer => OS provides platform to run our applications in computer. Ex : Notepad, calculator, browser... => We have several operating systems in market Ex: Windows, Linux, Mac, Android, IOS etc.. =========== Windows OS =========== => Developed by Microsoft company (Bill Gates) => Windows os is licensed (commercial) => Windows is single user based os => Security features are less in windows os (anti virus s/w required) => Windows is GUI based (graphical user interface) => Windows is recommended for personal use Ex: watch movies, play games, online classes, coding.... ========== Linux OS ========== => Linux is community based os (not specific to any company) => Linux is free and open source os => Linux is Multi User based OS => Linux is highly secured (anti virus is not required) => Linux supports both GUI and CLI => Linux is highly recommended for business use (servers management) Ex: App Servers, DB servers, jenkins, docker, k8s, nexus.... ============== Linux History ============== => Linux OS developed by "Linus Torvalds" -> Linus Torvalds identified some challenges/issues in Unix OS -> Linus Torvalds identified one OS which is matching with his ideas i.e Minux OS -> Linus Torvalds used Minux OS code and made some changes and released into market as new OS i.e Linux OS. (Li) nus + Mi (nux) = Linux ====================== Linux Distributions ====================== -> Linus Torvalds provided Linux OS source code for free of cost -> So many companies downloaded Linux OS source code and modified according to their requirement and released into market with their brand names. Those are called as Linux Distributions/ Linux Flavours. -> We have 200+ Linux Distributions in the market. Ex: Amazon Linux, Ubuntu, Red HAT, Cent OS, suse, debian, kali, fedora.... =========================================== How to setup Linux Machine For Practice ? =========================================== Approach-1 : Download and Install Linux OS in our System (Physical machine) Approach-2 : Use Virtual Box and install Linux os as guest os in windows machine Approach-3 : Setup Linux VM in AWS Cloud for free of cost ( t2.micro / t3.micro -> 1 year free & monthly 750 hours you can use ) => To connect with Linux VM from windows machine we can use SSH Client softwares 1) Git Bash : Only CLI support 2) PuTTy : Only CLI support 3) MobaXterm : Supports both GUI and CLI 4) WinSCP : Only GUI support --------------------------------------------------------------------------------- *AWS Account Setup* : https://youtu.be/xi-JDeceLeI?si=4MgBX_H4_NPuzdT8 *Linux VM Setup in AWS and Connect by Using Git bash* : https://www.youtube.com/watch?v=JMlQaTXvw5o *Connect To Linux VM with MobaXterm* : https://youtu.be/uI2iDk8iTps?si=ZuZs0lQTxoRpbRMk *Connect To Linux VM with Putty* : https://youtu.be/GXc_bxmP0AA?si=HgSydrP89mPxv23s ---------------------------------------------------------------------------------- =================== Linux Architecture =================== 1) Hardware components : cpu, ram, io 2) Kernel : heart of linux os - Will recieve instructions from shell - convert commands into hardware understandable format - It is mediator between shell and hardware 3) Shell : mediator between users and kernel - It will read commands executed by users - Validate command syntax - convert command into kernel understandable format - shell will send instructions to kernel for execution user ----> linux commands ---> shell -------> kernel ----> linux hardware =================== Linux File System =================== => In Linux OS everything will be represented as File only. => Root Directory (/) is starting point of linux machine => Inside root directory we have several directories like below /home : It contains linux machine user's home directories ashok : /home/ashok/ john : /home/john/ ec2-user : /home/ec2-user /bin : Essentials ready-to-run-program (binaries) /boot : static files of boot loader (to start linux os) /dev : device files (storage devices) /lib : shared libraries /etc : OS related configuration files will be available here /opt : Optional application software packages /usr : user utilities and applications /tmp : Temporary files will be stored here ================= Linux Commands ================= whoami : Display logged-in username pwd : Display present working directory date : Display current date cal : Display current month calendar cal 2030 : Display 2030 calendar cd : change directory cd .. => go one step back from pwd cd => Go inside directory mkdir : Make directory (create folder) mkdir linux mkdir aws mkdir devops azure gcp rmdir : remove empty directory (delete) rmdir devops rm -rf : remove non-empty directory (recursively & forcefully) rm -rf devops ls : display present working directory content ls -l : long list the files in alphabetical order ls -lr : display files in reverse of alphabetical order ls -lt : display latest files on top ls -ltr : display old files on top ls -la : display hidden files touch : To create empty files in linux touch f1.txt touch f2.txt f3.txt mv : For rename and move files & directories mv existing-name new-name mv presention-location new-location cat : create new file with data + append data to file + print file data # create new file with data cat > f1.txt # append data to existing file cat >> f1.txt # print file data from top to bottom cat f1.txt # print file data with line numbers cat -n f1.txt tac : To print file data from bottom to top tac f1.txt cp : copy data from one file to another file cp f1.txt f2.txt Note: If we want to copy data from multiple files then we should use cat command cat f1.txt f2.txt > f3.txt ==================================== Working with Text Filters in Linux ==================================== head : print first 10 lines of the file head f1.txt head -n 15 f1.txt head -n 100 f1.txt tail : print last 10 lines of the file tail f1.txt tail -n 20 f1.txt tail -n 100 f1.txt grep : grep stands for global regular expression print Note: Using grep command we can search for content in the file grep 'people' data.txt grep -n 'people' data.txt grep -i 'LIKE' data.txt grep -v 'winter' data.txt tail data.txt | grep 'like' ======================= Text Editors in Linux ======================= => vi (visual editor) it is default editor in linux machines. => Using 'vi' we can create new files and we can modify existing file data. $ vi f1.txt => vi command is having 3 modes 1) command mode (just to open file) 2) insert mode (to edit the file) -----> press 'i' in keyword 3) esc mode (to come out from insert mode) --> press 'esc' in keyword # save the changes and close the file ====> :wq + enter # close the file without saving changes ===> :q! + enter Note: vi command will open the file if it is already avilable otherwise it will create new file and it will open that file. ======================== file creation commands ======================== touch : to create empty file cat : create file with data (cat > f1.txt) cp : copy one file data into another file (cp f1.txt f2.txt) vi : create and open file for editing (vi f3.txt) ==================== reading file data ==================== cat : print file data from top to bottom tac : print file data from bottom to top head : print first 10 lines of file data tail : print last 10 lines of file data vi : open the file and read data in command mode ============= SED Command ============= => SED stands for stream editor => SED is used to process the data (substitute, delete, print, insert) => Using SED command we can perform operations on the file without opening the file. => SED is very powerful command in linux for processing file data => we will use below options with SED command s - substitute d - delete p - print i - insert # replace first occurance of linux with unix in every line sed 's/linux/unix/' os.txt # replace second occurance of linux with unix in every line sed 's/linux/unix/2' os.txt # replace third occurance of linux with unix in every line sed 's/linux/unix/3' os.txt # replace all occurances of linux keyword with unix sed 's/linux/unix/g' os.txt # substitute linux with unix and save changes in the file sed -i 's/linux/unix/g' os.txt # delete first line of the file sed -i '1d' os.txt # delete 4th line of the file sed -i '4d' os.txt # delete last line of the file sed -i '$d' os.txt # delete from 3rd line to last line sed -i '3, $d' os.txt # delete from 10th line to 15th line sed -i '10, 15d' os.txt # print 10th line only sed -n '10p' os.txt # print data from 2nd line to 5th line sed -n '2,5p' os.txt # Add line at end of file sed -i '$a\i am from ashokit' data.txt # Add line at end of file sed -i '2i\i want to learn devops' os.txt ====================== Networking commands ====================== ## ping : To check connectivity ping www.google.com ping www.facebook.com ping 192.168.1.34 ## ifconfig : To get IP address of our machine ## wget : It is used to download files from internet wget ## curl : It is used to send http request to the url curl curl https://dummyjson.com/quotes/random =========================Assignments ========================== ๐Ÿ‘‰ *1) User Management in Linux* : https://youtu.be/nESqeKi1aZc ๐Ÿ‘‰ *2) How to enable pwd based authentication in linux* : https://youtu.be/tptJFz-eLPA ๐Ÿ‘‰ *3) File Permissions in Linux* : https://youtu.be/r47yfVMi45U ================================================================ ================= User Management ================= => Linux is a multi user based OS => Multiple users can acces single linux machine and can perform multi tasking at time. => Within one linux machine we can create multiple user accounts. Note: "ec2-user" is a default user in "amazon linux" vm. ec2-user having sudo priviliges. Amazon Linux AMI ===> ec2-user Ubuntu AMI ==> ubuntu => when we create user account, for user one home directory will be created. ec2-user => /home/ec2-user john => /home/john smith => /home/smith # display all users created cat /etc/passwd # create user sudo useradd # set pwd for user sudo passwd # delete user along with user home directory sudo userdel --remove # rename user sudo usermod -l ###### /etc/passwd : This file contains users information ##### =================================== Working with user groups in linux =================================== => When we create user in linux, for that user one user group also will be created with the given username. # display all groups cat /etc/group # create group in linux sudo groupadd # add user to group sudo usermod -aG # remove user from group sudo gpasswd -d # display users belongs to group sudo lid -g # check one user belongs to how many groups id # delete group sudo groupdel # changing group name $ sudo groupmod -n ================================= What is sudoers file in Linux ================================= => It is very important configuration file in linux machine. => Using this file we can control which user can run command as a superuser. # print sudoers file content sudo cat /etc/sudoers # Open sudoers file sudo visudo # Add below line under root user ALL=(ALL) ALL => After making changes to close sudoers file => ( CTRL + X + Y + Enter) ======================================================== How to enable password based authentication for users ? ======================================================== => In linux vm, by default passwordauthentication is no => If we want to connect with linux vm by using username and password then we need to set passwordauthentication value as yes. => WE WLL MODIFY THIS IN "sshd_config" file. # Display sshd_configurration file data $ sudo cat /etc/ssh/sshd_config # Open file $ sudo vi /etc/ssh/sshd_config Note: Go to insert mode and enable pwdbasedauthentication as yes # restart sshd service # sudo systemctl restart sshd Note : Now we can connect with linux vm using username and pwd Step-1 : Open gitbash Step-2 : Execute below command $ ssh uname@public-ip ========================== File Permissions in Linux ========================== => Using file permissions we can secure our files and we can protect our file data. => We have 3 types of permissions in linux r => read w => write x => execute => file/directory permissions will be represented like below rwxrwxrwx f1.txt => file permissions contains 9 characters and those are divided into 3 parts first 3 characters => user/owner permissions middle 3 characters => group permissions last 3 characters => other users permissions => Lets under file permissions with diff scenarios Scenario-1 :: rw-r--r-- f1.txt user => read + write group => read others => read Scenario-2 :: rwxr-xr-x f1.txt user => read + write + execute group => read + execute others => read + execute Scenario-3 : rwx--xr-x f1.txt user => read + write + execute group => execute others => read + execute => To change file/directory permissions we will use 'chmod' command + => To add permission - => To remove permission # Giving execute permission for user $ chmod u+x f1.txt # giving write permission for group $ chmod g+w f1.txt # Remove execute permission for others $ chmod o-x f1.txt # give all permissions for group $ chmod g+rwx f1.txt # Remove all permissions for others $ chmod o-rwx f1.txt ==================================== File Permissions in Numeric Format ==================================== 0 => No Permission 1 => Execute 2 => Write 3 => ( 2 + 1) => Write + Execute 4 => Read 5 => (4 + 1) => Read + Execute 6 => (4 + 2) => Read + Write 7 => (6 + 1) => Read + Write + Execute # ugo + x chmod 111 f2.txt # ugo + w chmod 222 f2.txt # u+r g+rx o+rw chmod 456 f2.txt # u+rwx g+rw o+rx chmod 765 f2.txt # u-rwx g-rwx o+rwx chmod 7 f2.txt =============== chown command =============== => It is used to change file/directory ownership # change owner sudo chown new-owner # change owner-group sudo chown :new-group # change owner & group of file/directory sudo chown new-owner:new-group ========================== Package Managers in Linux ========================== => Package means a software Ex: git, maven, java, python etc..... => package managers are used to manage software packages (softwares) in linux machine. => package managers are specific to linux distribution Amazon Linux ===> yum Ubuntu Linux ===> apt # check git client installed or not git --version # install git client s/w sudo yum install git -y # check git installation path whereis git # check java installed or not java -version # install java s/w sudo yum install java -y # check java installation path whereis java ============================= Webserver Setup in Linux VM ============================ => Webserver is a software which is used to run websites / web applications. => Website means collection of web pages ex: login page, register page, dashboard page, about us page .... => Websites are divided into 2 types 1) static website 2) dynamic website => The website which gives same response for every user is called as static website. Ex: www.eenadu.net => The website which gives response based on logged in user is called as dynamic website. Ex : www.gmail.com => To run static websites we can use 'httpd' as a webserver. => To run dynamic websites we can use 'tomcat, iis, jboss, weblogic as webservers. # install httpd webserver sudo yum install httpd -y # check httpd status sudo service httpd status # start httpd server sudo service httpd start Note: httpd webserver runs on http protocol with 80 port number. Note: To access our webserver we need to enable http protocol in Ec2 vm security group inbound rules. # Navigate to website content directory $ cd /var/www/html # create index.html file with website content $ sudo vi index.html => We can access our webserver using ec2-vm public ip. ================================ What is systemctl in linux ? ================================ => systemctl is used to manage services in linux machines. => using systemctl we can perform below operations 1) start service 2) stop service 3) re-start service #stop the service sudo systemctl stop httpd #start the service sudo systemctl start httpd #reload service sudo systemctl reload httpd ============================================= How to change hostname in vm (temporarly) ? ============================================= # set hostname $ sudo hostname # re-start session $ exit ============================================= How to find the location of files in linux? ============================================= => in linux we can use 'find' command to search file paths. # search for the files which are having name as f1.txt sudo find /home -name f1.txt # search for empty files inside /home sudo find /home -type f -empty # search for empty directories inside /home sudo find /home -type d -empty # Matches files that were last modified more than 30 days ago. sudo find /home -type f -mtime +30 # Find files modified within the last 30 days. sudo find /home -type f -mtime -30 # Exactly 30 days ago. sudo find /home -type f -mtime 30 ===================== process management ===================== => Process management in Linux involves controlling and monitoring the execution of programs (processes) on the system. # display processes running with PID $ ps aux Note: Every process will have process id (PID) # kill process kill # terminate process immediatley (forcefully) kill -9 =========================== Working with Link Files =========================== => In linux we can create link files ( similar to shorcut files in windows ) => We have 2 types of link files in linux 1) Hard link 2) Soft link (symbolic link) -------------------------------- Syntax To create Hard Link -------------------------------- $ ln $ touch f1.txt $ ln f1.txt f100.txt Note: f100.txt is link file for f1.txt Note: If we write some data to original file, it is reflecting in link file also. Note: when we delete original file, link file is not effected (hard link). ------------------------------ Syntax To create Soft Link ------------------------------ $ ln -s Ex: $ touch s1.txt $ ln -s s1.txt s100.txt $ cat >> s1.txt Note: Original file data reflecting in soft link file also. Note: When we remove original file then soft link file will become dangling file. We can't access that file. ============================================================================== Assignment : How to copy a file from one linux vm to another linux vm. Solution : scp -i "pem file" /file/path/ uname@public-ip:/dest/path/ # Step-1 : Connect with vm-1 using mobaxterm # Step-2 : Upload pem file and give read permission chmod 400 # Step-3 : Create file in vm-1 using cat or vi command # Step-4 : Execute scp command to copy file from vm-1 to vm-2 Ex: $ scp -i "awslab.pem" os.txt ec2-user@vm2-public-ip:/home/ec2-user/ # Step-5 : Connect with vm-2 and file copied or not. ========================================================== How to copy files from windows machine to linux machine ? ========================================================== => By using "WinSCP" tool we can copy files from windows to linux and vice versa. => WinSCP is a GUI tool to connect from windows to linux machine. left side : windows file system right side : linux file system ============================================================= 1) whoami 2) pwd 3) date 4) cal 5) cd 6) ls 7) mkdir 8) rmdir 9) rm 10) touch 11) mv 12) cp 13) cat 14) tac 15) head 16) tail 17) grep 18) vi 19) sed 20) ping 21) ifconfig 22) wget 23) curl 24) find 25) ps aux 26) kill 27) useradd 28) passwd 29) userdel 30) usermod 31) groupadd 32) groupdel 33) gpasswd 34) lid 35) id 36) groupmod 37) chmod 38) chown 39) systemctl 40) yum install 41) whereis 42) uptime 43) free : system memory usage 44) top : monitoring system performance in real-time. $ top $ top -u ec2-user 45) cat /etc/os-release : print machine os-family details. 46) uname -r : display kernel version 47) uname -a : all system info 48) hostname 49) arch : CPU architecture 50) lsblk : list block devices (storage) 51) df -h : show disc space 52) du -sh * : show disc usage 53) ln : to work with link files 54) scp : secured copy ========= Summary ========= 1) What is Linux OS 2) Windows Vs Linux 3) Linux VM Setup in AWS cloud 4) Connecting with Linux VM using SSH client 5) Directories & Files Based Operations 6) Text Editors 7) Text filters 8) User & Group Management 9) File Permissions 10) File Ownership 11) Archieves 12) Networking 13) suderos file 14) sshd_config 15) package managers 16) static website hosting (httpd) 17) Service Management (systemctl) 18) Process management 19) Working with link files 20) Changing Hostname ======================================= Linux Important Interview Questions ======================================= 1) What is the difference between Linux and Unix? Linux : Free Os Unix : Proprioty os 2) How do you find out what process is using a specific port like 8080 ? sudo lsof -i :8080 3) What does the 'chmod 755' command do? owner (7) : read + write + execute group (5) : read + execute others (5) : read + execute 4) Whatโ€™s the difference between a soft link and a hard link? Soft link (symlink): Points to the file path Hard link: Points to the fileโ€™s inode. Still works if original file is deleted. 5) What are file permissions in Linux? How do you change them? rwxrwxrwx chmod 6) How do you check disk space usage? df -h du -sh /dir/path/ 7) What command is used to view system logs? journalctl 8) What is the difference between apt and yum? yum โ†’ RHEL-based (CentOS, Fedora, Amazon Linux) apt โ†’ Debian-based (Ubuntu) 9) How do you add a user to a group? sudo usermod -aG 10) How do you search for a string in a file using the terminal? grep "search_term" filename 11) What is the difference between /etc/passwd and /etc/shadow ? /etc/passwd: User details /etc/shadow: Encrypted passwords (more secure) 12) How do you manage services with systemd? using systemctl we can manage services systemctl start nginx systemctl enable docker systemctl status sshd 13) how to find 30 days old files in linux ? using find command with mtime. 14) How to find linux vm os family details cat /etc/os-release 15) How to find linux kernel version uname -r 16) How to find user belongs to which groups ? id 17) How to check linux machine execution time ? uptime 18) How to change file/directory owner in linux ? chown 19) How to enable pwd based authentication in linux ? we need to enable in "/etc/sshd/sshd_config" file 20) how to provide sudo priviliages for a user in linux ? by configuring user in sudoers file (visudo) 21) How to create a user without home directory ? # create user along with home directory sudo useradd john # create user without home directory sudo useradd -M smith 22) how to change password for the user ? sudo passwd