Welcome to the ultimate guide to Linux questions and answers! 🐧💻 Whether you're new to the world of Linux or an experienced user, this blog post has something for everyone. We're diving into everything from basic commands to advanced topics, providing you with the knowledge you need to master the Linux operating system.
🌱 Starting with the Basics: If you're just getting started with Linux, don't worry! We'll cover essential commands, file management, and navigation tips to help you feel at home in the terminal.
🌟 Leveling Up: For those with some Linux experience, we've got you covered with intermediate topics, including shell scripting, package management, and system monitoring.
🔥 Going Advanced: Ready to push your Linux skills to the limit? Explore advanced topics like kernel customization, networking, and security.
🚀No matter where you are on your Linux journey, this guide will be your companion. So grab your terminal, put on your penguin hat, and let's dive into the world of Linux!
🐧**Happy Learning!!**
What is Linux
Linux is an open-source kernel developed by Linus Torvalds in 1991. It is the core component of many Linux distributions, providing the foundation for various operating systems.
\=============================================================
What is a distribution (distro) in Linux?
A Linux distribution (or distro) is a version of the Linux operating system that includes the Linux kernel and additional software such as system utilities, libraries, and applications. Examples include Ubuntu, Fedora, and Debian
\=============================================================
How do you access the command line in Linux?
The command line interface (CLI) in Linux is accessed through a terminal emulator. Common terminal emulators include GNOME Terminal, Konsole, and xterm.
\=============================================================
What is the root user in Linux?
The root user, also known as the superuser, has administrative privileges and can perform tasks that regular users cannot, such as installing software, modifying system configurations, and managing files owned by other users
\=============================================================
What is a shell in Linux?
A shell is a command interpreter that allows users to interact with the operating system by executing commands. Common shells in Linux include Bash (Bourne Again Shell), Zsh (Z Shell), and tcsh (TENEX C Shell).
\=============================================================
What is the use of echo command?
echo is a command that outputs the strings that are passed to it as arguments
Eg: echo Hello world
\=============================================================
How to check the host-name/computer name in Linux?
Using “hostname” command
\=============================================================
How to check current user in Linux terminal
“whoami” shows the current user
\=============================================================
How to check your current path/directory you are working on?
Using “pwd”
\=============================================================
How to rename a file in linux
Using “mv” command
\=============================================================
What are the commands used to create a file in Linux?
we can use touch, vi, vim, nano, cat & echo
\=============================================================
How to search for a String in a file?
Using “grep” command
\=============================================================
What is the difference between grep and egrep?
egrep can be used to search for more than one strings at same time
\=============================================================
How can you read a file without using cat command?
Using “less,more,vi & nano”
\=============================================================
What is the advantage of using less command?
we can easily read big files, forward and backward search is easy. Navigation from top to bottom is easy
\=============================================================
How to check a file’s permission?
Using “ ls –l “ or “ll” or “getfacl”
\===========================================================
How to check the ip of your Linux server?
Using “ipconfig” or ip addr”
\=============================================================
How to read the top 5 lines in a file?
head -5 filename
\============================================================
How to read the last 5 lines in a file?
tail -5 filename
\=============================================================
How to list hidden files?
Using “ls –a”
\=============================================================
How to see all the recently used commands?
Using “history” command
\=============================================================
What is root?
Admin or super user, /root is the home directory for root user
\=============================================================
What is i node and how to find it for a file?
Using “ls –li”
inode is an index node. It saves as a unique identifier for a specific piece of metadata on a file system
\=============================================================
Which command can you use for finding files on a Linux system?
Using find and locate command
The find command is a versatile tool for searching for files and directories recursively in a directory hierarchy. It allows you to specify various search criteria such as file name, file type, file size, permissions, etc.
The locate command is used to quickly find files by name by searching an index database of the entire file system. It's generally faster than find, but it relies on a pre-built index, so it might not give you the most up-to-date results.
\=============================================================
What are the commands for counting words & lines?
Using “wc” and “wc –l”
\=============================================================
How can you combine two commands or what is pipe symbol used for?
Using | sign
pipe is used to combine two commands and redirect output of command1 to command2
\=============================================================
How to view the difference between two files?
diff file1 file2 can be used
\=============================================================
What is the use of shred command?
Permanently delete a file which is unable to recover
shred –u filename
shred –remove filename
\=============================================================
How to check system architecture info?
Using demidecode and lscpu
dmidecode
stands for Desktop Management Interface decode. It reads the system DMI (Desktop Management Interface) table to display hardware information of the system. This includes details about the BIOS, motherboard, CPU, memory, and other hardware components.
lscpu
is a command used to display information about the CPU architecture. It provides information such as CPU architecture, number of CPU's, CPU family, CPU model, CPU flags, and more.
\=============================================================
How to combine two files and redirect it to a third file?
Can be done using “cat” command
\=============================================================
How can you find the type of a file?
file filename
\=============================================================
How to sort the content of a file?
sort filename
\=============================================================
What are the different ways to access a Linux server remotely from a windows machine?
Using some tools and terminals like putty, git bash, mobaxterm, cmd etc
\=============================================================
What are the different types of permissions for a file in Linux?
read(r), write(w), executable(x)
\=============================================================
Which permission allow a user to run an executable file?
X permission
\=============================================================
How to write the output of a command in to a file?
command > filename
\=============================================================
How to add content in a file without deleting the existing content?
we can append the file using >> symbol
\=============================================================
How to automate any task or script?
Using cron jobs, for which we have crontab and at command
\=============================================================
How to check scheduled jobs?
Using crontab –l
To schedule , crontab –e
\=============================================================
If your cron job didn’t work, how would you check?
Verify file system
Verify Cron Job Configuration:
Check Permissions:
Check system load
verify the command manually
Restart cron service
Verify /var/log/messages
\=============================================================
What is a daemon service?
The service that keeps running in background
eg: httpd, sshd, chronyd
\=============================================================
How to check if a system is running or not?
systemctl status servicename
\=============================================================
How to check for free disk space?
Using “df –h”
\=============================================================
How to check the size of a directory’s content?
Using “du –h”
\=============================================================
How to check cpu usage for a process?
Using “top” command
\=============================================================What is a process in Linux?
An instance of a running program.
Whenever you start a program or execute a command a process is created.
For every process a unique no. is assigned which is called PID(process ID).
\=============================================================
How to terminate/stop a running process?
Using kill command
kill -9 PID
kill with no signal specified (SIGTERM) allows the process to gracefully shut down, while kill -9 (SIGKILL) forcefully terminates the process without giving it a chance to clean up
\=============================================================
How to check if a ip server is accessible or not?
we can use ping or telnet command
The ping command is used to test the reachability of a host on an IP network and to measure the round-trip time for packets sent from the originating host to a destination computer.
The telnet command is used to establish a connection to a remote host or to check if a specific port is open on a remote host.
telnet example.com 80
example.com is the domain name or IP address of the remote server.
80 is the port number typically associated with HTTP, the protocol used for serving web pages.
\============================================================
Which command is used to get info about ports?
Using netstat, -> netstat –tln
\=============================================================
How to check open port on Linux system?
The command netstat -putan | grep portno is used to filter the output of netstat to show only the lines containing a specific port number (portno). Let's break down the command:
netstat: This command is used to display network-related information such as active connections, listening ports, routing tables, etc.
-putan: These are options passed to netstat:
-p: Show the PID and name of the program to which each socket belongs.
-u: Show UDP sockets.
-t: Show TCP sockets.
-a: Show all sockets (both listening and non-listening).
-n: Show numerical addresses instead of resolving hostnames.
|: This is a pipe operator, used to redirect the output of one command as input to another command.
grep portno: This command filters the input based on the pattern provided. In this case, it filters the output of netstat to show only the lines containing the specified port number (portno).
\=============================================================
What are the differences between telnet and ssh?
Telnet:
Unencrypted protocol for remote terminal access.
Transmits data, including credentials, in plaintext.
Vulnerable to eavesdropping and interception attacks.
Typically uses port 23.
Offers basic authentication but lacks security over untrusted networks.
SSH (Secure Shell):
Secure, encrypted protocol for remote access.
Encrypts all data, providing protection against eavesdropping.
Supports various authentication methods, including public-key authentication.
Typically uses port 22.
Widely preferred for secure remote administration over untrusted networks.
\=============================================================
Which server should be running on server to allow you to connect remotely?
Using ssh or sshd
\=============================================================
What is ssh and why it is called as secure shell?
ssh is a network communication protocol that enables computer devices to communicate and share data.
It is called as secured shell as the communication between the host and client will be in encrypted format,
\=============================================================What is RHEL Linux stands for ?
Red hat enterprise Linux operating system
\=============================================================
Give some examples of Linux distributions?
Redhat,Debain,Fedora,kali linux,Ubuntu, centos etc
\=============================================================
What is Kernel?
The linux kernel is the main component of a linux OS and is the core interface between a computers hardware and its processes.
\=============================================================
What is the use of scp command?
scp is used to copy files/directories between a local and a remote system or between 2 remote systems, scp uses ssh for data transfer.
SCP, on the other hand, uses SSH for encryption and authentication, providing a more secure way to transfer files over a network
scp [options] [source] [destination]
scp /path/to/local/file.txt user@remote_host:/path/to/remote/directory/
\=============================================================
What is the use of FTP command?
FTP is the file transfer protocol to exchange files to and from a remote computer or network.
ftp remote_host
ftp > put localfile.txt
When using FTP, it's important to remember that the communication is not encrypted, so sensitive information like usernames and passwords are sent in plaintext
\=============================================================
What is alias and how to setup?
alias is used to create shortcut to the command or task.
alias new_command='original_command'
alias ll='ls -l'
Aliases are handy for creating custom shortcuts for commonly used commands, but they should be used judiciously to avoid confusion and conflicts with existing commands or system utilities.
It's a good practice to choose alias names that are intuitive and not likely to be confused with other commands.
\=============================================================
Difference between update and upgrade?
Update: Updates all the presently installed packages to their latest versions that are available in the repositories
Upgrade: Performs the same action as update, but once finished it also removes all the obsolete packages from the system.
\==========================================================
Name the default ports used for DNS, SMTP, FTP, SSH, DHCP, http, https & Squid?
DNS – 53, SSH – 22, Http – 80, Https: 443, DHCP – 67,68, Squid – 3128, SMTP – 25, FTP – 21
\=============================================================
What is the command that exactly sends three ping packets to google.com?
ping –c3 google.com
\=============================================================
Which file contains a list of group names and GIDS?
/etc/group
\=============================================================
Which of the following contains the value of the exit status of the previously executed command?
$?
Let's say you run a command and want to check if it was successful or not:
ls /path/to/nonexistent/directory
if [ $? -eq 0 ]; then
echo "Command executed successfully."
else
echo "Command encountered an error."
fi
In this example:
ls /path/to/nonexistent/directory attempts to list the contents of a nonexistent directory.
[ $? -eq 0 ] checks if the exit status of the previous command is equal to 0 (indicating success).
Depending on the exit status, the script prints either "Command executed successfully." or "Command encountered an error."
Using $? allows you to handle errors gracefully and perform appropriate actions based on the success or failure of commands in your scripts or shell sessions.
\=============================================================Which command displays the number and type of processors used by a Linux system?
cat /proc/cpuinfo
lscpu
\============================================================
which command is used to get a list of connected USB devices?
Using “lsusb”
\=============================================================
What are the valid values of the exit status?
0 – 255 , 0 – success
\=============================================================
According to the FHS , which is the correct directory for log files?
/var/log
\=============================================================
What port must be open to ping a given host?
Ping doesn’t use any port
\=============================================================
How will you transfer file from a linux server to your windows OS and vice-versa?
Using SSH, FTP, SFTP & tools like winscp, filezilla.
\=============================================================
Can you split a file in to two? If yes, how?
split -n filename
This command will split the file filename into two parts. By default, split will use alphabetical suffixes for the output files (xaa, xab, etc.) to indicate the order of the split parts. You can also specify a prefix for the output files using the -a option.
For example, if filename is example.txt, the command will generate two output files: xaa and xab, each containing half of the contents of filename
\=============================================================
How can you get the unique values from a list?
sort filename | uniq -> the data should be sorted before using uniq command
\=============================================================
what is tee command used for ?
The tee command is used to read from standard input and write to standard output and one or more files simultaneously. It allows you to redirect the output of a command to a file while also displaying it on the terminal.
Here the output of ls command is redirected to file1.txt file by displaying the results at same time.
\=============================================================
What is Xargs used for?
It converts the standard input in to command line argument
cat file1.txt | xargs rm
In this example:
cat file1.txt outputs the contents of filelist.txt to standard output.
xargs rm reads each filename from standard input and executes the rm command for each filename, effectively deleting all the files listed in filelist.txt.
\=============================================================
I want to read only line 25 to 30th line , how you will do it?
head -30 filename | tail -5
head -30 filename: This command displays the first 30 lines of the file named filename.
tail -5: This command displays the last 5 lines of its input.
When you combine them with the pipe (|) operator, it means "take the output of head -30 filename and feed it as input to tail -5".
\=============================================================
What is the use of tar command?
Compress a group of files in to a archive , combines multiple files or directories together in to a single file.
\=============================================================
How to redirect both standard o/p and error to a file?
command > file 2>&1
command: This represents the command whose output we want to redirect.
\>: This is the output redirection operator, which directs the standard output of the command to the specified file.
file: This is the name of the file where we want to redirect the output.
2>&1: This part redirects the standard error (file descriptor 2) to the same location as standard output (file descriptor 1).
So, when you execute command > file 2>&1, both the standard output and standard error of command will be written to the file named file
\=============================================================
Name some commands for DNS resolution?
ping, nslookup, dig, host etc
\=============================================================
What is the use of “at” command?
at command is used to schedule a task once.
at -f script.sh 10:00 PM
This will schedule the execution of the commands in the file script.sh at 10:00 PM.
\=============================================================
Which command syntax is correct if you want to assign read only permission to everyone and write permission to the owner of the file?
chmod u+w, chmod a+r
\=============================================================
Which command changes the group ownership of file “/var/www” directory to the “nginx” group?
chgrp nginx /var/www
\=============================================================
what is chmod 777 filename do?
777 means provide all the permissions rwx to users, groups & other users also
\=============================================================
what is ACL and its advantages?
Access control List is used to modify the permissions of files, for this we use setfacl and getfacl commands.
Advantages:
ACLs offer finer control over access permissions than traditional Unix permissions, enabling administrators to assign specific permissions to individual users or groups. This granularity allows for more tailored access control, reducing the risk of unauthorized access. Additionally, ACLs support multiple levels of access and delegation of access control tasks, leading to improved security and reduced administrative overhead in complex environments
\=============================================================What are the links in Linux and its types?
A connection between a filename & the actual data on the disk. We can call it a shortcut
Hard link: Hard link is just a backup of any file, whenever the file is updated in future it will update the backup file as well and vice-versa.
eg: ln file2 backupfile2
Softlink: Softlink is used to create a shortcut, even if we delete the softlink file the original file will not be effected, once original file gets deleted, the softlink file will become useless.
eg: ln –s file1 softfile1
\=============================================================
What command you can use to see all the env variables?
Using “env” or “printenv”
\=============================================================
What command allows you to display all the process IDS of the running nginx processes
Using pidof httpd
\=============================================================
What is nice value of a process?
niceness scale goes from -20 to 19. The lower the number more priority that task gets
nice –n5 process
\=============================================================
How can you set env variables and the different ways to set it in Linux?
Exporting Variables in the Shell: Use export VARIABLE_NAME=value to set variables for the current shell session only.
Setting Variables in Shell Configuration Files: Set variables in .bashrc, .bash_profile, .profile, .zshrc, etc., for persistent availability across shell sessions.
Using setenv in C Shell (csh or tcsh): In C shell, use setenv VARIABLE_NAME value to set environment variables.
Using export Command in Shell Script: Set variables within a shell script using export VARIABLE_NAME=value for script-specific environment variables.
Using /etc/environment File: Set system-wide environment variables in /etc/environment for availability to all users and processes on the system.
\============================================================
You want a process with a PID of 8657 to complete its task faster than the default rate, which command should you run to accomplish this goal?
renice -20 –p 8657
this can be done by top command as well.
\=============================================================
If you want to run a task/script in background even if you close terminal?
nohup process &
nohup process > /dev/null 2>&/&
\=============================================================
How to view all the active jobs?
Using “jobs” command
\=============================================================How to resume a stopped job in background or foreground?
fg %job_id
bg %job_id
Replace %job_id with the job ID of the stopped job you want to resume.
\=============================================================
Which command displays info about kernel-related messages along with hardware and system startup messages in kernel ring buffer?
Using dmesg
\============================================================
In a directory you have a combination of files like yml, xml, logs etc, how you will get see only xml files?
ls –l .xml
\=============================================================
*Create 100 files with naming file1, file2, ------ file 100?
touch file{ 1---100 }
\=============================================================
How would you display all the files in the present working directory that start with either the letter “c” or “m” ?
ls [cm]
\=============================================================
Which of the following commands lists all files and directories except those beginning with letter “e”?
ls –l | grep –v ^a
\=============================================================
Run a command that shows all the lines except any lines starting with character # in a file?
cat file1 | grep –v ^#
\=============================================================
*Which of the following bash command produces the o/p of “bat bet bit bot” ?
echo b{a,e,i,o}t
\=============================================================
What is a vm and hypervisor ?
Virtual machine is a virtual env that functions as a virtual computer system with its own cpu, memory, network interface, created on a physical hardware system.
Hypervisor is a software that creates and runs virtual machines.
It is of 2 types: Type2 (Hosted), Type1 or bare metal hypervisors
\=============================================================
Which command can you use to display a list of currently configured volume groups?
vgs
\=============================================================
Which command displays memory usage, including the amount of swap space being used?
Using “free” command
\=============================================================
How can you create a tar archive of a directory with all permissions intact?
You can use the tar command with the -p option to preserve permissions and ownership. For example: tar -cvpf backup.tar directory_to_backup
\=============================================================
How to check linux file system?
Using lsblk
\=============================================================
You want to set up a cron job to execute a script every day at 2:00 AM.How can you create a cron job for scheduling tasks?
You can edit the crontab file using the crontab -e command and add the following line:
0 2 * /path/to/your/script.sh
\=============================================================
You want to monitor system performance and resource utilization over time.Which command-line tool can you use to collect and analyze system performance data?
You can use the sar (System Activity Reporter) command to collect, report, and save system activity information such as CPU, memory, disk, and network usage.
sudo apt-get install sysstat
sar -o output_file 10 5
sar -f output_file
In this example, sar -o output_file 10 5 collects system activity data every 10 seconds for 5 times and saves it to a file named "output_file". Then, sar -f output_file is used to view the collected data.
\=============================================================
You want to create a symbolic link to a file or directory. How can you create a symbolic link in Linux?
You can use the ln command with the -s option followed by the target file or directory and the name of the symbolic link.
For example: ln -s /path/to/target /path/to/symlink
\=============================================================
You need to find files larger than a specific size on your system. How can you search for files larger than a certain size?
You can use the find command with the -size option to search for files based on their size. For example, to find files larger than 100MB:
find / -size +100M
\=============================================================