Module 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

MODULE 5

INTRODUCTION TO LINUX PROGRAMMING

5.1 Overview of Linux architecture


Linux is an open-source UNIX-based operating system. The main component of the Linux
operating system is Linux kernel.
Components of Linux:
Like any operating system, Linux consists of software, computer programs, documentation,
and hardware.
The main components of Linux operating system are: Application, Shell, Kernel, Hardware,
Utilities.

Fig: Linux Architecture

The architecture of Linux is composed of kernel, shell and application programs that is
software.

 HARDWARE: physical parts of a computer, such as central processing unit (CPU),


monitor, mouse, keyboard, hard disk and other connected devices to CPU.
 KERNEL: A kernel is a computer program and is the central, core part of an
operating system. It manages the operations of the computer and the hardware, most
notably memory and CPU time. It is an integral part of any operating system.
 SHELL: Shell is an environment in which we can run our commands, programs, and
shell scripts. It is a user interface for access to an operating system's services. (User
interface program execution, file system manipulation, input/output operations,
communication, resource allocation, error detection, security and protection)
 Application programs/software: An application, or application program, is a
software program that runs on your computer. It is excited by user. Some inbuilt
application programs in Linux are terminal, Firefox browser, Libre office.

5.2Types of shells
1. The Bourne Shell –

It is denoted as sh

 It was written by Steve Bourne at AT&T Bell Labs. It is the original UNIX shell.
It is faster and more preferred. It lacks features for interactive use like the ability
to recall previous commands. It also lacks built-in arithmetic and logical
expression handling. It is default shell for Solaris OS. For the Bourne shell the:

Command full-path name is /bin/sh and /sbin/sh,


Non-root user default prompt is $,
Root user default prompt is #.

2. The Korn Shell

It is denoted as ksh

 It was written by David Korn at AT&T Bell Labs. It is a superset of the


Bourne shell. So it supports everything in the Bourne shell.It has interactive
features. It includes features like built-in arithmetic and C-like arrays,
functions, and string-manipulation facilities. It is faster than C shell. It is
compatible with script written for C shell. For the Korn shell the:

Command full-path name is /bin/ksh,


Non-root user default prompt is $,
Root user default prompt is #.
3. The C Shell –

It is denoted as csh

 Bill Joy created it at the University of California at Berkeley. It incorporated


features such as aliases and command history. It includes helpful programming
features like built-in arithmetic and C-like expression syntax. In C shell:

Command full-path name is /bin/csh,


Non-root user default prompt is hostname %,
Root user default prompt is hostname #.

4. Bash Shell –

Bash is a command-line interpreter or Unix Shell and it is widely used in


GNU/Linux Operating System. It is written by Brian Jhan Fox. It is used as a
default login shell for most Linux distributions. Scripting is used to automate the
execution of the tasks so that humans do not need to perform them individually.
Bash scripting is a great way to automate different types of tasks in a system.
Developers can avoid doing repetitive tasks using bash scripting.
Bash scripting supports variables, conditional statements, and loops just like
programming languages.

Ex:

#!/bin/bash

echo "Hello, GeeksforGeeks"

Shell Scripting
Basic Linux Commands
File and Directory Commands:
1. ls: List directory contents.
o Syntax: ls [options] [directory]
o Example: ls -l /home/user
2. cd: Change the current directory.
o Syntax: cd [directory]
o Example: cd /home/user
3. pwd: Print the working directory.
o Syntax: pwd
o Example: pwd
4. mkdir: Create a new directory.
o Syntax: mkdir [directory]
o Example: mkdir new_folder
5. rmdir: Remove an empty directory.
o Syntax: rmdir [directory]
o Example: rmdir old_folder
6. rm: Remove files or directories.
o Syntax: rm [options] [file/directory]
o Example: rm file.txt
o Example: rm -r directory_name
7. cp: Copy files or directories.
o Syntax: cp [options] source destination
o Example: cp file.txt /home/user/backup/
o Example: cp -r folder_name /home/user/backup/
8. mv: Move or rename files or directories.
o Syntax: mv [source] [destination]
o Example: mv file.txt /home/user/new_location/
o Example: mv old_name.txt new_name.txt
9. cat: Concatenate and display file content (including creating a file, editing a file, and
displaying a file).
o Syntax: cat [options] [file]
o Example (Creating a file): cat > newfile.txt
 (Then type the content and press Ctrl+D to save)
o Example (Editing a file): cat >> existingfile.txt
 (Then add the content and press Ctrl+D to save)
o Example (Displaying a file): cat file.txt
10. head: Display the beginning of a file.
o Syntax: head [options] [file]
o Example: head -n 10 file.txt
11. tail: Display the end of a file.
o Syntax: tail [options] [file] Example: tail -n 10 file.txt
File Permissions in Linux:
In Linux, each file and directory have a set of permissions that determine who can read,
write, or execute them. These permissions are divided into three categories:
1. Owner: The user who owns the file.
2. Group: A group of users who share the same permissions.
3. Others: All other users on the system.
Each category can have three types of permissions:
 Read (r): Permission to read the contents of the file or list the directory.
 Write (w): Permission to modify the contents of the file or create/delete files in the
directory.
 Execute (x): Permission to execute the file as a program or search the directory.
Viewing File Permissions
To view the permissions of a file or directory, you can use the ls -l command. This command
lists the contents of a directory in long format, showing detailed information about each file,
including its permissions.
 Syntax: ls -l [directory]
 Example: ls -l /home/user
The output looks like this:

-rwxr-xr—

Here’s what each character represents:


1. The first character indicates the type of file (- for a regular file, d for a directory, l for
a symbolic link, etc.).
2. The next three characters represent the owner's permissions (read, write, execute).
3. The next three characters represent the group's permissions.
4. The last three characters represent others' permissions.

Changing File Permissions


To change the permissions of a file or directory, you can use the chmod command. There are
two ways to specify permissions: using symbolic representation and using numeric (octal)
representation.
Symbolic Representation
 Syntax: chmod [who][operation][permissions] [file]
 Example: chmod u+rwx,g+rx,o-r file.txt
In the example above:
 u (user/owner) is granted read (r), write (w), and execute (x) permissions.
 g (group) is granted read (r) and execute (x) permissions.
 o (others) is removed read (r) permission.
Operations include:
 +: Adds the specified permission(s).
 -: Removes the specified permission(s).
 =: Sets the specified permission(s), removing others.
Numeric (Octal) Representation
Permissions can also be represented using octal numbers:
 Read (r): 4
 Write (w): 2
 Execute (x): 1
Each category (owner, group, others) is represented by a digit (sum of permissions).
 Syntax: chmod [permissions] [file]
 Example: chmod 755 file.txt
In the example above:
 7 (owner) = 4 (read) + 2 (write) + 1 (execute)
 5 (group) = 4 (read) + 1 (execute)
 5 (others) = 4 (read) + 1 (execute)
Examples of Setting Permissions
1. Read, Write, and Execute for Owner; Read and Execute for Group and Others
o Symbolic: chmod u=rwx,g=rx,o=rx file.txt
o Numeric: chmod 755 file.txt
2. Read and Write for Owner; Read Only for Group and Others
o Symbolic: chmod u=rw,g=r,o=r file.txt
o Numeric: chmod 644 file.txt
3. Read and Write for Owner; No Permissions for Group and Others
o Symbolic: chmod u=rw,g=,o= file.txt
o Numeric: chmod 600 file.txt

4. Execute Only for Owner; No Permissions for Group and Others


o Symbolic: chmod u=x,g=,o= file.txt
o Numeric: chmod 100 file.txt
5. Add Write Permission for Group
o Symbolic: chmod g+w file.txt
o Numeric: chmod 664 file.txt (assuming the original permission was 644)
Changing Ownership
To change the owner and group of a file or directory, you can use the chown command.
 Syntax: chown [owner][:group] [file]
 Example: chown user:group file.txt
In this example, user is the new owner, and group is the new group.
By understanding and using these commands, you can effectively manage file and directory
permissions in Linux, ensuring the right users have the appropriate access.

Control Statements
Control statements are used to alter the flow of the script based on conditions. In shell
scripting, the primary control statements are if, if-else, if-elif-else, case, and exit.
1. If Statement
The if statement is used to test a condition, and if the condition is true, the script will execute
a block of commands.
Syntax:
if [ condition ]
then
# commands to execute if condition is true
fi

Example:
#!/bin/bash
AGE=20
if [ $AGE -ge 18 ]
then
echo "You are an adult."
fi

2 If-Else Statement
The if-else statement allows you to specify what should happen if the condition is true and
also what should happen if it is false.
Syntax:
if [ condition ]
then
# commands to execute if condition is true
Else
# commands to execute if condition is false
fi
Example:

#!/bin/bash

AGE=16

if [ $AGE -ge 18 ]

then

echo "You are an adult."

else

echo "You are a minor."


fi

3 If-Elif-Else Statement
The if-elif-else statement is used when there are multiple conditions to check. If the first
condition is false, the script checks the second condition (elif), and if none of the conditions
are true, it executes the else block.
Syntax:

if [ condition1 ]

then

# commands for condition1


elif [ condition2 ]

then

# commands for condition2

else

# commands if none of the above conditions are true


fi

Example:

#!/bin/bash

NUMBER=5

if [ $NUMBER -gt 10 ]

then

echo "Number is greater than 10."

elif [ $NUMBER -eq 5 ]

then

echo "Number is equal to 5."

else

echo "Number is less than 5."


fi

2. Looping Statements

Looping statements are used to execute a block of code multiple times.


2.1 For Loop
The for loop is used to iterate over a list or a range of values.

Syntax:
for variable in list

do

# commands to execute
done
example:

#!/bin/bash

for i in 1 2 3 4 5

do

echo "Number: $i"


done

Linux System Programming:


Linux system programming involves writing programs that interact with the Linux operating
system at a low level, often directly interfacing with the kernel or utilizing system calls. This
type of programming typically involves understanding the underlying architecture of the
operating system, managing resources like memory and processes, and using various libraries
and tools available in the Linux environment.

System Calls:
 Basic system calls (e.g., open, read, write, close, fork, exec, wait, exit)
 Process management (fork, exec, wait, kill)

 fork: Creates a new process (as mentioned above).

 exec: Executes a new program in the current process space (as mentioned
above).

 wait: Waits for a child process to finish execution (as mentioned above).
 kill: Sends a signal to a process or a group of processes.

 File operations (open, read, write)


 Inter-process communication (IPC) (e.g., pipes, message queues, shared memory,
semaphores)

Linux Security Fundamentals:

Linux security involves a combination of practices, tools, and technologies to protect the
operating system and data from unauthorized access, vulnerabilities, and threats. Below are
key aspects of Linux security:
1. User and Group Management
 Users and Groups: Linux uses users and groups to manage permissions and access
control. Users are individual accounts, while groups are collections of users.
o User Management Commands:
 useradd, usermod, userdel for managing users.
 passwd for changing passwords.
o Group Management Commands:
 groupadd, groupmod, groupdel for managing groups.
 gpasswd for managing group passwords.
2. File Permissions and Ownership
 Permission Types: Read (r), write (w), and execute (x) permissions.
o Permission Levels: User (owner), group, and others.
 Commands:
o chmod: Changes file permissions.
o chown: Changes file ownership.
o chgrp: Changes group ownership.
 Special Permissions:
o Setuid: Allows users to run an executable with the file owner's privileges.
o Setgid: Allows users to run an executable with the file group's privileges.
o Sticky Bit: Used on directories to restrict file deletion.

User Authentication in Linux:


User authentication in Linux ensures that only authorized users can access the system and its
resources. Linux provides several methods for authentication, including password-based
authentication, SSH keys, and Pluggable Authentication Modules (PAM). Here's a detailed
explanation of the different user authentication mechanisms in Linux:
1. Password-based Authentication
 Password authentication is the most common method where a user provides a
password to authenticate themselves. Passwords are stored in a hashed form in the
/etc/shadow file.
 When a user logs in, the system prompts for a username and password. The entered
password is hashed and compared against the stored hash. If they match, the user is
granted access.
2. Pluggable Authentication Modules (PAM)
 PAM provides a framework for integrating authentication methods into Linux
applications. It allows system administrators to configure authentication policies
without modifying application code.
 PAM modules can control authentication methods like password checking, account
management, session management, and password aging.
 Configuration: PAM configuration files are located in /etc/pam.d/. For example, the
file /etc/pam.d/common-auth defines authentication settings.
3. Sudo Authentication
 Sudo allows users to execute commands as another user, usually the root user, after
authenticating with their own password.
 The configuration for sudo permissions is stored in /etc/sudoers, which can be edited
using the visudo command to ensure syntax correctness.
 Example: A user with sudo permissions might run the following command to install a
package
sudo apt update
sudo apt install package_name
4. SSH Key Authentication
SSH stands for Secure Socket Shell,
 SSH key-based authentication is more secure than password-based authentication. It
uses a pair of cryptographic keys: a private key (stored securely on the client
machine) and a public key (stored on the server).
 To set up SSH key-based authentication, the user generates a key pair with ssh-
keygen, and then copies the public key to the server using ssh-copy-id or manually
appending it to the ~/.ssh/authorized_keys file on the server.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy