Explain how Linux mechanisms work, especially when the system starts ?
When a Linux system starts, it follows a well-defined sequence of steps, from power-on to running the user’s applications. Here’s an overview of how these mechanisms work during the Linux boot process:
1. BIOS/UEFI Initialization
- When the system powers on, the BIOS (Basic Input/Output System) or UEFI (Unified Extensible Firmware Interface) initializes the hardware components (e.g., CPU, memory, and disk) and performs a Power-On Self-Test (POST) to check for hardware integrity.
- The BIOS/UEFI then identifies the boot device (usually a hard drive) and loads the bootloader from the Master Boot Record (MBR) or GPT.
2. Bootloader (GRUB)
- The bootloader (commonly GRUB in Linux) is responsible for loading the Linux kernel into memory. GRUB may present a boot menu to choose from different kernel versions or operating systems.
- Once a kernel is selected, the bootloader transfers control to the kernel by loading it into memory along with an initial RAM disk (initrd) or initramfs, which contains temporary files needed by the kernel during early boot.
3. Kernel Initialization
- The Linux kernel is loaded into memory and begins initializing system components such as device drivers, memory management, and the scheduler.
- The kernel mounts the initial RAM disk (initrd or initramfs) to access essential files required for system initialization (e.g., modules, drivers).
- After loading these modules and configuring the hardware, the kernel mounts the root filesystem and starts the first process, called init (or systemd).
4. Init/Systemd
- The init system is responsible for launching all other user-space services and processes. In modern Linux systems, systemd has replaced the traditional SysVinit system.
- systemd works based on unit files, which define services, targets, and dependencies. It manages the startup sequence of services, ensuring that dependent services start in the right order.
- systemd initializes essential services like network management, logging, and user-space daemons.
5. Runlevels/Targets
- The system enters a runlevel (SysVinit) or target (systemd), which is a predefined state that specifies which services should be running. Common targets include:
- Multi-user.target: Multi-user mode without a graphical interface.
- Graphical.target: Multi-user mode with a graphical interface.
- Based on the target, systemd starts the appropriate services such as network services, databases, and other applications.
6. Login Prompt and Shell
- Once all the necessary services are up, the system presents a login prompt (for servers, usually a text-based terminal; for desktops, a graphical login screen).
- After successful login, the shell (e.g., bash) is started, allowing users to interact with the system and run applications.
Summary of Key Components:
- BIOS/UEFI: Initializes hardware and loads the bootloader.
- Bootloader (GRUB): Loads the Linux kernel into memory.
- Kernel: Initializes system components and hardware.
- Init/Systemd: Manages services and processes.
- Runlevels/Targets: Define the system’s operating mode.
- Login/GUI: Allows user interaction with the system.
How do you find all files modified in the last 7 days in the current directory?
find . -type f -mtime -7
How do you check ports in Linux ?
netstat -tuln
What are the main components of the Linux operating system?
The main components of the Linux operating system are:
- Kernel: The core part that manages hardware resources and system calls.
- Shell: Command-line interface for user interaction, allowing command entry (e.g., Bash, Zsh).
- File System: Organizes and manages data on storage devices (e.g., ext4, XFS).
- System Libraries: Pre-written code used by applications for common tasks (e.g., GNU C Library).
- User Interface: Includes graphical user interfaces (GUIs) like GNOME and KDE, as well as command-line usage.
- Utilities: Essential tools for system maintenance and file management (e.g.,
grep
,cron
). - Applications: Software programs that run on Linux (e.g., web browsers, office suites).
- Package Manager: Tool for installing, updating, and removing software packages (e.g., APT, YUM).
Explain the Linux file system hierarchy and what each directory is typically used for ?
The Linux file system hierarchy follows a standard structure known as the Filesystem Hierarchy Standard (FHS). Here’s a brief overview of key directories and their typical uses:
- / (Root): The top-level directory that contains all other directories.
- /bin: Contains essential user binaries (executables) needed for system boot and repair, such as commands for basic file manipulation and system management.
- /boot: Contains files required for the boot process, including the Linux kernel and initial RAM disk images.
- /dev: Contains device files that represent hardware devices (e.g., disks, terminals) used by the system.
- /etc: Holds system configuration files and scripts that control the system’s behavior. Examples include network configurations and user accounts.
- /home: The home directory for user accounts. Each user typically has a subdirectory here (e.g.,
/home/user1
). - /lib: Contains essential shared libraries and kernel modules needed by binaries in
/bin
and/sbin
. - /media: Mount point for removable media (e.g., USB drives, CDs). Devices are typically mounted here when accessed.
- /mnt: A generic mount point for temporary mounting of filesystems.
- /opt: Used for optional application software packages. Third-party software not included in the standard distribution may be installed here.
- /proc: A virtual filesystem that provides process and system information (e.g., running processes, system resources).
- /root: The home directory for the root user (the superuser).
- /sbin: Contains essential system binaries, primarily for system administration tasks, used by the root user.
- /srv: Contains data for services provided by the system, such as web or FTP servers.
- /tmp: Temporary files created by users and applications. This directory is often cleared on reboot.
- /usr: Contains user-related programs and data. It includes subdirectories like
/usr/bin
for user binaries,/usr/lib
for libraries, and/usr/share
for shared data. - /var: Contains variable data files, such as logs (
/var/log
), spool files for mail (/var/spool
), and temporary files.
What is the difference between a hard link and a soft link?
Hard links refer directly to the file’s data, can’t link to directories, and must reside in the same filesystem.
Soft links are pointers to file names, can link across filesystems, and can point to directories, but become invalid if the target file is deleted.
Explain the purpose of the /etc/passwd and /etc/shadow files ?
/etc/passwd: This file contains basic information about user accounts on the system.
/etc/shadow: This file securely stores the hashed passwords and additional information related to user authentication.
What is a kernel, and what role does it play in Linux?
The kernel is the core component of the Linux operating system that manages hardware and software interactions. Its key roles include:
- Resource Management: Allocates CPU, memory, and I/O devices efficiently.
- Process Management: Handles process creation, scheduling, and termination.
- Memory Management: Manages memory allocation and prevents interference between processes.
- Device Drivers: Interfaces with hardware, allowing software to communicate with devices.
- System Calls: Provides an interface for applications to request services from the kernel.
- Security: Enforces permissions and security measures for resource access.
How can you search for a specific file or directory on your system?
To search for a specific file or directory on your Linux system, you can use several commands. The find
command is versatile for searching files based on various criteria. For example, you can search for a file named example.txt
in the /home
directory with find /home -name "example.txt"
. Alternatively, the locate
command provides a faster search by referencing a pre-built database of file locations. Before using it, ensure the mlocate
package is installed and the database is updated with sudo updatedb
, then search using locate filename
, such as locate example.txt
. Lastly, you can also use grep
in combination with ls
to filter directory listings, like ls -R /path/to/directory | grep "filename"
, which allows you to see if a file exists within a specific directory structure. These commands offer effective ways to locate files and directories quickly in your system.
What is the purpose of the tar command, and how do you use it?
The tar
command in Linux is used for archiving files and directories. It combines multiple files into a single archive file, often to simplify storage, backup, or transfer. The tar
command can also compress these archives to save space.
Create an Archive: To create a compressed archive named backup.tar.gz
from the myfolder
directory: tar -czvf backup.tar.gz myfolder
Extract an Archive: To extract the contents of backup.tar.gz
: tar -xzvf backup.tar.gz
Explain the difference between SSH and Telnet. Why is SSH preferred over Telnet?
SSH (Secure Shell) and Telnet are both protocols used for remote communication with network devices and servers, but they have key differences:
1. Security:
- SSH: Provides a secure, encrypted connection, protecting data from eavesdropping and man-in-the-middle attacks. It uses encryption for both the session and authentication.
- Telnet: Transmits data in plain text, making it vulnerable to interception and attacks. Any sensitive information, like usernames and passwords, can be easily compromised.
2. Authentication:
- SSH: Supports various authentication methods, including password-based and public key authentication, enhancing security.
- Telnet: Primarily uses simple username and password authentication, which is less secure.
3. Functionality:
- SSH: Offers additional features such as secure file transfer (SCP, SFTP), port forwarding, and tunneling capabilities.
- Telnet: Mainly focuses on providing a terminal emulation service without any advanced features.
Why SSH is Preferred Over Telnet:
SSH is preferred over Telnet due to its strong security features, including encryption and secure authentication. As cyber threats increase, the need for secure communication is paramount, making SSH the go-to choice for remote access to servers and network devices. Using Telnet in modern environments poses significant security risks, particularly for sensitive data transmission. Therefore, SSH is widely adopted in both enterprise and personal use cases to ensure secure remote management.
How to check the list of installed packages?
yum list installed
apt list --intstalled
Explain the purpose of apt update and apt upgrade ?
apt update
: Fetches the latest package information from all configured sources (repositories).- apt upgrade: Upgrades all installed packages to the latest versions available in the repositories.
Password Policy in linux
A password policy in Linux refers to a set of rules that define the requirements for user passwords. This helps improve security by ensuring passwords are strong and regularly updated.
Key Aspects of Password Policy
- Minimum Password Length:
- Ensures passwords are of a certain minimum length.
- Example: At least 8 characters.
- Password Complexity:
- Requires a mix of uppercase, lowercase, numbers, and special characters.
- Password Expiry:
- Forces users to change passwords after a specified period.
- Password History:
- Prevents users from reusing old passwords.
- Lockout Policy:
- Locks the user account after multiple failed login attempts.
- Inactive Account Expiration:
- Automatically disables inactive accounts.
Implementing Password Policies in Linux
1. Set Password Length and Complexity
Use the /etc/security/pwquality.conf
file to define password rules.
- Edit the file:
sudo nano /etc/security/pwquality.conf
minlen = 12 # Minimum password length
minclass = 3 # At least 3 character classes (uppercase, lowercase, digits, symbols)
dcredit = -1 # Require at least 1 digit
ucredit = -1 # Require at least 1 uppercase letter
lcredit = -1 # Require at least 1 lowercase letter
ocredit = -1 # Require at least 1 special character
2. Enforce Password Expiry
Modify the /etc/login.defs
file:
- Edit:
sudo nano /etc/login.defs
- Configure:
PASS_MAX_DAYS 90 # Maximum days before password change is required PASS_MIN_DAYS 7 # Minimum days before a password can be changed PASS_WARN_AGE 7 # Warning period before password expiration
3. Prevent Password Reuse
Use the pam_unix.so
module in /etc/pam.d/common-password
:
- Add the following line:
password required pam_unix.so remember=5
- This prevents the last 5 passwords from being reused.
4. Lock Account After Failed Attempts
Configure the /etc/pam.d/common-auth
file:
- Add:
auth required pam_tally2.so onerr=fail deny=5 unlock_time=300
- Deny access after 5 failed attempts and unlock the account after 5 minutes.
5. Expire Inactive Accounts
Use the useradd
command or modify /etc/default/useradd
:
- Set inactivity period:
sudo chage -I 30 <username>
- This disables the account after 30 days of inactivity.
Diagram of Linux Architecture
+----------------------+
| User Applications |
+----------------------+
| System Utilities |
+----------------------+
| System Libraries |
+----------------------+
| Shell |
+----------------------+
| Kernel |
| (Process, Memory, |
| File, Network, |
| Device Drivers) |
+----------------------+
| Hardware |
+----------------------+