Please enable JavaScript.
Coggle requires JavaScript to display documents.
Linux, Operating System - Coggle Diagram
Linux
package manager
downloads, installs and updates existing software from a repository and ensures the authenticity and integrity of the package. manages and resolves all required dependencies needed for the software to work. Splits binaries into /bin and /sbin folder and places dependencies in /lib folder.
Software packages are verified before added to the repository. It can happen that you won't get the software or latest version of a software from the repositories. Like browsers and code editors are not available on the repositories mostly.
apt (Advance Package Tool)
Debian based distros like Ubuntu, Debian, Mint, etc
- apt search <package_name>
- apt install <package_name>
- apt remove <package_name>
apt update : always execute this command before installing to update the software and their latest versions from the repositories
ls /etc/apt/sources.list : shows all the repositories apt will use the fetch and install the packages.
When installing newer application they will have a guide to install the application by adding the repository to this sources.list file and by using the command add-apt-repository <repository_name>.
Also people can share software using PPA which Personal Package Archive maintained by Marcel Kapfer and is available to ubuntu and linux distribution based on ubuntu. Just replace repository_name in above command with ppa:software_path
ppa are no guarantee software with come with a risk of security. It can cause after upgrading to new OS release.
-
apt-get
- same as apt but with no search command and progress bar
- more command options but not user friendly. Recommended to use apt instead of apt-get
snap
- contains application and its dependencies bundled into one package.
- supports universal linux packages (.snap)
- automatic updates
- only problem is that shared dependencies will be downloaded each time for each user differently increasing size of the package and redundancy.
YUM
Red Hat based distros like RHEL, CentOS, Fedora,etc
Users and Permissions
categories
- superuser/root user : unrestricted permission
- Regular user: A regular user we create to login
- Service user: User create to run their own services like mysql user to run mysql service. Never run services using root user!
In windows users can be managed centrally through an admin machine which connects to all user machines whereas in linux user accounts are managed on that specific hardware only. That is why, companies and universities prefer windows over linux.
- User level permissions
- Groups level permissions : add user to group and give group permissions so that user can inherit those. Flexibility of removing and adding multiple user at the same time.
access control files
/etc/passwd
- stores user account info. Everyone can read it but only root user can change it
- shahid:x:1244:3245:shahid,,,:/home/shahid:/bin/bash this means username:password:UserId:GroupId:Description:homedirectory:default_shell_of_user
- password is encrypted and stored in /etc/shadow file and groupId can be found in /etc/group file
- UID 0 is for root user
user managing commands
- adduser <username> : creates new user and automatically chooses UID and GID values and creates home directory with minimal configuration
- groupadd <groupname> : creates new group and assisgns the next available GID from the range specified in login.defs file
- there other similar commands like useradd and addgroup. The difference is command starting with add are more interactive and user friendly. They automatically chosse UID and GID for you and also create home directory with skeletal config. Use them when executing manually.
- Whereas commands ending with add will need you to provide values as input. Use them when automating.
- userdel and deluser
- groupdel and delgroup
- usermod : to modify user group and configurations.
- groups : lists all groups. groups <username> will list groups associated to the mentioned user.
-
basic cli commands
on linux cli you would see username@hostname of the machine who has logged in before typing command
Each following command either takes pathname or absolute path or filename/foldername
- ~ : it is the shortcut to users home directory path
- $ : represents regular user
-
#
: represents root user
- pwd : print working directory. Shows the path you are currently in
- ls : list folders and files
- cd : change directory to preferred path
- mkdir : create new directory/folder
- touch : create files
- rm : delete files/folders. -r flag is to delete a non empty folder and its subfolders. -d/rmdir to delete an empty directory
- clear : clears the terminal
- mv : rename or move file to desired location. for renaming just use mv <filename> <new_filename>. and for moving give path in second argument above.
- cp : copy files and folders to desired location. same arguments as move. Use -r flag to recursively copy non empty folder.
- history : gives history of commands typed in the current terminal session of the user
- cat : display contents of a file
- echo : print out desired string or variable/file content
Some Previous commands with flags and combination commands
- ls -R <dirname> : to list all files in a folder recursively
- ls -a : lists hidden files also
- ls -l : lists in vertical format with details or in long listing format.
- history 20 : will output history of last 20 commands typed
- CTRL + r : Search command history
- CTRL + c : stop command execution
- SHIFT + INSERT or CTRL + SHIFT + V : to paste
- SPACEBAR or n - used to navigating page for most commands
- b : used to navigating previous page for most commands
- q : for quitiing command
- su - <username> : switch user
- su - : Login as root
advanced commands
- uname -a : show system details and kernel
- cat /etc/osrelease : shows details about operating system
- lscpu : shows details about cpu
- lsmem : shows details about memory
- less : displays contents of a file or command one page at a time. It loads faster as it does not read the whole file at a time.
- grep : Globally Search for Regular Expression and Print out. Search pattern of characters and displays all the lines that contain that pattern.
superuser commands
- sudo : allows regular users to execute superuser or root user commands
- adduser <username>
- addgroup <groupname>
pipes and redirects
- pipes ( | ) are used for chaining outputs as input to another commands
- Example : history | grep sudo | less
- redirects ( > ) are used to save or append (>>) to a new file or an existing file.
- Example : history 30 > history.txt
Vi and Vim editor
Vim is more improved version of Vi. Vi is most used text editor in linux distros. Vim may be or may not be installed depending on the linux distro
commands
- vim <filename> : open existing or create new file and edit with vim
- default mode: cannot edit text. Used for navigating, searchingm deleting, undo, save, quit, etc. Pressing ESC key will put you in this mode.
- Insert mode : Press i to switch to this mode and enter or edit text.
- dd : to delete whole line
- d10 : to delete next 10 lines
- :wq : to write to the file and quit
- :q! : to quit without saving changes
- u : to undo previous change
- A : jump end of line and switch to insert mode
- 0 : jump to start of the line
- $ : jump to end of the line
- 10G : jump to 10th line
- /<pattern> : to search words and use n key to jump to next match and N to search match in opposite direction
- %s/old/new : to replace a old word with new word
shell scripting
- automation of running commands using a script on cli
- for logical and bulk operations
- end with .sh extension which is movable and user can write and execute it.
- Shell is a program that interprets and executes various commands that we type in the terminal
- You can execute file using command sh <scriptname> or bash <scriptname> or ./<scriptname to execute directly from default shell
types:
- sh (Bourne Shell) - /bin/sh
- Bash (Bourne again Shell) - /bin/bash - improved version of sh.
- Z shell ( Extended bourne shell) - /bin/zsh
- There are many other shell but these are the popular one and most widely used
How does the machine know which shell to use
- By using shebang line at start of the file
-
#
!/bin/sh or #
!/bin/bash or #
!/bin/zsh
-
#
in musical notation is called sharp and ! is also called bang. That's why the name Shebang.
-
file system
- hierarchical tree structure ( one root folder )
/
- /home : home directories for all non-root users
- /root : home directory for root user
- /bin : basic Linux command binaries are located here
- /sbin : system binaries/commands that need superuser permission are located here like adduser, change password, etc
- /lib : essential shared libraries that executables use from /bin and /sbin folders are located here
- /usr : this was the user location before /home location was added in the file system. But now it has the same /bin, /sbin and /lib folder as the root (/) location. The binaries are duplicated and split into both this locations. Most of the time /usr would have more binaries than the root (/).
- /usr/local : this will contains the third party application binaries that you install as an user such as java, docker, etc. This also has the /bin and /sbin folder which are specifically for third party applications installed. Applications installed here will be available for all user system wide.
- /opt : this folder serves same purpose as /usr/local which is location installed third party application. But the catch here is, it is only for applications which do not split their binaries in /bin and /sbin. For example code editors and web browsers.
- /boot : contains files required for booting the system
- /etc : place where configuration files (.conf) for system wide applications are stored
- /dev : location for files for external devices like mouse, keyboard. webcams, etc are stored here. Apps and drivers will access this and not users.
- /var : contains files that system writes during course of its operation. /var/log contains log files where as /var/cache contains cache from applications.
- /tmp : temporarily files required by some processes kept here temporarily. For example your system kills some processes and then needs the data it will be stored here. The files will be deleted sometime in near future when the process is done with.
- /media : contains files or subdirectories of removable media such as dvd's, cd's and usb's. The media will also be referenced in /dev location
- /mnt : for manually mounting temporary files systems you can use this location. For example, sys admins previously used to mount file systems temporarily here.
hidden files and folders : this start with dot at the start such .cache, .ssh, .config, .bashhistory, etc. This files and folders are created by the OS or the applications to store data.
EVERYTHING IN LINUX IS A FILE
Documents, pictures, audio, folders, files, commands, devices, etc. are all files.
Operating System
Components?
Kernel
Heart of every OS. This is the part which loads first on startup. Every OS have their own kernel different from each other.
It is basically a software/program which handles communication from apps to hardware such as memory, storage, cpu. It also handles i/o devices communication using device drivers (another software, different drivers for different devices.
It starts a process for a application, allocates resources for the application and clean up resources after application termination.
-
-
macOs vs Linux
- Unix was created in 1970 which was eventually used as codebase from many different OS.
- macOs Darwin kernel is based on Unix
- To keep all OS built on top of Unix compatible standards were introduced namely POSIX standard (Portable Operating System Interface).
- On the other hand Linux was developed in parallel to Unix in 1991 by Linus Torvalds.
- Linux was not based on Unix but rather a clone of Unix that's why also called as Unix Like
- Linux and macOs are both POSIX compliant.