Linux viva questions set2

11. What are Linux file permissions and how do you modify them?
Linux file permissions control access to files and directories using three levels:
  • User (owner): Permissions for the file owner
  • Group: Permissions for members of the file's group
  • Others: Permissions for all other users
Three permission types:
  • r (read): 4
  • w (write): 2
  • x (execute): 1
Modify permissions using:
chmod 755 filename  # rwxr-xr-x
chmod u+x script.sh  # Add execute for owner
chmod g-w file.txt   # Remove write for group
View permissions with ls -l. Special permissions include:
  • SetUID (4000): Execute as owner
  • SetGID (2000): Execute as group
  • Sticky bit (1000): Restrict file deletion
12. Explain the use of grep, awk, and sed with examples.
These are powerful text processing tools:

grep (Global Regular Expression Print):

grep "error" log.txt        # Search for lines containing "error"
grep -i "warning" file.log # Case-insensitive search
grep -r "main" ./src/      # Recursive search

awk (Aho-Weinberger-Kernighan):

awk '{print $1}' data.txt    # Print first column
awk '/pattern/ {print $3}' file # Print 3rd column of matching lines
awk -F: '{print $1}' /etc/passwd # Use colon as delimiter

sed (Stream Editor):

sed 's/old/new/g' file.txt # Replace all occurrences
sed '/pattern/d' file    # Delete matching lines
sed -i '5d' data.txt     # Delete 5th line (in-place)
These tools are often combined in pipelines for complex text processing.
13. What are environment variables and how are they used in shell scripting?
Environment variables are dynamic values that affect processes:
  • Common variables:
    • PATH: Directories to search for executables
    • HOME: User's home directory
    • USER: Current username
  • View variables: printenv or env
  • Set variables:
    export VAR=value    # Available to child processes
    VAR=value command  # Only for this command
  • Use in scripts:
    #!/bin/bash
    echo "User: $USER"
    echo "Path: $PATH"
Important considerations:
  • Variables are case-sensitive
  • Use unset VAR to remove
  • Persistent variables go in ~/.bashrc or /etc/environment
14. How do you schedule tasks in Linux using cron?
The cron daemon executes scheduled tasks:

Basic usage:

crontab -e  # Edit user's cron jobs
crontab -l  # List current jobs

Cron syntax (minute hour day month weekday command):

# Run at 3:15 AM daily
15 3 * * * /path/to/script.sh

# Run every Monday at 5 PM
0 17 * * 1 /usr/bin/backup

# Run every 10 minutes
*/10 * * * * /home/user/check_status

System-wide crontab: /etc/crontab

Special directories:

  • /etc/cron.hourly
  • /etc/cron.daily
  • /etc/cron.weekly
Logs are typically in /var/log/syslog or /var/log/cron.
15. What is the difference between #!/bin/bash and #!/bin/sh?
These are different shell interpreters:
#!/bin/bash #!/bin/sh
Bourne-Again Shell (Bash) Bourne Shell or compatible
More features and extensions Basic POSIX-compliant features
Supports arrays, [[ ]], {1..10} Limited to POSIX standard
Better for interactive use Better for portability
Key considerations:
  • On most systems, /bin/sh is symlinked to bash or dash
  • Use bash for advanced scripting features
  • Use sh for maximum compatibility
  • Check with ls -l /bin/sh
16. Explain process states in Linux.
Linux processes can be in these states:
  • Running (R): Currently executing or ready to run
  • Sleeping (S): Waiting for an event (interruptible)
  • Uninterruptible Sleep (D): Waiting for I/O (cannot be killed)
  • Stopped (T): Suspended by signal (SIGSTOP, SIGTSTP)
  • Zombie (Z): Terminated but not reaped by parent
View states with:
ps aux    # STAT column shows state
top       # Interactive process viewer
htop      # Enhanced version of top
Important notes:
  • Zombies consume minimal resources but indicate programming errors
  • D-state processes often indicate hardware issues
  • States can be combined with flags like S+ (sleeping, foreground)
17. What are pipes and redirections in Linux?
These are methods for controlling input/output:

Pipes (|): Connect output of one command to input of another

ls -l | grep ".txt"    # Find text files
ps aux | sort -nk 4   # Sort processes by memory

Redirections:

command > file    # Overwrite file with output
command >> file   # Append to file
command < file    # Use file as input
command 2> error.log # Redirect stderr

Advanced techniques:

command > file 2>&1       # Redirect both stdout and stderr
command | tee output.log # Display and log output
command1 |& command2     # Pipe both stdout and stderr
These features enable powerful command combinations and logging.
18. How do you debug a shell script?
Effective shell script debugging techniques:
  • Syntax checking:
    bash -n script.sh  # Check syntax without executing
  • Tracing execution:
    bash -x script.sh  # Print each command before execution
    set -x            # Enable tracing within script
    set +x            # Disable tracing
  • Verbose mode:
    bash -v script.sh  # Print commands as read
  • Debugging tools:
    • echo statements for variable inspection
    • trap for handling signals and errors
    • PS4 variable to customize debug output
  • Error handling:
    set -e    # Exit on error
    set -u    # Treat unset variables as error
    set -o pipefail  # Catch pipeline errors
19. What are Linux signals and how are they used?
Signals are software interrupts delivered to processes:
Signal Number Purpose
SIGHUP 1 Hangup (reload configuration)
SIGINT 2 Interrupt (Ctrl+C)
SIGQUIT 3 Quit with core dump
SIGKILL 9 Forceful termination (cannot be caught)
SIGTERM 15 Polite termination request
SIGSTOP 19 Pause process (cannot be caught)
Usage examples:
kill -9 PID       # Force kill process
kill -HUP PID    # Reload process
trap "cleanup" SIGINT  # Handle Ctrl+C
Signals enable process control and inter-process communication.
20. Explain the Linux boot process from BIOS to login prompt.
The Linux boot sequence involves these stages:
  1. BIOS/UEFI:
    • Performs hardware initialization
    • Locates bootable device
    • Loads and executes bootloader
  2. Bootloader (GRUB):
    • Displays boot menu
    • Loads kernel and initramfs
    • Passes control to kernel
  3. Kernel Initialization:
    • Mounts root filesystem
    • Starts init process (PID 1)
    • Systemd (or SysV init) takes over
  4. System Initialization:
    • Mounts filesystems
    • Starts system services
    • Runs startup scripts
  5. Login Prompt:
    • getty or display manager starts
    • User authentication
    • Shell or desktop environment loads
Key files:
  • /boot/grub/grub.cfg: GRUB configuration
  • /etc/fstab: Filesystem mount points
  • /etc/inittab (SysV) or /etc/systemd (systemd)

Comments

Popular posts from this blog

Analysis of algorithms viva questions / Interview questions - set1 /sorting algorithms

Operating System Viva questions/interview questions 2025

Recommendation System viva questions/ Interview questions