Linux Interview Questions

Overview of Linux

What is Linux?

Linux is an open-source Unix-like operating system. It is a multi-user, multitasking, multithreaded, and multiprocessor operating system with strong security and stability features.

What is Virtual Memory in Linux?

In Linux, virtual memory is a technology that combines main memory (RAM) and disk space to be used together, extending the available memory of the system. It achieves this by converting part of physical memory into disk space; essentially, it moves less frequently used data from RAM to the hard drive to free up more memory for running programs.

The implementation of virtual memory relies on the operating system's memory management mechanisms and includes the following main functions:

  1. Memory Mapping: Virtual memory abstracts physical addresses between main memory and the disk into virtual addresses, mapping these virtual addresses to physical addresses through memory mapping technology.
  2. Paging: Main memory and disk space are divided into fixed-size pages, and a page table is used to record the mapping relationship between virtual addresses and physical addresses.
  3. Page Replacement: When main memory space becomes insufficient, virtual memory replaces part of the less frequently used data from main memory onto the disk to free up more space for running programs. If this data needs to be accessed again, it will be read back into main memory by the virtual memory system.

Through virtual memory technology, the Linux system can allocate a certain amount of virtual memory space for each process; even when physical memory is insufficient, it ensures the normal operation of processes. Additionally, virtual memory can cache frequently used data to improve system performance and response speed. However, since virtual memory requires frequent page replacement and I/O operations, this may impact overall system performance.

How to Check the Linux System Version?

You can use the following command to check the Linux system version:

$ cat /etc/os-release

Or

$ lsb_release -a

How to Check Hardware Information of a Linux System?

You can use the following commands to check hardware information in a Linux system:

$ lshw

Or

$ lspci

How to Check Memory Usage in a Linux System?

You can use the following command to check memory usage in a Linux system:

$ free -m

Or

$ top

How to Check Disk Usage in a Linux System?

You can use the following command to check disk usage in a Linux system:

$ df -h

Or

$ du -sh *

How to Restart Services in a Linux System?

You can use the following command to restart services in a Linux system:

$ sudo systemctl restart service_name

How to Create a New User in a Linux System?

You can use the following command to create a new user in a Linux system:

$ sudo adduser username

Then follow the prompts to enter your password and other information.

What is the Architecture of Linux?

  • User Space (User Space): User space includes user applications (User Applications) and the C library (C Library).
  • Kernel Space: Kernel space includes system call interfaces (System Call Interface), the kernel itself (Kernel), and architecture-dependent code (Architecture-Dependent Kernel Code).

What is the Linux Kernel?

The Linux kernel is the core part of the operating system, responsible for handling various tasks such as hardware management, memory management, process management, file system management, network management, etc. It provides an interface that allows user-space applications to interact with hardware devices and communicate with each other.

Files, Directories, Disks

What is a Linux File System?

An important concept in the Linux system is "everything is a file." In the Linux operating system, all resources managed by the OS—such as network interface cards, disk drives, printers, input/output devices, regular files, or directories—are viewed as files.

Linux supports the following five types of files:

  • Regular Files: Used to store information and data on auxiliary storage devices (like disks).
  • Directory Files: Used to represent and manage files in the system; directory files contain file names and subdirectory names.
  • Link Files: Used for sharing files across different directories.
  • Device Files: Used to access hardware devices.
  • Command Pipes: A special type of file that allows processes within a Linux system to communicate through this file.

What is an inode in Linux?

In the Linux file system, an inode (index node) is a data structure used to store metadata information about files or directories. Metadata includes the owner, permissions, timestamps, size, and pointers to file data blocks of the file.

Every file or directory has a unique inode number used to identify that specific file or directory. In the file system, the filename serves as an alias for the inode number; therefore, multiple hard links can be created for the same file, which are logically identical because they all point to the same inode number. Hard links simply add different filenames to the same inode number.

The inode data structure typically includes the following information:

  • File type
  • File permissions
  • File owner
  • File size
  • Timestamps (including creation time, access time, and modification time)
  • Pointers to file data blocks

You can use the command ls -i to view a file's inode number. In the Linux system, inodes play a very important role in the file system; they enable efficient storage and management of files while supporting various advanced features such as hard links, snapshots, compression, etc.

In Linux, a link (link) is a mechanism that associates filenames with data blocks. Linux supports two types of links: symbolic links (soft links) and hard links.

  1. Soft Link: Also known as a symbolic link, it is a special type of file that points to another file. A soft link is essentially a text file containing the path name of the target file; this text file acts as an indicator pointing to the actual data. Soft links can be created using the ln -s command.

    Characteristics of Soft Links:

    • They can span across different file systems, meaning they can point to files in another file system.
    • They can point to directories.
    • They can point to non-existent files or multiple different files.
    • Deleting a soft link does not affect the data of the original file; however, if the link is deleted (broken), it becomes inaccessible until recreated.
  2. Hard Link: A hard link is a mechanism where multiple filenames within the same file system point to the same set of data blocks. Once created, both the filename and the original file share the same inode number; therefore, they are logically identical files. Hard links can be created using the ln command.

    Characteristics of Hard Links:

    • They can only be created within the same file system and cannot span across different file systems.
    • They cannot point to directories; they must point to regular files.
    • They must point to an existing file; they cannot point to non-existent files.
    • Deleting a hard link does not affect the data of the original file because both share the same underlying data blocks.

How to View Log Files in a Linux System?

You can use the following command to view log files in a Linux system:

$ tail -f /var/log/syslog

Or

$ less /var/log/messages

How to Install Software Packages in a Linux System?

You can use the following commands to install software packages in a Linux system:

$ sudo apt-get install package_name

Or (for RHEL/CentOS based systems):

$ sudo yum install package_name

How to Search for Files in a Linux System?

You can use the following command to search for files in a Linux system:

$ find /path/to/search -name "filename"