topics/linux/README.md
A completely free application for testing your knowledge on Linux. Disclaimer: developed by repository owner
<a href="https://play.google.com/store/apps/details?id=com.codingshell.linuxmaster"></a>
| Name | Topic | Objective & Instructions | Solution | Comments |
|---|---|---|---|---|
| Navigation | cd, pwd | Exercise | Solution | |
| Create and Destroy | touch, rm, mkdir | Exercise | Solution | |
| Copy Time | touch, cp, ls | Exercise | Solution |
| Name | Topic | Objective & Instructions | Solution | Comments |
|---|---|---|---|---|
| Unique Count | Exercise | Solution |
Wikipedia: "Linux is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged in a Linux distribution."
Red Hat: "Linux® is an open source operating system (OS). An operating system is the software that directly manages a system’s hardware and resources, like CPU, memory, and storage. The OS sits between applications and hardware and makes the connections between all of your software and the physical resources that do the work."
</b></details>
<details> <summary>Explain what each of the following commands does and give an example on how to use it:The -r (or -R in some commands) flag allows the user to run a certain command recursively. For example, listing all the files under the following tree is possible when done recursively (ls -R):
/dir1/ dir2/ file1 file2 dir3/ file3
To list all the files, one can run ls -R /dir1
</b></details>
These are files directly not displayed after performing a standard ls direct listing. An example of these files are .bashrc which are used to execute some scripts. Some also store configuration about services on your host like .KUBECONFIG. The command used to list them is, ls -a
</b></details>
myProgram < input.txt > executionOutput.txt
</b></details>
sed -i s/salad/burger/ggrep 'word' file.mdgrep -c 'This is a string' file.mdcut OPTION [FILE]
cut -b 1-2 file.md, output: woUsing the mv command.
</b></details>
rm -rf dircat or lesschmod 777 /tmp/xcd ~sed -i s/good/great/g /tmp/y
</b></details>echo hello world
echo "hello world"
The echo command receives two separate arguments in the first execution and in the second execution it gets one argument which is the string "hello world". The output will be the same. </b></details>
<details> <summary>Explain piping. How do you perform piping?</summary> <b>Using a pipe in Linux, allows you to send the output of one command to the input of another command. For example: cat /etc/services | wc -l
</b></details>
sed 's/1/2/g' /tmp/myFile # sed "s/1/2/g" is also fine
find . -iname "*.yaml" -exec sed -i "s/1/2/g" {} \;
</b></details>
<details> <summary>How to check which commands you executed in the past?</summary> <b>history command or .bash_history file
As to fix it there are several options:
Note: There are many ways of getting errors like this: if bash_profile or any configuration file of your interpreter was wrongly modified; causing erratics behaviours, permissions issues, bad compiled software (if you compiled it by yourself)... there is no answer that will be true 100% of the time. </b>
</p> </details> <details> <summary>How do you schedule tasks periodically?</summary> <b>You can use the commands <code>cron</code> and <code>at</code>. With cron, tasks are scheduled using the following format:
<code>*/30 * * * * bash myscript.sh</code> Executes the script every 30 minutes.
<minute> <hour> <day of month> <month> <day of week> <command to execute>
The tasks are stored in a cron file, you can write in it using <code>crontab -e</code>
Alternatively if you are using a distro with systemd it's recommended to use systemd timers. </b></details>
<a name="questions-linux-redirection"></a>
Here are some common examples of IO redirection:
<code>ls > ls_output.txt</code> </b></details>
<details> <summary>Demonstrate Linux stderr output redirection</summary> <b><code>yippiekaiyay 2> ls_output.txt</code> </b></details>
<details> <summary>Demonstrate Linux stderr to stdout redirection</summary> <b><code>yippiekaiyay &> file</code> </b></details>
<details> <summary>What is the result of running the following command? <code>yippiekaiyay 1>&2 die_hard</code></code></summary> <b>An output similar to: yippikaiyay: command not found...
The file die_hard will not be created
</b></details>
<a name="questions-linux-fhs"></a>
The root of the filesystem. The beginning of the tree. </b></details>
<details> <summary>What is stored in each of the following paths?/tmp folder is cleaned automatically, usually upon reboot.
</b></details>
It contains useful information about the processes that are currently running, it is regarded as control and information center for kernel. </b></details>
<details> <summary>What makes /proc different from other filesystems?</summary> <b> /proc is a special virtual filesystem in Unix-like operating systems, including Linux, that provides information about processes and system resources. </b></details> <details> <summary>True or False? only root can create files in /proc</summary> <b>False. No one can create file in /proc directly (certain operations can lead to files being created in /proc by the kernel). </b></details>
<details> <summary>What can be found in /proc/cmdline?</summary> <b>The command passed to the boot loader to run the kernel </b></details>
<details> <summary>In which path can you find the system devices (e.g. block storage)?</summary> <b> /dev </b></details><a name="questions-linux-permissions"></a>
Using the chmod command.
</b></details>
</b></details>
<details> <summary>What this command does? <code>chmod +x some_file</code></summary> <b> It adds execute permissions to all sets i.e user, group and others </b></details> <details> <summary>Explain what is setgid and setuid</summary> <b>The sudo program is installed by default in almost all Linux distributions. If you need to install sudo in Debian/Ubuntu, use the command apt-get install sudo
</b></details>
<details> <summary>True or False? In order to install packages on the system one must be the root user or use the sudo command</summary> <b>True </b></details>
<details> <summary>Explain what are ACLs. For what use cases would you recommend to use them?</summary> <b> ACL stands for Access Control Lists. We can use ACL to have more granular control over accesses to certain files for certain users specifically. For instance, we can return the ACL of a particular file with the command <code>getfacl /absolute/file/path</code> and modify ACLs for a specific file with <code>setfacl -m</code>.</b></details>
<details> <summary>You try to create a file but it fails. Name at least three different reason as to why it could happen</summary> <b>Using sudo setfacl -m u::rx /usr/bin/chmod will set the execute permissions on chmod for all the users. Post this, the chmod binary can be used as usual.
</b></details>
<a name="questions-linux-scenarios"></a>
There are multiple ways to transfer files between hosts. Personal opinion: use rsync
</b></details>
One way is to run the following: cat /proc/sys/kernel/random/uuid
</b></details>
mkpasswd -l 7
</b></details>
<a name="questions-linux-systemd"></a>
A daemon is a program that runs in the background without direct control of the user, although the user can at any time talk to the daemon.
systemd has many features such as user processes control/tracking, snapshot support, inhibitor locks..
If we visualize the unix/linux system in layers, systemd would fall directly after the linux kernel.
Hardware -> Kernel -> <u>Daemons</u>, System Libraries, Server Display. </b>
</details> <details> <summary>How to start or stop a service?</summary> <b>To start a service: systemctl start <service name>
To stop a service: systemctl stop <service name>
</b></details>
systemctl status <service name>
</b></details>
<code>journalctl</code> </b></details>
<details> <summary>Describe how to make a certain process/app a service</summary> <b> The process will need a <code>.service</code> file to be created at the location <code>/etc/systemd/system/service-name.service</code> to be made into a service. The file has certain characteristics and need certain inputs to work. More details <a href="https://medium.com/@benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6">here</a>. </b></details>/var/log </b></details>
<details> <summary>How to follow file's content as it being appended without opening the file every time?</summary> <b>tail -f <file_name> </b></details>
<details> <summary>What are you using for troubleshooting and debugging <b>network</b> issues?</summary> <b><code>dstat -t</code> is great for identifying network and disk issues. <code>netstat -tnlaup</code> can be used to see which processes are running on which ports. <code>lsof -i -P</code> can be used for the same purpose as netstat. <code>ngrep -d any metafilter</code> for matching regex against payloads of packets. <code>tcpdump</code> for capturing packets <code>wireshark</code> same concept as tcpdump but with GUI (optional). </b></details>
<details> <summary>What are you using for troubleshooting and debugging <b>disk & file system</b> issues?</summary> <b><code>dstat -t</code> is great for identifying network and disk issues. <code>opensnoop</code> can be used to see which files are being opened on the system (in real time). </b></details>
<details> <summary>What are you using for troubleshooting and debugging <b>process</b> issues?</summary> <b><code>strace</code> is great for understanding what your program does. It prints every system call your program executed. </b></details>
<details> <summary>What are you using for debugging CPU related issues?</summary> <b><code>top</code> will show you how much CPU percentage each process consumes <code>perf</code> is a great choice for sampling profiler and in general, figuring out what your CPU cycles are "wasted" on <code>flamegraphs</code> is great for CPU consumption visualization (http://www.brendangregg.com/flamegraphs.html) </b></details>
<details> <summary>You get a call from someone claiming "my system is SLOW". What do you do?</summary> <b>top for anything unusualdstat -t to check if it's related to disk or network.sariostat
</b></details>lsof <FILE_PATH>kill <PID></b></details>
The kernel is part of the operating system and is responsible for tasks like:
uname -a command
</b></details>
A Linux kernel module is a piece of code that can be dynamically loaded into the kernel to extend its functionality. These modules are typically used to add support for hardware devices, filesystems, or system calls. The kernel itself is monolithic, but with modules, its capabilities can be extended without having to reboot the system or recompile the entire kernel. </b></details>
<details> <summary>Explain user space vs. kernel space</summary> <b>The operating system executes the kernel in protected memory to prevent anyone from changing (and risking it crashing). This is what is known as "Kernel space". "User space" is where users executes their commands or applications. It's important to create this separation since we can't rely on user applications to not tamper with the kernel, causing it to crash.
Applications can access system resources and indirectly the kernel space by making what is called "system calls". </b></details>
<details> <summary>In what phases of kernel lifecycle, can you change its configuration?</summary> <b>Usually it will reside in /boot/config-<kernel version>.<os release>.<arch>
</b></details>
/proc/cmdline
</b></details>
sysctl -a
</b></details>
Yes, you might notice that in most systems, when running systctl -a with root, you'll get more runtime parameters compared to executing the same command with a regular user.
</b></details>
sudo sysctl net.ipv4.ip_forward=1
To make it persistent (applied after reboot for example): insert net.ipv4.ip_forward = 1 into /etc/sysctl.conf
Another way to is to run echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
</b></details>
If you strace the sysctl command you can see it does it by changing the file under /proc/sys/...
In the past it was done with sysctl system call, but it was deprecated at some point. </b></details>
<details> <summary>How changes to kernel runtime parameters persist? (applied even after reboot to the system for example)</summary> <b>There is a service called systemd-sysctl that takes the content of /etc/sysctl.conf and applies it. This is how changes persist, even after reboot, when they are written in /etc/sysctl.conf
</b></details>
No. Containers have their own /proc filesystem so any change to kernel parameters inside a container, are not affecting the host or other containers running on that host. </b></details>
<a name="questions-linux-ssh"></a>
Wikipedia Definition: "SSH or Secure Shell is a cryptographic network protocol for operating network services securely over an unsecured network."
Hostinger.com Definition: "SSH, or Secure Shell, is a remote administration protocol that allows users to control and modify their remote servers over the Internet."
An SSH server will have SSH daemon running. Depends on the distribution, you should be able to check whether the service is running (e.g. systemctl status sshd). </b></details>
<details> <summary>Why SSH is considered better than telnet?</summary> <b>Telnet also allows you to connect to a remote host but as opposed to SSH where the communication is encrypted, in telnet, the data is sent in clear text, so it doesn't considered to be secured because anyone on the network can see what exactly is sent, including passwords. </b></details>
<details> <summary>What is stored in <code>~/.ssh/known_hosts</code>?</summary> <b>The file stores the key fingerprints for the clients connecting to the SSH server. This fingerprint creates a trust between the client and the server for future SSH connections. </b></details>
<details> <summary>You try to ssh to a server and you get "Host key verification failed". What does it mean?</summary> <b>It means that the key of the remote host was changed and doesn't match the one that stored on the machine (in ~/.ssh/known_hosts). </b></details>
<details> <summary>What is the difference between SSH and SSL?</summary> <b> </b></details> <details> <summary>What <code>ssh-keygen</code> is used for?</summary> <b><code>ssh-keygen</code> is a tool to generate an authentication key pair for SSH, that consists of a private and a public key. It supports a number of algorithms to generate authentication keys :
One can also specify number of bits in key. Command below generates an SSH key pair with RSA 4096-bits :
$ ssh-keygen -t rsa -b 4096
The output looks like this:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa
Your public key has been saved in /home/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:f5MOGnhzYfC0ZCHvbSXXiRiNVYETjxpHcXD5xSojx+M user@mac-book-pro
The key's randomart image is:
+---[RSA 4096]----+
| . ..+***o|
| o o++*o+|
| . =+.++++|
| B.oX+. .|
| S *=o+ |
| . o oE. |
| . + + + |
| . = + . |
| . . |
+----[SHA256]-----+
One can check how many bits an SSH key has with :
$ ssh-keygen -l -f /home/user/.ssh/id_rsa
Output should look like this :
4096 SHA256:f5MOGnhzYfC0ZCHvbSXXiRiNVYETjxpHcXD5xSojx+M user@mac-book-pro (RSA)
It shows the key is RSA 4096-bits.
-l and -f parameters usage explanation :
-l Show the fingerprint of the key file.
-f filename Filename of the key file.
Learn more : How can I tell how many bits my ssh key is? - Superuser </b></details>
<details> <summary>What is SSH port forwarding?</summary> <b> </b></details><a name="questions-linux-wildcards"></a>
aaa bbb ccc.aaa aaaaaa</summary> <b>
lines 1 and 3. </b></details>
<details> <summary>What is the difference single and double quotes?</summary> <b> </b></details> <details> <summary>What is escaping? What escape character is used for escaping?</summary> <b> </b></details> <details> <summary>What is an exit code? What exit codes are you familiar with?</summary> <b>An exit code (or return code) represents the code returned by a child process to its parent process.
0 is an exit code which represents success while anything higher than 1 represents error. Each number has different meaning, based on how the application was developed.
I consider this as a good blog post to read more about it: https://shapeshed.com/unix-exit-codes </b></details>
<a name="questions-linux-boot"></a>
Another way to ask this: what happens from the moment you turned on the server until you get a prompt </b></details>
<details> <summary>What is GRUB2?</summary> <b> </b></details> <details> <summary>What is Secure Boot?</summary> <b> </b></details> <details> <summary>What can you find in /boot?</summary> <b> </b></details><a name="questions-linux-disk-fs"></a>
For each file (and directory) in Linux there is an inode, a data structure which stores meta data related to the file like its size, owner, permissions, etc. </b></details>
<details> <summary>Which of the following is not included in inode:File name (it's part of the directory file) </b></details>
<details> <summary>How to check which disks are currently mounted?</summary> <b>Run mount
</b></details>
cat /proc/mounts
</b></details>
Hard link is the same file, using the same inode. Soft link is a shortcut to another file, using a different inode. </b></details>
<details> <summary>True or False? You can create an hard link for a directory</summary> <b>False </b></details>
<details> <summary>True or False? You can create a soft link between different filesystems</summary> <b>True </b></details>
<details> <summary>True or False? Directories always have by minimum 2 links</summary> <b>True. </b></details>
<details> <summary>What happens when you delete the original file in case of soft link and hard link?</summary> <b> </b></details> <details> <summary>Can you check what type of filesystem is used in /home?</summary> <b>There are many answers for this question. One way is running df -T
</b></details>
du -sh
</b></details>
</b></details>
<details> <summary>What is NFS? What is it used for?</summary> <b> </b></details> <details> <summary>What RAID is used for? Can you explain the differences between RAID 0, 1, 5 and 10?</summary> <b> </b></details> <details> <summary>Describe the process of extending a filesystem disk space</summary> <b> </b></details> <details> <summary>What is lazy umount?</summary> <b> </b></details> <details> <summary>What is tmpfs?</summary> <b> </b></details> <details> <summary>What is stored in each of the following logs?</b></details>
<details> <summary>True or False? both /tmp and /var/tmp cleared upon system boot</summary> <b>False. /tmp is cleared upon system boot while /var/tmp is cleared every a couple of days or not cleared at all (depends on distro). </b></details>
<a name="questions-linux-performance-analysis"></a>
One can use uptime or top
</b></details>
This article summarizes the load average topic in a great way </b></details>
<details> <summary>How to check process usage?</summary> <b>pidstat </b></details>
<details> <summary>How to check disk I/O?</summary> <b>iostat -xz 1
</b></details>
You can use the commands <code>top</code> and <code>free</code> </b></details>
<details> <summary>How to check TCP stats?</summary> <b>sar -n TCP,ETCP 1 </b></details>
<a name="questions-linux-processes"></a>
The "ps" command can be used to list all the processes running in a system. The "ps aux" command provides a detailed list of all the processes, including the ones running in the background. </b></details>
<details> <summary>How to run a process in the background and why to do that in the first place?</summary> <b>You can achieve that by specifying & at the end of the command. As to why, since some commands/processes can take a lot of time to finish execution or run forever, you may want to run them in the background instead of waiting for them to finish before gaining control again in current session. </b></details>
<details> <summary>How can you find how much memory a specific process consumes?</summary> <b> <code> mem() { ps -eo rss,pid,euser,args:100 --sort %mem | grep -v grep | grep -i $@ | awk '{printf $1/1024 "MB"; $1=""; print }' } </code> [Source](https://stackoverflow.com/questions/3853655/in-linux-how-to-tell-how-much-memory-processes-are-using) </b></details> <details> <summary>What signal is used by default when you run 'kill *process id*'?</summary> <b> <pre> The default signal is SIGTERM (15). This signal kills process gracefully which means it allows it to save current state configuration. </pre> </b></details> <details> <summary>What signals are you familiar with?</summary> <b>SIGTERM - default signal for terminating a process SIGHUP - common usage is for reloading configuration SIGKILL - a signal which cannot caught or ignored
To view all available signals run kill -l
</b></details>
</b></details>
<details> <summary>Every couple of days, a certain process stops running. How can you look into why it's happening?</summary> <b> One way to investigate why a process stops running is to check the system logs, such as the messages in /var/log/messages or journalctl. Additionally, checking the process's resource usage and system load may provide clues as to what caused the process to stop </b></details> <details> <summary>What happens when you press ctrl + c?</summary> <b> When you press "Ctrl+C," it sends the SIGINT signal to the foreground process, asking it to terminate gracefully. </b></details> <details> <summary>What is a Daemon in Linux?</summary> <b>A background process. Most of these processes are waiting for requests or set of conditions to be met before actually running anything. Some examples: sshd, crond, rpcbind. </b></details>
<details> <summary>What are the possible states of a process in Linux?</summary> <b> <pre> Running (R) Uninterruptible Sleep (D) - The process is waiting for I/O Interruptible Sleep (S) Stopped (T) Dead (x) Zombie (z) </pre> </b></details> <details> <summary>How do you kill a process in D state?</summary> <b> A process in D state (also known as "uninterruptible sleep") cannot be killed using the "kill" command. The only way to terminate it is to reboot the system. </b></details> <details> <summary>What is a zombie process?</summary> <b>A process which has finished to run but has not exited.
One reason it happens is when a parent process is programmed incorrectly. Every parent process should execute wait() to get the exit code from the child process which finished to run. But when the parent isn't checking for the child exit code, the child process can still exists although it finished to run. </b></details>
<details> <summary>How to get rid of zombie processes?</summary> <b>You can't kill a zombie process the regular way with kill -9 for example as it's already dead.
One way to kill zombie process is by sending SIGCHLD to the parent process telling it to terminate its child processes. This might not work if the parent process wasn't programmed properly. The invocation is kill -s SIGCHLD [parent_pid]
You can also try closing/terminating the parent process. This will make the zombie process a child of init (1) which does periodic cleanups and will at some point clean up the zombie process. </b></details>
<details> <summary>How to find all theIf you mention at any point ps command with arguments, be familiar with what these arguments does exactly. </b></details>
<details> <summary>What is the init process?</summary> <b> It is the first process executed by the kernel during the booting of a system. It is a daemon process which runs till the system is shutdown. That is why, it is the parent of all the processes </b></details> <details> <summary>Can you describe how processes are being created?</summary> <b> </b></details> <details> <summary>How to change the priority of a process? Why would you want to do that?</summary> <b> To change the priority of a process, you can use the nice command in Linux. The nice command allows you to specify the priority of a process by assigning a priority value ranging from -20 to 19. A higher value of priority means lower priority for the process, and vice versa.You may want to change the priority of a process to adjust the amount of CPU time it is allocated by the system scheduler. For example, if you have a CPU-intensive process running on your system that is slowing down other processes, you can lower its priority to give more CPU time to other processes. </b></details>
<details> <summary>Can you explain how network process/connection is established and how it's terminated?></summary> </b> When a client process on one system wants to establish a connection with a server process on another system, it first creates a socket using the socket system call. The client then calls the connect system call, passing the address of the server as an argument. This causes a three-way handshake to occur between the client and server, where the two systems exchange information to establish a connection.Once the connection is established, the client and server can exchange data using the read and write system calls. When the connection is no longer needed, the client or server can terminate the connection by calling the close system call on the socket. </b></details>
<details> <summary>What <code>strace</code> does? What about <code>ltrace</code>?</summary> <b> Strace is a debugging tool that is used to monitor the system calls made by a process. It allows you to trace the execution of a process and see the system calls it makes, as well as the signals it receives. This can be useful for diagnosing issues with a process, such as identifying why it is hanging or crashing.Ltrace, on the other hand, is a similar tool that is used to trace the library calls made by a process. It allows you to see the function calls made by a process to shared libraries, as well as the arguments passed to those functions. This can be useful for diagnosing issues with a process that involve library calls, such as identifying why a particular library is causing a problem.
</b></details>
<details> <summary>Find all the files which end with '.yml' and replace the number 1 in 2 in each file</summary> <b>find /some_dir -iname *.yml -print0 | xargs -0 -r sed -i "s/1/2/g" </b></details>
<details> <summary>You run ls and you get "/lib/ld-linux-armhf.so.3 no such file or directory". What is the problem?</summary> <b>The ls executable is built for an incompatible architecture. </b></details>
<details> <summary>How would you split a 50 lines file into 2 files of 25 lines each?</summary> <b>You can use the <code>split</code> command this way: <code>split -l 25 some_file</code> </b></details>
<details> <summary>What is a file descriptor? What file descriptors are you familiar with?</summary> <b> Kerberos File descriptor, also known as file handler, is a unique number which identifies an open file in the operating system.In Linux (and Unix) the first three file descriptors are:
This is a great article on the topic: https://www.computerhope.com/jargon/f/file-descriptor.htm </b></details>
<details> <summary>What is NTP? What is it used for?</summary> <b> </b></details> <details> <summary>Explain Kernel OOM</summary> <b> </b></details><a name="questions-linux-security"></a>
One way is using openssl this way:
openssl genrsa -aes256 -out ca-private-key.pem 4096
</b></details>
openssl req -new -x509 -days 730 -key [private key file name] -sha256 -out ca.pem
If using the private key from the previous question then the command would be:
openssl req -new -x509 -days 730 -key ca-private-key.pem -sha256 -out ca.pem
</b></details>
Encode: echo -n "some password" | base64
Decode: echo -n "allE19remO91" | base64
</b></details>
<a name="questions-linux-networking"></a>
ip link show
</b></details>
<details> <summary>What is the loopback (lo) interface?</summary> <b>The loopback interface is a special, virtual network interface that your computer uses to communicate with itself. It is used mainly for diagnostics and troubleshooting, and to connect to servers running on the local machine. </b></details>
<details> <summary>What the following commands are used for?</b></details>
<details> <summary>What is a network namespace? What is it used for?</summary> <b> </b></details> <details> <summary>How to check if a certain port is being used?</summary> <b>One of the following would work:
netstat -tnlp | grep <port_number>
lsof -i -n -P | grep <port_number>
</b></details>
<details> <summary>How can you turn your Linux server into a router?</summary> <b> </b></details> <details> <summary>What is a virtual IP? In what situation would you use it?</summary> <b> </b></details> <details> <summary>True or False? The MAC address of an interface is assigned/set by the OS</summary> <b>False </b></details>
<details> <summary>Can you have more than one default gateway in a given system?</summary> <b>Technically, yes. </b></details>
<details> <summary>What is telnet and why is it a bad idea to use it in production? (or at all)</summary> <b>Telnet is a type of client-server protocol that can be used to open a command line on a remote computer, typically a server. By default, all the data sent and received via telnet is transmitted in clear/plain text, therefore it should not be used as it does not encrypt any data between the client and the server. </b></details>
<details> <summary>What is the routing table? How do you view it?</summary> <b> </b></details> <details> <summary>How can you send an HTTP request from your shell?</summary> <b>Using nc is one way
</b></details>
<details> <summary>What are packet sniffers? Have you used one in the past? If yes, which packet sniffers have you used and for what purpose?</summary> <b> It is a network utility that analyses and may inject tasks into the data-stream travelling over the targeted network. </b></details> <details> <summary>How to list active connections?</summary> <b> </b></details> <details> <summary>How to trigger neighbor discovery in IPv6?</summary> <b>One way would be ping6 ff02::1
</b></details>
There a couple of modes:
<a name="questions-linux-dns"></a>
cat /etc/hostname
You can also run hostnamectl or hostname but that might print only a temporary hostname. The one in the file is the permanent one.
</b></details>
You can specify one or more of the following:
ANSWER SECTION:
codingshell.com. 3515 IN A 185.199.109.153
What is the meaning of the number 3515?
</summary> <b>This is the TTL. When you lookup for an address using a domain/host name, your OS is performing DNS resolution by contacting DNS name servers to get the IP address of the host/domain you are looking for.
When you get a reply, this reply in cached in your OS for a certain period of time. This is period of time is also known as TTL and this is the meaning of 3515 number - it will be cached for 3515 seconds before removed from the cache and during that period of time, you'll get the value from the cache instead of asking DNS name servers for the address again. </b></details>
<details> <summary> How can we modify the network connection via `nmcli` command, to use `8.8.8.8` as a DNS server? </summary> <b>Find the connection name:
# nmcli con show
NAME UUID TYPE DEVICE
System ens5 8126c120-a964-e959-ff98-ac4973344505 ethernet ens5
System eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ethernet --
Here the connection name is "System ens5". Let's say we want to modify settings for this connection.
Modify the connection to use 8.8.8.8 as DNS server:
# nmcli con mod "System ens5" ipv4.dns "8.8.8.8"
We need to reactivate the connection for the change to take effect:
nmcli con up "System ens5"
Verify our settings once more:
cat /etc/resolv.conf
nmcli -f ipv4.dns con show "System ens5"
<a name="questions-linux-packaging"></a>
The answer depends on the distribution being used.
In Fedora/CentOS/RHEL/Rocky it can be done with rpm or dnf commands.
In Ubuntu it can be done with the apt command.
</b></details>
Package managers allow you to manage packages lifecycle as in installing, removing and updating the packages.
In addition, you can specify in a spec how a certain package will be installed - where to copy the files, which commands to run prior to the installation, post the installation, etc. </b></details>
<a name="questions-linux-dnf"></a>
From the repo:
"Dandified YUM (DNF) is the next upcoming major version of YUM. It does package management using RPM, libsolv and hawkey libraries."
Official docs
</b></details>
<details> <summary>How to look for a package that provides the command /usr/bin/git? (the package isn't necessarily installed)</summary> <b>dnf provides /usr/bin/git </b></details>
<a name="questions-linux-apps-and-services"></a>
Depends on the init system.
Systemd: <code> systemctl enable [service_name] </code> System V: <code> update-rc.d [service_name] </code> and add this line <code> id:5678:respawn:/bin/sh /path/to/app </code> to /etc/inittab Upstart: add Upstart init script at /etc/init/service.conf </b></details>
<details> <summary>You run <code>ssh 127.0.0.1</code> but it fails with "connection refused". What could be the problem?</summary> <b>Nginx, Apache httpd. </b></details>
<a name="questions-linux-users-and-groups"></a>
Command to create users is useradd
Syntax:
useradd [options] Username
There are 2 configuration files, which stores users information
/etc/passwd - Users information like, username, shell etc is stored in this file
/etc/shadow - Users password is stored in encrypted format
/etc/groups file stores the group name, group ID, usernames which are in secondary group.
passwd <username> is the command to set/change password of a user.
/etc/shadow file holds the passwords of the users in encrypted format. NO, it is only visible to the root user
YES, we can create new user by manually adding an entry in the /etc/passwd file.
For example, if we need to create a user called john.
Step 1: Add an entry to /etc/passwd file, so user gets created.
echo "john:x:2001:2001::/home/john:/bin/bash" >> /etc/passwd
Step 2: Add an entry to /etc/group file, because every user belong to the primary group that has same name as the username.
echo "john:x:2001:" >> /etc/group
Step 3: Verify if the user got created
id john
/etc/passwd is a configuration file, which contains users information. Each entry in this file has, 7 fields,
username:password:UID:GID:Comment:home directory:shell
username - The name of the user.
password - This field is actually a placeholder of the password field. Due to security concerns, this field does not contain the password, just a placeholder (x) to the encrypted password stored in /etc/shadow file.
UID - User ID of the user.
GID - Group ID
Comment - This field is to provide description about the user.
home directory - Abousulte path of the user's home directory. This directory gets created once the user is added.
shell - This field contains the absolute path of the shell that will be used by the respective user.
adduser user_name --shell=/bin/false --no-create-home
You can also add a user and then edit /etc/passwd.
</b></details>
su command. Use su - to switch to root </b></details>
<details> <summary>What is the UID the root user? What about a regular user?</summary>UID of root user is 0
Default values of UID_MIN and UID_MAX in /etc/login.defs
UID_MIN is 1000
UID_MAX is 60000
Actually, we can change this value. But UID < 1000 are reserved for system accounts.
Therefore, as per the default configuration, for regular user UID starts from 1000.
Re-install the OS IS NOT the right answer :) </b></details>
<details> <summary>What is /etc/skel?</summary>/etc/skel is a directory, that contains files or directories, so when a new user is created, these files/directories created under /etc/skel will be copied to user's home directory.
Using the last command.
</b></details>
useradd - Command for creating new users
usermod - Modify the users setting
whoami - Outputs, the username that we are currently logged in
id - Prints the
</b></details>
The user you are using isn't defined locally but originates from services like LDAP.
You can verify with: getent passwd
</b></details>
<a name="questions-linux-hardware"></a>
/proc/cpuinfo
You can also use nproc for number of processors
</b></details>
dmidecoode </b></details>
<details> <summary>How can you print all the information on connected block devices in your system?</summary> <b>lsblk </b></details>
<details> <summary>True or False? In user space, applications don't have full access to hardware resources</summary> <b>True. Only in kernel space they have full access to hardware resources. </b></details>
<a name="questions-linux-namespaces"></a>
True. Inside the namespace it's PID 1 while to the parent namespace the PID is a different one. </b></details>
<details> <summary>True or False? In a child PID namespace all processes are aware of parent PID namespace and processes and the parent PID namespace has no visibility of child PID namespace processes</summary> <b>False. The opposite is true. Parent PID namespace is aware and has visibility of processes in child PID namespace and child PID namespace has no visibility as to what is going on in the parent PID namespace. </b></details>
<details> <summary>True or False? By default, when creating two separate network namespaces, a ping from one namespace to another will work fine</summary> <b>False. Network namespace has its own interfaces and routing table. There is no way (without creating a bridge for example) for one network namespace to reach another. </b></details>
<details> <summary>True or False? With UTS namespaces, processes may appear as if they run on different hosts and domains while running on the same host</summary> <b>True </b></details>
<details> <summary>True or False? It's not possible to have a root user with ID 0 in child user namespaces</summary> <b>False. In every child user namespace, it's possible to have a separate root user with uid of 0. </b></details>
<details> <summary>What time namespaces are used for?</summary> <b>In time namespaces processes can use different system time. </b></details>
<a name="questions-linux-virtualization"></a>
Is an open source virtualization technology used to operate on x86 hardware.
From the official docs Recommended read:
It's an open source collection of software used to manage virtual machines. It can be used with: KVM, Xen, LXC and others. It's also called Libvirt Virtualization API.
From the official docs Hypervisor supported docs </b></details>
<a name="questions-linux-awk"></a>
From Wikipedia: "AWK is domain-specific language designed for text processing and typically used as a data extraction and reporting tool" </b></details>
<details> <summary>How to print the 4th column in a file?</summary> <b>awk '{print $4}' file
</b></details>
awk 'length($0) > 79' file
</b></details>
Using system calls </b></details>
<a name="questions-linux-system-calls"></a>
fork() is used for creating a new process. It does so by cloning the calling process but the child process has its own PID and any memory locks, I/O operations and semaphores are not inherited. </b></details>
<details> <summary>What is the return value of fork()?</summary> <b>Not enough memory to create a new process </b></details>
<details> <summary>Why do we need the wait() system call?</summary> <b>wait() is used by a parent process to wait for the child process to finish execution. If wait is not used by a parent process then a child process might become a zombie process. </b></details>
<details> <summary>How the kernel notifies the parent process about child process termination?</summary> <b>The kernel notifies the parent by sending the SIGCHLD to the parent. </b></details>
<details> <summary>How the waitpid() is different from wait()?</summary> <b>The waitpid() is a non-blocking version of the wait() function.
It also supports using library routine (e.g. system()) to wait a child process without messing up with other children processes for which the process has not waited. </b></details>
<details> <summary>True or False? The wait() system call won't return until the child process has run and exited</summary> <b>True in most cases though there are cases where wait() returns before the child exits. </b></details>
<details> <summary>Explain the exec() system call</summary> <b>It transforms the current running program into another program.
Given the name of an executable and some arguments, it loads the code and static data from the specified executable and overwrites its current code segment and current static code data. After initializing its memory space (like stack and heap) the OS runs the program passing any arguments as the argv of that process. </b></details>
<details> <summary>True or False? A successful call to exec() never returns</summary> <b>True
Since a successful exec replace the current process, it can't return anything to the process that made the call. </b></details>
<details> <summary>What system call is used for listing files?</summary> <b> </b></details> <details> <summary>What system calls are used for creating a new process?</summary> <b>fork(), exec() and the wait() system call is also included in this workflow. </b></details>
<details> <summary>What execve() does?</summary> <b>Executes a program. The program is passed as a filename (or path) and must be a binary executable or a script. </b></details>
<details> <summary>What is the return value of malloc?</summary> <b> </b></details> <details> <summary>Explain the pipe() system call. What does it used for?</summary> <b>"Pipes provide a unidirectional interprocess communication channel. A pipe has a read end and a write end. Data written to the write end of a pipe can be read from the read end of the pipe. A pipe is created using pipe(2), which returns two file descriptors, one referring to the read end of the pipe, the other referring to the write end." </b></details>
<details> <summary>What happens when you execute <code>ls -l</code>?</summary> <b>Shell reads the input using getline() which reads the input file stream and stores into a buffer as a string
The buffer is broken down into tokens and stored in an array this way: {"ls", "-l", "NULL"}
Shell checks if an expansion is required (in case of ls *.c)
Once the program in memory, its execution starts. First by calling readdir()
Notes:
This way provides a lot of flexibility. It allows the shell for example, to run code after the call to fork() but before the call to exec(). Such code can be used to alter the environment of the program it about to run. </b></details>
<details> <summary>Describe shortly what happens when you execute a command in the shell</summary> <b>The shell figures out, using the PATH variable, where the executable of the command resides in the filesystem. It then calls fork() to create a new child process for running the command. Once the fork was executed successfully, it calls a variant of exec() to execute the command and finally, waits the command to finish using wait(). When the child completes, the shell returns from wait() and prints out the prompt again. </b></details>
<a name="questions-linux-fs-files"></a>
There are a couple of ways to do that:
open("/my/file") = 5
read(5, "file content")
These system calls are reading the file <code>/my/file</code> and 5 is the file descriptor number. </b></details>
<details> <summary>Describe three different ways to remove a file (or its content)</summary> <b> </b></details> <details> <summary>What is the difference between a process and a thread?</summary> <b> </b></details> <details> <summary>What is context switch?</summary> <b>From wikipedia: a context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point </b></details>
<details> <summary>You found there is a server with high CPU load but you didn't find a process with high CPU. How is that possible?</summary> <b> </b></details><a name="questions-linux-advanced-networking"></a>
Another common way to task this questions is "what part of the tcp header does traceroute modify?" </b></details>
<details> <summary>What is network bonding? What types are you familiar with?</summary> <b> </b></details> <details> <summary>How to link two separate network namespaces so you can ping an interface on one namespace from the second one?</summary> <b> </b></details> <details> <summary>What are cgroups?</summary> <b> </b></details> <details> <summary>Explain Process Descriptor and Task Structure</summary> <b> </b></details> <details> <summary>What are the differences between threads and processes?</summary> <b> </b></details> <details> <summary>Explain Kernel Threads</summary> <b> </b></details> <details> <summary>What happens when socket system call is used?</summary> <b>This is a good article about the topic: https://ops.tips/blog/how-linux-creates-sockets </b></details>
<details> <summary>You executed a script and while still running, it got accidentally removed. Is it possible to restore the script while it's still running?</summary> <b> It is possible to restore a script while it's still running if it has been accidentally removed. The running script process still has the code in memory. You can use the /proc filesystem to retrieve the content of the running script. 1.Find the Process ID by running ``` ps aux | grep yourscriptname.sh ``` Replace yourscriptname.sh with your script name. 2.Once you have the PID, you can access the script's memory through the /proc filesystem. The script will be available at /proc/<PID>/fd/, where <PID> is the process ID of the running script. Typically, the script's file descriptor is 0 or 1.You can copy the script content to a new file using the cp command:
cp /proc/<PID>/fd/0 /path_to_restore_your_file/yourscriptname.sh
Replace <PID> with the actual PID of the script and /path_to_restore_your_file/yourscriptname.sh with the path where you want to restore the script.
</b></details>
<a name="questions-linux-memory"></a>
MemFree - The amount of unused physical RAM in your system MemAvailable - The amount of available memory for new workloads (without pushing system to use swap) based on MemFree, Active(file), Inactive(file), and SReclaimable. </b></details>
<details> <summary>What is the difference between paging and swapping?</summary> <b> </b></details> <details> <summary>Explain what is OOM killer</summary> <b> </b></details><a name="questions-linux-distributions"></a>
<a name="questions-linux-sed"></a>
echo $line | sed 's/.*\[//g;s/].*//g;s/:.*//g'
</b></details>
<a name="questions-linux-misc"></a>
/etc/*-release file
/etc/redhat-release and for Amazon it will be /etc/os-releaselsb_release is a common command you can use in multiple different distributions
</b></details>ls, wc, dd, df, du, ps, ip, cp, cd ... </b></details>
<details> <summary>What ways are there for creating a new empty file?</summary> <b>$OLDPWD </b></details>
<details> <summary>List three ways to print all the files in the current directory</summary> <b>For these we can use wc command.
To count the number of lines in file
wc -l
To count the number of words in file
wc -w
</b></details>
A good answer can be found here </b></details>
<details> <summary>Explain "environment variables". How do you list all environment variables?</summary> <b> </b></details> <details> <summary>What is a TTY device?</summary> <b> </b></details> <details> <summary>How to create your own environment variables?</summary> <b>X=2 for example. But this will persist to new shells. To have it in new shells as well, use export X=2
</b></details>
It's used in commands to mark the end of commands options. One common example is when used with git to discard local changes: git checkout -- some_file
</b></details>
/dev </b></details>
<details> <summary>Why there are different sections in man? What is the difference between the sections?</summary> <b> </b></details> <details> <summary>What is User-mode Linux?</summary> <b> In Linux, user mode is a restricted operating mode in which a user's application or process runs. User mode is a non-privileged mode that prevents user-level processes from accessing sensitive system resources directly.In user mode, an application can only access hardware resources indirectly, by calling system services or functions provided by the operating system. This ensures that the system's security and stability are maintained by preventing user processes from interfering with or damaging system resources.
Additionally, user mode also provides memory protection to prevent applications from accessing unauthorized memory locations. This is done by assigning each process its own virtual memory space, which is isolated from other processes.
In contrast to user mode, kernel mode is a privileged operating mode in which the operating system's kernel has full access to system resources, and can perform low-level operations, such as accessing hardware devices and managing system resources directly.
</b></details>
<details> <summary>Under which license Linux is distributed? </summary> <b>GPL v2 </b></details>