Continuing from the previous set, here are more essential Linux questions and topics that commonly arise in interviews, with practical use cases, troubleshooting tips, and best practices.
Q25: How do you declare a variable in a shell script?
Answer:
# Basic variable declaration
name="John"
count=42
# Array declaration
declare -a array=("one" "two" "three")
declare -A map=([key1]="value1" [key2]="value2")
Common Use Cases:
# Configuration variables
DB_HOST="localhost"
DB_PORT=3306
# Dynamic variables
timestamp=$(date +%Y%m%d)
user_count=$(who | wc -l)
Q26: What do $?, $#, and $ represent in shell scripting?
Answer:
Special parameters in shell:
$? # Exit status of last command (0=success)
$# # Number of arguments passed to script
$ # Current process ID
$0 # Script name
$1 # First argument
$@ # All arguments as separate strings
Practical Examples:
# Error handling
if [ $? -ne 0 ]; then
echo "Previous command failed"
exit 1
fi
# Argument validation
if [ $# -lt 2 ]; then
echo "Usage: $0 <source> <destination>"
exit 1
fi
Q27: How do you read command line input in a shell script?
Answer:
# Basic input
read name
echo "Hello, $name"
# With prompt
read -p "Enter username: " username
# Secure password input
read -s -p "Password: " password
Interactive Script Example:
#!/bin/bash
read -p "Database name: " db_name
read -s -p "Password: " db_pass
echo
mysql -u admin -p"$db_pass" "$db_name"
Q28: What is umask in Linux?
Answer:
Sets default permissions for new files/directories:
# View current umask
umask
# Set new umask
umask 022 # Files=644, Dirs=755
umask 027 # Files=640, Dirs=750
Common Applications:
# For secure environments
umask 077 # Private files only
# In .bashrc for permanent setting
if [ "$UID" -gt 199 ]; then
umask 002
else
umask 022
fi
Gotchas: Misconfigured umask
can cause security risks by making files too accessible.
Q29: How do you change file permissions in Linux?
Answer:
# Symbolic mode
chmod u+x file # Add execute for user
chmod g-w file # Remove write for group
chmod o= file # No permissions for others
# Octal mode
chmod 755 file # rwxr-xr-x
chmod 600 file # rw-------
Security Best Practices:
# Secure configuration files
chmod 600 ~/.ssh/config
chmod 644 public_html/*
find . -type d -exec chmod 755 {} \; # Directories
Q30: How can you connect to remote servers without a password?
Answer:
Using SSH key-based authentication:
# Generate SSH key pair
ssh-keygen -t rsa -b 4096
# Copy public key to server
ssh-copy-id user@remote-server
# Alternative manual method
cat ~/.ssh/id_rsa.pub | ssh user@remote-server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Automation Use Case:
# In scripts for automated deployments
#!/bin/bash
for server in $(cat servers.txt); do
ssh-copy-id -i ~/.ssh/deploy_key user@$server
done
Q31: How do you open a file in read-only mode in the vi editor?
Answer:
# Method 1
view filename
# Method 2
vi -R filename
# Method 3
vim -r filename
Practical Applications:
# Safely viewing logs
sudo view /var/log/syslog
# Checking configuration
view /etc/nginx/nginx.conf
Q32: What is the purpose of the export command in Linux?
Answer:
Makes variables available to child processes:
# Export variable
export PATH=$PATH:/new/path
export DEBUG=true
# List exported variables
export -p
Common Use Cases:
# In .bashrc/.profile
export JAVA_HOME=/usr/lib/jvm/java-11
export NODE_ENV=production
# Temporary environment setup
export PGHOST=localhost
export PGUSER=admin
Gotchas: Be mindful of modifying critical variables like PATH
.
Q33: How do you send error logs and stdout logs to different files?
Answer:
# Redirect stdout and stderr separately
command > output.log 2> error.log
# Both to same file
command &> combined.log
# Append mode
command >> output.log 2>> error.log
Real-world Example:
#!/bin/bash
# Script with comprehensive logging
./backup.sh > >(tee -a backup.log) 2> >(tee -a error.log >&2)
Q34: What is the nohup command in Linux?
Answer:
Runs processes immune to hangups:
nohup command &
nohup ./script.sh > output.log 2>&1 &
Common Applications:
# Long-running processes
nohup ./backup.sh &
# With custom output
nohup ./process.sh > /dev/null 2>&1 &
Use Case: Ideal for running scripts that should survive logout.
Gotchas: nohup
creates a nohup.out
file for output by default.
Q35: What does the netstat command do in Linux?
Answer:
Network statistics and connections:
netstat -tuln # TCP/UDP listening ports
netstat -anp # All connections with PID
netstat -s # Protocol statistics
Monitoring Examples:
# Monitor TCP connections
watch -n 1 'netstat -t'
# Check service availability
netstat -nlp | grep :80
Gotchas: netstat
is deprecated in favor of ss
on many distributions.
Q36: How do you run a script at boot level in Linux?
Answer:
Multiple methods available:
# Using systemd
sudo systemctl enable myservice
# Using rc.local
echo "/path/to/script.sh" >> /etc/rc.local
# Using crontab
@reboot /path/to/script.sh
Service Configuration Example:
# /etc/systemd/system/myscript.service
[Unit]
Description=My Boot Script
After=network.target
[Service]
ExecStart=/path/to/script.sh
Type=simple
[Install]
WantedBy=multi-user.target
Advanced Troubleshooting Matrix
Issue | Command | Solution |
High CPU | top , htop | Identify and kill resource-heavy processes |
Memory Leak | free -h , smem | Monitor memory usage patterns |
Disk Space | df -h , du -sh | Clean up large files/old logs |
Network | netstat , ss | Check connections and port status |
Best Practices Summary
- Always use version control for scripts
- Implement proper error handling
- Document your configurations
- Regular security audits
- Maintain backup strategies
Common Pitfalls to Avoid
- Not testing scripts in dev environment
- Forgetting to handle script errors
- Neglecting file permissions
- Poor password management
- Insufficient logging
Remember: Linux administration is about understanding both the individual commands and how they work together in a system context.
Hope this will help anyone that comes across this blog. I will do something like that for DevOps maybe in the future.