Linux Flashcards
The Red Hat Family
Red Hat Enterprise Linux (RHEL), CentOS, Scientific Linux and Oracle Linux
Key Facts About the Red Hat Family
- It often serves as an upstream testing platform for RHEL.
- CentOS and Scientific Linux are close clones of RHEL, while Oracle Linux is mostly a copy with some changes.
- Kernel version 3.10 is used in RHEL/CentOS 7.
- It supports hardware platforms such as x86, x86-64, Itanium, PowerPC, and IBM System z.
- It uses the RPM-based yum package manager (we cover it in more detail later) to install, update, and remove packages in the system.
- RHEL is widely used by enterprises which host their own systems.
The SUSE Family
SUSE, SUSE Linux Enterprise Server (SLES), and openSUSE
Key Facts About the SUSE Family
- SUSE Linux Enterprise Server (SLES) is upstream for openSUSE.
- Kernel version 4.12 is used in openSUSE Leap 15.
- It uses the RPM-based zypper package manager (we cover it in more detail later) to install, update, and remove packages in the system.
- It includes the YaST (Yet Another Setup Tool) application for system administration purposes.
- SLES is widely used in retail and many other sectors.
The Debian Family
Ubuntu, Linux Mint etc
Key Facts About the Debian Family
- The Debian family is upstream for Ubuntu, and Ubuntu is upstream for Linux Mint and others.
- Kernel version 4.15 is used in Ubuntu 18.04 LTS.
- It uses the DPKG-based APT package manager (using apt-get, apt-cache, etc. which we cover in more detail later) to install, update, and remove packages in the system.
- Ubuntu has been widely used for cloud deployments.
- While Ubuntu is built on top of Debian and is GNOME-based under the hood, it differs visually from the interface on standard Debian, as well as other distributions.
The boot process
- Power ON
- BIOS(Basic Input/Output system) - initializes screen and keyboard and tests the main memory(POST - power on self Test)
- MasterBootRecord(MBR) also known as first sector of the Hard Disk
- Boot loader(e.g. GRUB)
- Kernel(Linux OS)
- Initial RAM Disk - initramfs Image
- /sbin/init (parent process)
- Command Shell using getty
- X Window System
A number of boot loaders exist for Linux
the most common ones are GRUB (for GRand Unified Boot loader), ISOLINUX (for booting from removable media), and DAS U-Boot (for booting on embedded devices/appliances).
For systems using the BIOS/MBR method, the boot loader resides at the {1} of the hard disk, also known as the {2}. The size of the {2} is just {3} bytes. In this stage, the boot loader examines the partition table and finds a bootable partition. Once it finds a bootable partition, it then searches for the second stage boot loader, for example GRUB, and loads it into RAM (Random Access Memory).
- first sector
- Master Boot Record (MBR)
- 512
For systems using the EFI/UEFI method, UEFI firmware reads its {1} data to determine which {2} is to be launched and from where (i.e. from which disk and partition the EFI partition can be found). The firmware then launches the {2}, for example GRUB, as defined in the boot entry in the firmware’s boot manager.
- Boot Manager
2. UEFI application
The second stage boot loader resides under…
/boot. A splash screen is displayed, which allows us to choose which operating system (OS) to boot. After choosing the OS, the boot loader loads the kernel of the selected operating system into RAM and passes control to it. The boot loader loads the selected kernel image and passes control to it. Kernels are almost always compressed, so its first job is to uncompress itself. After this, it will check and analyze the system hardware and initialize any hardware device drivers built into the kernel.
The initramfs filesystem image contains
programs and binary files that perform all actions needed to mount the proper root filesystem, like providing kernel functionality for the needed filesystem and device drivers for mass storage controllers with a facility called udev (for user device), which is responsible for figuring out which devices are present, locating the device drivers they need to operate properly, and loading them. After the root filesystem has been found, it is checked for errors and mounted.
The mount program instructs the operating system that a filesystem is ready for use, and associates it with a particular point in the overall hierarchy of the filesystem (the mount point). If this is successful,
the initramfs is cleared from RAM and the init program on the root filesystem (/sbin/init) is executed.
init handles
the mounting and pivoting over to the final real root filesystem. If special hardware drivers are needed before the mass storage can be accessed, they must be in the initramfs image.
The initial RAM Disk
- mount proper root filesystem
- providing kernel functionality
- locating devices
- locating drivers and load them
- checking for errors in root filesystem
Most distributions start six text terminals and one graphics terminal starting with F1 or F2. Within a graphical environment, switching to a text console requires pressing
CTRL-ALT + the appropriate function key (with F7 or F1 leading to the GUI).
The boot loader loads both
the kernel and an initial RAM–based file system (initramfs) into memory, so it can be used directly by the kernel.
When the kernel is loaded in RAM, it immediately initializes and configures
the computer’s memory and also configures all the hardware attached to the system. This includes all processors, I/O subsystems, storage devices, etc. The kernel also loads some necessary user space applications.
/sbin/init and Services
Once the kernel has set up all its hardware and mounted the root filesystem, the kernel runs /sbin/init. This then becomes the initial process, which then starts other processes to get the system running. Most other processes on the system trace their origin ultimately to init; exceptions include the so-called kernel processes. These are started by the kernel directly, and their job is to manage internal operating system details.
Besides starting the system, init is responsible
for keeping the system running and for shutting it down cleanly.
Startup Alternatives
1) Upstart
- Developed by Ubuntu and first included in 2006
- Adopted in Fedora 9 (in 2008) and in RHEL 6 and its clones.
2) systemd
- Adopted by Fedora first (in 2011)
- Adopted by RHEL 7 and SUSE
- Replaced Upstart in Ubuntu 16.04.
systemd Features
- Systems with systemd start up faster than those with earlier init methods. This is largely because it replaces a serialized set of steps with aggressive parallelization techniques, which permits multiple services to be initiated simultaneously.
- Complicated startup shell scripts are replaced with simpler configuration files, which enumerate what has to be done before a service is started, how to execute service startup, and what conditions the service should indicate have been accomplished when startup is finished. One thing to note is that /sbin/init now just points to /lib/systemd/systemd; i.e. systemd takes over the init process.
Starting, stopping, restarting a service (using nfs as an example) on a currently running system:
{1}
Enabling or disabling a system service from starting up at system boot:
{2}
1) $ sudo systemctl start|stop|restart nfs.service
2) $ sudo systemctl enable|disable nfs.service
Linux Filesystems
1) Conventional disk filesystems: ext2, ext3, ext4, XFS, Btrfs, JFS, NTFS, etc.
2) Flash storage filesystems: ubifs, JFFS2, YAFFS, etc.
3) Database filesystems
4) Special purpose filesystems: procfs, sysfs, tmpfs, squashfs, debugfs, etc.
A partition is a
physically contiguous section of a disk, or what appears to be so in some advanced setups.
A filesystem is a
method of storing/finding files on a hard disk (usually in a partition).
The Filesystem Hierarchy Standard
Linux systems store their important files according to a standard layout called the Filesystem Hierarchy Standard (FHS)
1) /bin/
2) /boot/
3) /dev/
4) /etc/
5) /home/
6) /lib/
7) /media/
8) /mnt/
9) /opt/
10) /sbin/
11) /srv/
12) /tmp/
13) /usr/
14) /var/
15) /root/
16) /proc/
1) essential user command binaries
2) static files of the boot loader
3) device files
4) HOST-specific system configuration
5) user home directories
6) essential shared libraries and kernel modules
7) mount point for removable media
8) mount point for a temporarily mounted filesystems
9) add-on application software packages
10) system binaries
11) data for services provided by this system
12) temporary files
13) multiuser utilities and applications(secondary hierarchy)
(required bin, include, lib, local, sbin, share)
14) variable files
15) home directory for root user
16) virtual filesystem documenting kernel and process status as text files
Choosing a distributive:
1) for server
2) for desktop
3) for embedded
1) RHEL, CentOS, Ubuntu Server, SLES, Debian
2) Ubuntu, Fedora, LinuxMint, Debian
3) Yocto, Open Embedded, Android
Many installers can do an installation completely automatically, using a configuration file to specify installation options. This file is called a {1} for Red Hat-based systems, an {2} for SUSE-based systems, and a {3} for Debian-based systems.
1) Kickstart file
2) AutoYAST profile
3) Preseed file
What does display manager
1) Display management
2) Loads X Server
3) Manage Graphical logins
A desktop environment consists of a
session manager, which starts and maintains the components of the graphical session, and the window manager, which controls the placement and movement of windows, window title-bars, and controls.
Seamless desktop environment
Session manager + Window manager + A set of utilities
The default display manager for GNOME is called
gdm
To show hidden files, select Show Hidden Files from the menu or press
CTRL-H
o open the File Manager from the command line, on most systems simply type
nautilus
Another quick way to access a specific directory is to press
CTRL-L
The default text editor in GNOME is
gedit
Deleting a file in Nautilus will automatically move the deleted files to the
.local/share/Trash/files/
To delete a file without trashing it
select the file or directory you want to permanently delete and press Shift-Delete
Find the latest modified file in
/var/log
The X server, which actually provides the GUI, uses the what file?
/etc/X11/xorg.conf
he Network Time Protocol (NTP) is the most popular and reliable protocol for setting the
local time via Internet servers.
more detailed configuration is possible by editing the standard NTP configuration file which located in…
/etc/ntp.conf
ascertain your current resolution by typing at the command line
xdpyinfo | grep dim
For Debian-based systems, the higher-level package management system is the
apt (Advanced Package Tool)
underlying package manager for DEBIAN-BASED systems.
dpkg
Most input lines entered at the shell prompt have three basic elements:
1) Command
2) Options
3) Arguments.
creating sudo user
1) su
2) echo “username ALL=(ALL) ALL” > /etc/sudoers.d/username
3) chmod 440 /etc/sudoers.d/username
Virtual Terminals (VT) are console sessions that use the entire display and keyboard outside of a graphical environment. Such terminals are considered “virtual” because
although there can be multiple active terminals
To switch between VTs, press
CTRL-ALT-function key for the VT. For example, press CTRL-ALT-F6 for VT 6. Actually, you only have to press the ALT-F6 key combination if you are in a VT and want to switch to another VT.
Turning Off the Graphical Desktop
$ sudo systemctl stop gdm (or sudo telinit 3)
restart Graphical Desktop (after logging into the console)
$ sudo systemctl start gdm (or sudo telinit 5)
The preferred method to shut down or reboot the system is to use the {1} command. This sends a warning message, and then prevents further users from logging in. The init process will then control shutting down or rebooting the system. It is important to always shut down properly; failure to do so can result in damage to the system and/or loss of data.
1) shutdown
The halt and poweroff commands issue
shutdown -h to halt the system;
reboot issues
shutdown -r
When administering a multiuser system, you have the option of notifying all users prior to shutdown, as in:
$ sudo shutdown -h 10:00 “Shutting down for scheduled maintenance.”
In general, executable programs and scripts should live in the
/bin, /usr/bin, /sbin, /usr/sbin directories, or somewhere under /opt, or /usr/local/bin and /usr/local/sbin, or in a directory in a user’s account space, such as /home/student/bin.
One way to locate programs is to employ the
which
Broader way to locate programs
whereis
Multiple slashes (/) between directories and files are allowed, but all but
one slash between elements in the pathname is ignored by the system.
List all files, including hidden files
ls –a
Suppose that file1 already exists. A hard link, called file2, is created with the command
$ ln file1 file2
The {1} option to ls prints out in the first column the inode number, which is a unique quantity for each file object
-i
Soft (or Symbolic) links are created with the -s option, as in:
ln -s file1 file3
push directory, pop directory, list directories
pushd, popd, dirs
cat
Used for viewing files that are not very long; it does not provide any scroll-back.
tac
Used to look at a file backwards, starting with the last line.
less
Used to view larger files because it is a paging program. It pauses at each screen full of text, provides scroll-back capabilities, and lets you search and navigate within the file. Note: Use / to search for a pattern in the forward direction and ? for a pattern in the backward direction. An older program named more is still used, but has fewer capabilities: “less is more”.
tail
Used to print the last 10 lines of a file by default. You can change the number of lines by doing -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default.
head
The opposite of tail; by default, it prints the first 10 lines of a file.
touch is often used to
set or update the access, change, and modify times of files. By default, it resets a file’s timestamp to match the current time.
However, you can also create an empty file using touch:
$ touch
touch provides several useful options. For example, the -t option allows you to set the date and timestamp of the file to a specific value, as in:
$ touch -t 12091600 myfile
This sets the myfile file’s timestamp to 4 p.m., December 9th (12 09 1600).
rm –i
interactivily remove file
The PS1 variable is the
character string that is displayed as the prompt on the command line. Most distributions set PS1 to a known default value, which is suitable in most cases. However, users may want custom information to show on the command line.
there are three standard file streams (or descriptors) always open for use:
standard input (standard in or stdin), standard output (standard out or stdout) and standard error (or stderr).
stdin is file descriptor 0, stdout is file descriptor 1, and stderr is file descriptor 2
send input data to program
$ do_something < input_file
If you want to send the output to a file
$ do_something > output-file
* Because stderr is not the same as stdout, error messages will still be seen on the terminal windows in the above example.
If you want to redirect stderr to a separate file
$ do_something 2> error-file
Note: By the same logic, do_something 1> output-file is the same as do_something > output-file.
A special shorthand notation can send anything written to file descriptor 2 (stderr) to the same place as file descriptor 1 (stdout)
$ do_something > all-output-file 2>&1
bash permits an easier syntax for the above:
$ do_something >& all-output-file
locate
performs a search taking advantage of a previously constructed database of files and directories on your system, matching all entries that contain a specified character string. This can sometimes result in a very long list.
locate utilizes a database created by a related utility, updatedb. Most Linux systems run this automatically once a day.
wildcards
? Matches any single character
* Matches any string of characters
[set] Matches any character in the set of characters, for example [adf] will match any occurrence of “a”, “d”, or “f”
[!set] Matches any character not in the set of characters
find
Searching for files and directories named gcc:
$ find /usr -name gcc
Searching only for directories named gcc:
$ find /usr -type d -name gcc
Searching only for regular files named gcc:
$ find /usr -type f -name gcc
To find and remove all files that end with .swp
:
$ find -name “*.swp” -exec rm {} ’;’
The {} (squiggly brackets) is a placeholder that will be filled with all the file names that result from the find expression, and the preceding command will be run on each one individually.
Please note that you have to end the command with either ‘;’ (including the single-quotes) or “\;”. Both forms are fine.
One can also use the -ok option, which behaves the same as -exec, except that find will prompt you for permission before executing the command. This makes it a good way to test your results before blindly executing any potentially dangerous commands.
To find files based on time
$ find / -ctime 3
To find files based on sizes
$ find / -size 0
For example, to find files greater than 10 MB in size and running a command on those files
$ find / -size +10M -exec command {} ’;’
Both package management systems operate on two distinct levels
a low-level tool (such as dpkg or rpm) takes care of the details of unpacking individual packages, running scripts, getting the software installed correctly, while a high-level tool (such as apt-get, yum, dnf or zypper) works with groups of packages, downloads packages from the vendor, and figures out dependencies.
List all installed packages(dpkg)
dpkg -l или dpkg –list
List all files contains in package(dpkg)
dpkg –listfiles bzip2
Package remove(dpkg)
dpkg –remove bzip2
Поиск по всем пакетам(apt)
sudo apt-cache search wget2
Man list all pages on the topic
man -f topic
same as whatis
Man list all pages that discuss a specified topic (even if the specified subject is not present in the name)
man -k topic
same as apropos
man will display all pages with the given name in all chapters, one after the other, as in:
man -a socket
man show topic in chapter n(n - integer)
man n topic
displays an index of available topics(not man)
info Items function like browser links and are identified by an asterisk (*) at the beginning of the item name. Named items (outside a menu) are identified with double-colons (::) at the end of the item name. Items can refer to other nodes within the file or to other files. n Go to the next node p Go to the previous node u Move one node up in the index
Processes can be of different types according to the task being performed
Need to be started by a user, either at a command line or through a graphical interface such as an icon or a menu selection.
examples: bash, firefox, top
Automatic processes which are scheduled from and then disconnected from the terminal. These tasks are queued and work on a FIFO (first-in, first-out) basis. examples: updatedb
Server processes that run continuously. Many are launched during system startup and then wait for a user or system request indicating that their service is required.
examples: httpd, xinetd, sshd
Lightweight processes. These are tasks that run under the umbrella of a main process, sharing memory and other resources, but are scheduled and run by the system on an individual basis. An individual thread can end without terminating the whole process and a process can create new threads at any time. Many non-trivial programs are multi-threaded.
examples: firefox, gnome-terminal-server
Kernel tasks that users neither start nor terminate and have little control over. These may perform actions like moving a thread from one CPU to another, or making sure input/output operations to disk are completed. examples: kthreadd, migration, ksoftirqd
process ids
Process ID (PID) Unique Process ID number
Parent Process ID (PPID)
Process (Parent) that started this process. If the parent dies, the PPID will refer to an adoptive parent; on recent kernels, this is kthreadd which has PPID=2.
Thread ID (TID) Thread ID number. This is the same as the PID for single-threaded processes. For a multi-threaded process, each thread shares the same PID, but has a unique TID.
To terminate a process, you can type
kill -SIGKILL or kill -9 .
The operating system identifies the user who starts the process by the
Real User ID (RUID) assigned to the user.
The user who determines the access rights for the users is identified by the
Effective UID (EUID). The EUID may or may not be the same as the RUID.
Users can be categorized into various groups. Each group is identified by the {1}. The access rights of the group are determined by the {2}. Each user can be a member of one or more groups.
1) Real Group ID (RGID).
2) Effective Group ID (EGID).
The priority for a process can be set by specifying a
nice value, or niceness, for the process. The lower the nice value, the higher the priority.
In Linux, what represent highest and lowest priority
of -20 represents the highest priority and 19 represents the lowest
The load average can be viewed by running
w, top or uptime
Load average is the
average of the load number for a given period of time
Assuming our system is a single-CPU system, the three load average numbers 0.45, 0.17, 0.12 are interpreted as follows
- 45: For the last minute the system has been 45% utilized on average.
- 17: For the last 5 minutes utilization has been 17%.
- 12: For the last 15 minutes utilization has been 12%.
You can put a job in the background by
suffixing & to the command, for example: updatedb &.
You can either use {1} to suspend a foreground job or {2} to terminate a foreground job and can always use the {3} and {4} commands to run a process in the background and foreground, respectively.
1) CTRL-Z
2) CTRL-C
3) bg
4) fg
The {1} utility displays all jobs running in background.
jobs
{1} provides the same information as jobs, including the PID of the background jobs.
jobs -l
{1} provides information about currently running processes keyed by PID
ps
displays all the processes in the system in full detail.
ps -ef
show processes in realtime with updates
top, htop, atop
displays the processes running on the system in the form of a tree diagram showing the relationship between a process and its parent process and any other processes that it created.
pstree
The first line of the top output displays a quick summary of what is happening in the system, including
- How long the system has been up
- How many users are logged on
- What is the load average.
The second line of the top output displays
- the total number of processes,
- the number of running, sleeping, stopped, and zombie processes.
The third line of the top output indicates
how the CPU time is being divided between the users (us) and the kernel (sy) by displaying the percentage of CPU time used for each.
The percentage of user jobs running at a lower priority (niceness - ni) is then listed. Idle mode (id) should be low if the load average is high, and vice versa. The percentage of jobs waiting (wa) for I/O is listed. Interrupts include the percentage of hardware (hi) vs. software interrupts (si). Steal time (st) is generally used with virtual machines, which has some of its idle CPU time taken for other uses.