Linux and System Programming
Linux and System Programming
Content List............................................................................................................................ 1
Linux .......................................................................................................................................... 3
/bin ....................................................................................................................................... 10
/Sbin ..................................................................................................................................... 10
/etc ........................................................................................................................................ 12
/proc ..................................................................................................................................... 12
Downloading Links:................................................................................................................. 13
1
Umask value: ....................................................................................................................... 23
System Information:............................................................................................................. 33
Networking: ......................................................................................................................... 37
2
Linux
Introduction to Linux:
Linux is an open-source operating system kernel initially developed by Linus Torvalds in 1991.
Since then, it has evolved into a powerful and versatile platform that runs everything from
small embedded systems to massive server farms.
At its core, Linux is built on the Unix operating system principles, emphasizing stability,
efficiency, and flexibility. What sets Linux apart is its open nature, allowing anyone to view,
modify, and distribute its source code under the terms of the GNU General Public License
(GPL).
3. Stability and Security: Linux is renowned for its stability and security. Its robust
architecture and stringent permission system make it less prone to crashes and malware
attacks compared to other operating systems.
5. Vast Software Ecosystem: Linux boasts a vast array of open-source software, covering
everything from productivity tools and multimedia applications to development
environments and server software.
3
Common Linux Distributions:
1. Ubuntu Linux: Ubuntu is known for its user-friendliness, extensive community
support, and regular release cycle. It's a great choice for beginners and experienced
users alike, offering a balance between stability and up-to-date software.
2. Fedora Linux: Fedora emphasizes the use of cutting-edge technologies and serves as
a testing ground for features that may eventually be incorporated into Red Hat
Enterprise Linux. It's favored by developers and enthusiasts who want access to the
latest software releases.
3. Red Hat Enterprise Linux (RHEL): RHEL is a leading distribution in the enterprise
sector, known for its stability, long-term support, and comprehensive ecosystem of
certified software and hardware. It's backed by Red Hat, Inc., providing premium
support and services.
4. CentOS Linux: CentOS, short for Community ENTerprise Operating System, is a free
and open-source distribution built from the same sources as RHEL. It aims to provide
a stable and compatible platform for organizations and individuals who prefer the
RHEL environment without the associated subscription costs.
6. SUSE Linux Enterprise Server (SLES): SUSE Linux Enterprise Server is a powerful
distribution tailored for mission-critical workloads and enterprise deployments. It offers
high availability, security, and interoperability features, making it a preferred choice for
businesses.
7. Gentoo Linux: Gentoo is a source-based distribution known for its flexibility and
performance optimization. Users have granular control over system configuration and
package management, allowing for a highly customized and efficient computing
environment.
4
9. Debian Linux: Debian is one of the oldest and most influential Linux distributions,
known for its commitment to free and open-source software principles. It serves as the
foundation for many other distributions, including Ubuntu and its variants.
10. Slackware: Slackware is one of the earliest Linux distributions, known for its
simplicity, stability, and adherence to Unix-like principles. It's favoured by experienced
users who appreciate its minimalistic approach and hands-on system administration.
5
1. / (Root Directory):
The root directory is the top-level directory in the file system hierarchy.
All other directories and files are contained within the root directory.
2. /bin (Binaries):
6
Contains essential executable files (binaries) required for system booting and
basic operations.
Contains boot loader files, kernel images, and other files required for system
booting.
4. /dev (Devices):
Configuration files for various services, applications, and system settings are
stored here.
Contains user home directories, each typically named after the corresponding
username.
Users have read and write access to their respective home directories.
7. /lib (Libraries):
Essential libraries for system functions and services are stored here.
Used as a temporary mount point for mounting external storage devices such as
USB drives and network shares.
9. /opt (Optional):
7
Contains optional software packages or applications installed by the system
administrator.
Virtual file system that provides information about running processes and
system resources.
Root user has full administrative privileges and can access system-wide
directories and files.
Used for storing temporary files created by system processes and users.
Subdirectories include /usr/bin for user binaries, /usr/sbin for system binaries,
/usr/lib for libraries, and /usr/share for shared data.
Contains variable data files that may change frequently during system
operation.
8
Subdirectories include /var/log for log files, /var/spool for print and mail spool
files, and /var/tmp for temporary files.
Detail View
/Home Directory
Contains normal users home directory details.
ls /home
To add another user to the system, we use the adduser or useradd. Using
adduser Simon
we get an error (Permission Denied). this is because the ec2-user does not have the permission
for adding a user. To view the current user in the system, run
whoami
The root user is the user with the permission to add other users. So, in order to add a user using
the ec2-user, we muss add sudo to the command (the sudo command gives the ec2-user
temporary admin priveledges).
9
The above users we’ve added are normal users, to add a user as a root user, we can use the
/bin
Contain binary files [commands]
1. ls:
Lists directory contents.
Example: ls.
2. whoami:
Prints the current username.
Example: whoami.
3. clear:
Clears the terminal screen.
Example: clear.
4. cd:
Changes the current directory.
Example: cd /path/to/directory.
5. chmod:
Changes file permissions.
Example: chmod u+x filename.
BUT some commands like the adduser are not found in the bin directory but rather in the sbin
directory.
/Sbin
Contains system binary files to be execute by the administrator
1. adduser:
10
2. passwd:
3. chage:
Interview Question
In Linux, the 'bin' and 'sbin' directories serve different purposes. The 'bin' directory, short
for 'binaries,' contains essential executable files that are fundamental to the basic operations
of the system. These binaries are accessible to all users of the system. On the other hand,
the 'sbin' directory, short for 'system binaries,' contains executable files that are primarily
used for system administration tasks. These binaries are typically meant to be executed by
the root user or other administrative users with elevated privileges. In summary, while both
directories contain executable files, 'bin' houses essential binaries for general use, whereas
'sbin' is reserved for binaries used specifically for system administration.
11
/etc
Contains configuration files
/etc/ssh/sshd_config: Configuration file for the SSH server (sshd), specifying server
settings such as authentication methods and port number.
cat /etc/ssh/sshd_config
cat /etc/shadow
/etc/passwd: Contains basic information about user accounts, such as usernames and
user IDs.
cat /etc/passwd
/proc
Provides access to kernel data structures and system information.
meminfo:
cat /proc/meminfo
cpuinfo:
cat /proc/cpuinfo
12
Installing Packages:
Here's the format for installing packages along with examples:
b. yum/dnf: Package management tool for Red Hat-based systems like CentOS.
Downloading Links:
When you need to download files or content from the internet directly to your system, you can
use the following commands:
a. wget:
Usage:
Example:
wget https://example.com/file.txt
b. curl:
Description: Another versatile command-line tool for transferring data with URLs.
13
Debian-based systems: sudo apt-get install curl
Usage:
Example:
curl -O https://example.com/file.txt
2. Connect via ssh into the server (make sure that ssh port 22 is open).
File:
Files can contain text, images, programs, or any other type of data.
Each file is identified by a unique name and can be accessed, modified, or deleted by
the user or the operating system.
Directory:
Directories can contain files and other directories, creating a hierarchical structure.
They provide a way to organize and manage files efficiently, making it easier for users
to locate and access the information they need.
14
Example: ls (lists files and directories in the current directory).
Or
Example:
15
mkdir class1
mkdir class1/class2
mkdir class1/class2/class3
mkdir class1/class2/class3/class4
This sequence of commands creates each directory (class1, class2, class3, and
class4) one after the other. Each mkdir command creates a single directory.
This approach allows you to create the directory structure step by step,
creating each level as needed.
Example: echo "I am a DevOps Engineer" (displays the text "I am a DevOps
Engineer" on the terminal).
Example: echo "I am a DevOps Engineer" > dev (redirects the output of the
echo command to a file named "dev").
16
Example: tree (displays the directory tree structure starting from the current
directory).
Example: rm -rf directory (forcefully removes the directory and its contents
recursively without prompting for confirmation).
Example: cat -n file.txt (displays the contents and the total number of lines of
file.txt).
17
Example: find /path/to/search -name "filename" (searches for files named
"filename" in the specified path).
Example: sort -k2 file.txt (sorts lines in "file.txt" based on the second column).
18
Example: wc -w file.txt (displays the number of words in "file.txt").
Text Editors:
Text editors are essential tools for editing files and creating new ones in a Linux environment.
Here are two popular text editors along with examples of how to use them:
1. Nano:
Nano is a simple and user-friendly text editor available on most Linux systems.
Saving Changes: After making changes, press Ctrl + O to save the file.
Entering Edit Mode: Press i to enter insert mode and start typing.
Saving Changes: In normal mode, type :w and press Enter to save the file.
Exiting Vim: In normal mode, type :q and press Enter to exit vim. If changes
were made and not saved, use :q! to force exit without saving.
Navigation: Use the arrow keys or h, j, k, l to move the cursor. Use gg to move
to the beginning of the file and G to move to the end.
19
File Permission and Ownership:
In Linux, each file and directory has three types of permissions, which are represented by three
sets of letters or numbers:
1. Owner Permissions: These permissions apply to the user who owns the file or
directory.
2. Group Permissions: These permissions apply to the group associated with the file or
directory.
3. Other Permissions: These permissions apply to all other users on the system who are
not the owner and not in the group associated with the file or directory.
Each set of permissions consists of three characters, representing the ability to read, write, and
execute the file or directory:
w (Write): Allows modifying or deleting the file or directory, or adding new files to a
directory.
x (Execute): Allows executing the file or accessing contents of the directory (for
directories).
1. Symbolic Notation:
r: Read permission.
w: Write permission.
x: Execute permission.
-: No permission.
For example, rwx indicates read, write, and execute permissions, while r-- indicates read-only
permissions.
2. Numeric Notation:
Each permission is assigned a numeric value: read (4), write (2), and execute
(1).
20
These values are then added together to represent the permissions.
The numeric notation is calculated separately for the owner, group, and other permissions,
resulting in three-digit numbers.
Here are some examples of how file permissions are represented using both symbolic and
numeric notation:
Symbolic Notation:
1. rwxr-xr--: Owner has read, write, and execute permissions; group has read and execute
permissions; others have read-only permissions.
2. rw-rw-r--: Owner and group have read and write permissions; others have read-only
permissions.
3. rwxr--r--: Owner has read, write, and execute permissions; group and others have read-
only permissions.
Numeric Notation:
1. 755: Owner has read, write, and execute permissions (4 + 2 + 1 = 7); group and others
have read and execute permissions (4 + 1 = 5).
2. 644: Owner has read and write permissions (4 + 2 = 6); group and others have read-
only permissions (4).
3. 744: Owner has read, write, and execute permissions (4 + 2 + 1 = 7); group and others
have read-only permissions (4).
Here are examples of how to change permissions of files and directories using the chmod
command:
21
To grant read, write, and execute permissions to the owner of a file: chmod
u+rwx file.txt
To revoke write permission from the group of a directory: chmod g-w directory
To grant read and write permissions to all (owner, group, and others) for a file:
chmod a+rw file.txt
To grant read, write, and execute permissions to the owner of a file: chmod 700
file.txt
To revoke write permission from the group of a directory: chmod 755 directory
To grant read and write permissions to all (owner, group, and others) for a file:
chmod 666 file.txt
To set specific permissions for a file: chmod 644 file.txt (Owner: read/write,
Group: read, Others: read)
22
Detailed view:
Umask value:
The umask value, short for "user file creation mask," is a set of permissions that determine the
default permissions applied to newly created files and directories by a user. It works by
subtracting the umask value from the maximum permissions (usually 777 for directories and
666 for files) to determine the actual permissions.
The umask value is typically exp ressed in octal notation and can be viewed or modified using
the umask command in a terminal.
Each digit in the umask value represents the permissions that are masked (i.e.,
removed) from the maximum permissions.
The first digit corresponds to the owner's permissions, the second to the group's
permissions, and the third to others' permissions.
For example, a umask value of 022 means that write permissions (2) are masked for
both the group and others, but not for the owner.
23
022: Default for many distributions. Removes write permissions for group and others,
leaving read and execute permissions intact.
027: Removes write and execute permissions for group and others, leaving read
permissions intact.
077: Removes all permissions for group and others, leaving only the owner with read,
write, and execute permissions.
002: Less common. Removes write permissions for others but allows all permissions
for the owner and group.
Interview Questions:
Others:
Here are examples of how to change ownership and group ownership of files and directories
using the chown and chgrp commands:
24
To change the owner and group of a file: chown new_owner:new_group
file.txt
Setting Date:
Use the date command to display or set the system date and time.
Example: date MMDDHHMMYYYY (sets the system date and time to the
specified value).
Setting Timezones:
25
User and Group Administration Commands:
User and group administration commands in Linux are essential for managing user accounts
and groups on the system. These commands allow system administrators to create, modify, and
delete user accounts, as well as manage group memberships and permissions.
1. useradd / adduser:
useradd: Command-line utility to create new user accounts in Linux.
adduser: Interactive command for adding new user accounts with additional
configuration options.
Example: sudo adduser username (interactively prompts for user details and
creates a new user account).
2. userdel:
3. passwd:
4. chage:
Example: sudo chage -E 2024-12-31 username (sets the password expiry date
for the user account "username" to December 31, 2024).
5. groupadd / groupdel:
26
groupdel: Command to delete groups from the system.
6. usermod:
7. id:
Example: id username (displays user and group information for the user
"username").
8. groups:
Example: groups username (displays the groups the user "username" belongs
to).
9. lid:
Command to display information about users and groups in a more user-friendly format.
10. su / sudo:
27
DEMO: User Account Management and Permissions
Creation of User Accounts:
Create user accounts for Louis and Hyson. Verify the successful creation of both accounts.
Password Assignment:
Assign passwords to the Louis and Hyson accounts to ensure secure access. Verify the
successful assignment of passwords.
Group Membership:
Add Louis to the "presidents" group. Confirm the update of Louis's membership in the group.
Administrative Privileges:
Account Expiration:
Solution to Task
1. Creation of User Accounts:
Explanation: The useradd command is used to create new user accounts. Here, we create user
accounts for Louis and Hyson using the sudo command to execute them with administrative
privileges.
Verification:
28
Explanation: We use the cat command to display the contents of the /etc/passwd file, which
stores user account information. The grep command is used to filter the output and check if the
user accounts for Louis and Hyson have been successfully created.
2. Password Assignment:
Explanation: The passwd command is used to assign passwords to user accounts. Here, we set
passwords for the Louis and Hyson accounts to ensure secure access.
Verification:
Explanation: We use the tail command to display the last few lines of the /etc/shadow file,
which stores encrypted password information. The grep command is used to filter the output
and verify that passwords have been successfully assigned to the Louis and Hyson accounts.
3. Group Membership:
Explanation: The first command checks if the group named "presidents" already exists in the
/etc/group file. We create a new group called "presidents" using the groupadd command.
Then, we add Louis to the "presidents" group using the usermod command with the -aG option
to append Louis to the group.
Verification:
Explanation:
We use the groups command to display the groups that Louis belongs to. This verifies
that Louis has been successfully added to the "presidents" group.
29
The id command displays user and group information for the user "Louis", including
the groups that Louis belongs to.
The lid command provides a more detailed and user-friendly view of user and group
information, including supplementary group memberships, for the user "Louis". We use
sudo to execute lid with administrative privileges.
4. Administrative Privileges:
Open the /etc/sudoers file using a text editor like vi: sudo vi /etc/sudoers.
Add the following line to grant 'Louis' user sudo access without a password:
OR
2. Create a separate file in /etc/sudoers.d/ directory:
This command uses echo to output the required configuration line and tee to
write it to a file named "Louis" in the /etc/sudoers.d/ directory.
After granting sudo access, Louis can switch to the root user by running:
sudo su - Louis
This command switches to the user "Louis" with elevated privileges, allowing administrative
tasks to be performed without repeatedly entering a password.
30
sudo vi /etc/ssh/sshd_config
sudo sed -i
"/^[^#]*PasswordAuthentication[[:space:]]no/c\PasswordAuthenti
cation yes" /etc/ssh/sshd_config
After modifying the configuration, restart the SSH service to apply the changes:
or
ssh Louis@server_ip
This will establish an SSH connection to the server using Louis's credentials, allowing external
access with password authentication.
5. Account Expiration:
31
Explanation: Specifies the minimum number of days that must pass before the
password can be changed. Here, we set it to 90 days.
Explanation: Indicates the last date the password was changed. By default, it
shows the current date. You can leave it as is or specify a different date if needed.
Explanation: Specifies the number of days after the password has expired before
the account is disabled. Setting it to -1 means the account will not be disabled
due to inactivity.
Explanation: Specifies the date when the account will expire. Setting it to -1
means the account will not expire based on a specific date. Here, we set it to
2928 days (8 years) from the current date.
Verification:
Explanation: We use the chage command with the -l option to display the account aging
information for Louis, including the expiration date. This verifies that the expiration date has
been successfully set for Louis's account.
32
System Information:
System information commands in Linux are used to gather various details about the system,
including user information, file locations, command descriptions, disk usage, and hostname.
Example: users.
Example: whoami
9. id: Command to display user and group information for the current user.
Example: id
10. whereis: Command to locate binary, source, and manual page files for a command.
33
Example: whatis command
Example: hostname
Process Management:
Process management in Linux involves monitoring, controlling, and interacting with running
processes on the system. It includes tasks such as viewing processes, terminating them,
adjusting their priorities, and monitoring system resource usage.
Example: top (displays dynamic information about CPU and memory usage).
Example: killall firefox (terminates all processes with the name "firefox").
5. sar: Collects, reports, and saves system activity information such as CPU, memory,
disk, and network usage.
Examples:
34
Task: Deployment and Verification of Apache HTTP Server.
Install httpd and Verify Installation:
Start the httpd service on your system to make the web server operational.
Open a web browser and access the default page served by the httpd server to confirm
that it is accessible.
Commands:
1. systemctl:
Usage:
35
List all services: systemctl list-unit-files
2. service:
Usage:
Task Solution:
1. Install httpd and Verify Installation:
Expected Output: Active (running) status indicating that the httpd service is
operational.
Ensure that the required port (typically port 80) is open in the security group.
Open a web browser and enter the public IP address or domain name of your
Amazon Linux instance.
36
Expected Outcome: The default page served by the Apache HTTP server should
be accessible.
Question:
Networking:
1. ping: Send ICMP echo requests to a host.
Example: netstat -tuln (displays a list of listening TCP and UDP ports).
Example: netstat -tulpn (displays a list of listening TCP and UDP ports along
with the PID and program name of each listening process).
Usage: When you forget the function of a command, use whatis followed by the command
name to quickly remind yourself of its purpose.
Example:
whatis ls
37
Output:
Benefits:
Handy tool for both beginners and experienced users to refresh their memory on
command functionalities.
I have extensive experience with Linux systems, particularly in the areas of system
administration, server management, and open-source technologies. Here are some key aspects
of my experience:
Answer:
I have extensive experience with Linux systems, particularly in managing and configuring
various distributions such as Ubuntu, CentOS, and Amazon Linux. Here’s an overview of my
key experiences:
1. Server Deployment and Management: I have created and managed Linux servers,
including the setup of Amazon Linux instances on AWS. This involved tasks like
launching instances, configuring security groups, and ensuring proper SSH access.
3. File System Structure: I have a solid grasp of the Linux file system structure, including
directories like /etc for configuration files, /proc for system information, and /var for
logs. I am familiar with commands for navigating and managing the file system, such
as ls, cd, chmod, touch, and mkdir.
38
4. Process Management: I can efficiently manage system processes using commands like
ps, top, kill, killall and sar. These skills help in monitoring system performance and
managing resources effectively.
8. Networking and SSH: I am skilled in configuring SSH for secure remote access,
including setting up password authentication and managing SSH keys. I can also access
servers externally and manage network settings and firewalls.
My experience with Linux systems has equipped me with the necessary skills to manage,
configure, and troubleshoot various Linux environments effectively, ensuring smooth and
secure system operations
Introduction: In addition to the basic and service management commands, there are several
other commands that are incredibly useful for system administration, monitoring, networking,
and file management.
1. cal:
2. script:
39
Example: script session.log (starts recording the terminal session to a file
named session.log).
3. telnet:
4. history:
5. *cat /etc/release:
Description: Displays the contents of the release files to identify the Linux
distribution and version.
6. watch:
7. restart:
Example: sudo systemctl restart httpd (restarts the Apache HTTP server
service).
8. reboot:
9. tar:
40
Examples:
10. zip:
Example: zip archive.zip file1 file2 (creates a ZIP archive containing file1 and
file2).
11. unzip:
These commands expand your toolkit for managing Linux systems, offering capabilities for
system monitoring, networking, file management, and automation.
41