Lecture6_Linux network for students .pptx

IslamReda28 11 views 61 slides Jun 22, 2024
Slide 1
Slide 1 of 61
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61

About This Presentation

This is for students who are interested in basics network linux


Slide Content

Introduction to Ubuntu Networking Package Management LXC SSH sed AWK 1 Contents

Introduction to Ubuntu Networking 2

Basic Networking Commands ifconfig Used to configure network interfaces Example: ifconfig ip More modern and versatile replacement for ifconfig Example: ip a to show IP addresses

Displaying Network Configuration ip a Displays all IP addresses and network interfaces ip link Shows the status of network interfaces

Network Interface Management ip link set To bring up or down network interfaces Example: ip link set eth0 up ifup / ifdown Commands to activate or deactivate interfaces Example: sudo ifdown eth0

Viewing Network Routes ip route Displays the IP routing table Example: ip route show Netstat Displays network connections, routing tables, interface statistics Example: netstat - rn

Checking Network Connectivity ping Checks network connectivity to a host Example: ping google.com traceroute Traces the path packets take to a network host Example: traceroute google.com

Troubleshooting Tips dmesg View kernel-related messages Example: dmesg | grep eth0 journalctl View system logs Example: journalctl -u networking

Debian Software Package Management with dpkg 9

Introduction to Debian and dpkg What is dpkg ?: Low-level package manager for Debian-based systems Example : Debian is known for its robust stability and large software repository. dpkg is used to handle .deb packages directly.

Understanding dpkg Bullet Points : Package Structure: .deb files, control files, data files, scripts Key Components: Control file, data files, installation scripts Example : A typical .deb package includes: control file : Contains metadata like package name, version, dependencies. data files : Actual software code and libraries. post-installation script : Runs after installation to configure the package.

Core dpkg Commands Bullet Points : Installing Packages: dpkg - i < package.deb > Removing Packages: dpkg -r <package-name> Listing Packages: dpkg -l Example: Install a package : dpkg - i mysoftware_1.0-1_amd64.deb Remove a package : dpkg -r mysoftware List installed packages : dpkg -l | grep mysoftware

Example (Installing nginx) 1- Download the Package : Command: wget http://nginx.org/packages/ubuntu/pool/nginx/n/nginx/nginx_1.22.1-1~focal_amd64.de b 2- Install the Package: Command: sudo dpkg - i nginx_1.22.1-1~focal_amd64.deb 3- Verify Installation:: Command: sudo systemctl start nginx sudo systemctl status nginx 4- Accessing nginx: Command: curl http://localhost

apt for Package Management 14

apt for Package Management Introduction to apt : apt is a higher-level command-line interface for package management in Debian-based systems. Simplifies common tasks for users.

Example Update package lists : sudo apt update Upgrade all installed packages : sudo apt upgrade Install a package : sudo apt install nginx Remove a package : sudo apt remove nginx sudo apt purge nginx sudo apt autoremove

apt-get for Package Management 17

apt for Package Management Introduction to apt-get : apt-get is an older, more feature-rich command-line tool for package management. Provides more control over package management tasks.

Example Update package lists : sudo apt-get update Upgrade all installed packages : sudo apt-get upgrade Install a package : sudo apt-get install nginx Remove a package : sudo apt-get remove nginx sudo apt-get purge nginx sudo apt-get autoremove

Linux Containers (LXC) 20

Introduction to Linux Containers (LXC) Definition: LXC is a userspace interface for the Linux kernel containment features. How It Works: Containers share the host system’s kernel, providing isolated environments without the overhead of a full OS for each container. Advantages: Lightweight, efficient, fast startup, low resource consumption.

Key Features of LXC Isolation: Each container has its own filesystem, network interface, and process tree. Resource Control: Limits on CPU, memory, disk I/O, and network bandwidth. Security: Containers can be sandboxed to prevent unwanted access between them. Portability: Containers can be moved easily between different Linux systems.

How LXC Works Kernel Features: Uses cgroups and namespaces for process isolation and resource management. Container Lifecycle: Creation, running, stopping, and deleting containers. Tools & Commands: lxc -create, lxc -start, lxc -stop, lxc -destroy.

Setting Up LXC (Demo) Debian/Ubuntu sudo apt update sudo apt install lxc Creating a New Container:Command : lxc -create -n mycontainer -t download -- -- dist ubuntu --release focal --arch amd64 Starting the Container: Command lxc -start -n mycontainer Attaching to the Container: lxc -attach -n mycontainer Listing Containers: lxc -ls Stopping a Container: lxc -stop -n mycontainer Deleting a Container: lxc -destroy -n mycontainer

Introduction to SSH 25

Introduction to SSH What is SSH? Definition: Secure Shell (SSH) is a cryptographic network protocol designed for secure communication over an unsecured network. Purpose: It provides a secure method for accessing and managing remote servers and devices. The default SSH port is 22 Key Features: Secure Remote Login: Users can log in to remote machines securely. Secure File Transfer: Tools like SCP (Secure Copy) and SFTP (Secure File Transfer Protocol) allow secure file transfers. Port Forwarding: Enables secure tunneling of other network protocols through SSH.

How SSH Works Key Components: Client and Server: Client: The device used to initiate the SSH connection. Server: The device that accepts and responds to the SSH connection. Authentication Methods: Password-Based: The client provides a password to authenticate. Public Key-Based: Uses a pair of cryptographic keys (public and private) for authentication. The server has the public key, and the client uses the private key. Encryption Techniques: Ensures confidentiality and integrity of data exchanged. Uses algorithms like AES , Blowfish, and 3DES.

How SSH Works Steps in an SSH Session: Initiation: Client initiates connection to the SSH server. Key Exchange: Server sends its public key. Client and server negotiate encryption. Authentication: Client authenticates using a password or private key. Session Establishment: A secure session is established, allowing encrypted communication.

Use Cases of SSH Remote Administration: Managing and configuring remote servers securely. Example: A system administrator accessing a remote Linux server. Secure File Transfers: Transferring files securely using SCP or SFTP. Example: Developers deploying code or transferring logs. Port Forwarding: Securely forwarding ports to protect data transmission. Example: Accessing a database server behind a firewall. Automated Scripts: Running automated scripts for backups and updates. Example: Automated deployment of software updates across multiple servers.

Setting Up LXC (Demo with ssh) Creating a New Container:Command : lxc -create -n mycontainer -t download -- -- dist ubuntu --release focal --arch amd64 Start the container: lxc -start -n mycontainer Attach to the container: lxc -attach -n mycontainer Install SSH (Ubuntu/Debian): apt update ;apt install openssh -server -y Start and enable SSH service: systemctl start ssh; systemctl enable ssh Exit the container: - exit Find the container’s IP address: lxc -info -n mycontainer | grep IP

SSH using password Configure Password Authentication: Ensure the container has a user with a password: passwd ubuntu Edit SSH configuration to ensure password authentication is enabled: nano / etc /ssh/ sshd_config Ensure the following lines are set: PasswordAuthentication yes Restart SSH service: systemctl restart ssh Find the Container’s IP Address: lxc -info -n mycontainer | grep IP SSH into the Container Using Password: ssh [email protected]

SSH using key-based authentication Generate SSH Key Pair on Host (if not already created): ssh-keygen -t rsa -b 2048 Follow the prompts to save the key pair. Copy Public Key to Container: ssh-copy-id [email protected] : Replace username with your SSH user. Replace 192.168.1.x with the container’s IP address. Alternatively, manually copy the public key: cat ~/.ssh/ id_rsa.pub | ssh [email protected] ' mkdir -p ~/.ssh && cat >> ~/.ssh/ authorized_keys ' Configure Key-Based Authentication in the Container: Edit SSH configuration to ensure key-based authentication is enabled: nano / etc /ssh/ sshd_config Ensure the following lines are set: PubkeyAuthentication yes AuthorizedKeysFile .ssh/ authorized_keys Restart SSH Service: systemctl restart ssh SSH into the Container Using Key: ssh - i ~/.ssh/ id_rsa [email protected]

Shell Scripting 33

The Streamlined Editor What is sed ? How sed works Addressing Commands Examples 34

What is sed ? It is a streamline, non-interactive editor. It performs the same kind of tasks as in vi. It doesn’t change your file unless the output is saved with shell redirection. 35

How does sed Work? The sed editor process a file (input) one line at a time and sends its output to the screen. The sed stores the line it process in a buffer and once processing is finished the line is sent to the screen (unless command was delete) and the next line is read and the process is repeated until the last line is reached. 36

Addressing Addressing is used to determine which lines to be edited. The addressing format can be Number Regular expression Both * Number represents a line number. 37

Commands The sed commands tell sed what to do with the line: Print it Remove it Change it The sed format sed ‘command’ filename 38

Examples To print lines contain the pattern root $ sed ‘/root/p’ myfile sherine maha root root user To suppresses the default behavior of the sed $ sed –n ‘/root/p’ myfile root 39

Examples To print lines from maha to root $ sed –n ‘/ maha /,/root/p’ myfile maha root To print lines from 2 to the line that begins with us $ sed –n ‘2,/^us/p’ myfile maha root user 40

Examples To delete the third line $ sed ‘3d’ myfile sherine maha user To delete the last line $ sed ‘$d’ myfile sherine maha root 41

Examples To delete lines from 1 to 3 $ sed ‘1,3d’ myfile user To delete from line 3 to the end $ sed ‘3,$d’ myfile sherine maha To delete lines containing root pattern $ sed ‘/root/d’ myfile sherine maha user 42

Examples To substitute islam by iaskar $ sed ‘s/ sherine / sbahader /g’ myfile sbahader maha root user $ sed –n ‘s/ sherine / sbahader / gp ’ myfile sbahader 43

Examples To issue multi command $ sed –e ‘2d’ –e ‘s/ sherine / sbahader /g’ myfile sbahader root user 44

“?|| N N The AWK Utility What is AWK? What does AWK stands for? The awk’s format Records and Fields Examples BEGIN Pattern END Pattern Conditional Expressions Loops Examples 45

What is AWK? awk is a programming language used for manipulating data and generating reports. awk scans a file line by line, searching for lines that match a specified pattern performing selected actions 46

What does AWK stands for? awk stands for the first initials in the last names of each authors of the language, Alfred Aho , Peter Weinberger, and Brian Kernighan 47

Awk’s Format The awk program consists of awk command Program instructions enclosed in quotes Input file or default stdin . $ awk ‘instructions’ inputfile 48

Records and fields By default, each line is called a record and terminated with a new line. Record separators are by default carriage return, stored in a built-in variables ORS and RS . The $0 variable is the entire record. The NR variable is the record number. 49

Records and fields Each record consists of words called fields which by default separated by white spaces. NF variables contains the number of fields in a record FS variable holds the input field separator, space/tab is the default. 50

Examples To print the first field of the file, but as the default delimiter is white space, you have to specify the delimiter $ awk –F: ‘{print $1}’ / etc / passwd root daemon sherine … $ awk –F: ‘{print “ Logname :”,$1}’ / etc / passwd Logname:root Logname:daemon Logname:sherine … 51

Examples To display the whole file (cat) $ awk ‘{print $0}’ / etc / passwd root:x:0:1:Super-user:/:/ sbin / sh ... To display the file numbered (cat -n) $ awk ‘{print NR,$0}’ / etc / passwd 1 root:x:0:1:Super-user:/:/ sbin / sh ... To display number of fields (words) in each record (line) $ awk –F: ‘{print $0,NF}’ / etc / passwd root:x:0:1:Super-user:/:/ sbin / sh 7 ... 52

BEGIN Pattern BEGIN Pattern is followed by an action block that is executed before awk process any line from the input file. BEGIN action is often used to change the value of the built-in variables, FS, RS, and so forth to assign initial values to user-defined variables and print headers of titles. Example $ awk ‘BEGIN{FS=“:”; RS=“\n\n”} {print $1,$2,$3}’ myfile 53

END Pattern END patterns are handled after all lines of input have been processed. It does not match any input line Example: To print the number of lines in a file $ awk ‘END { print NR } ’ testfile 54

condition expression1 ? expression2 :expression3 if (expression1) expression2 else expression3 if (expression1){ statement; statement;... } else if (expression2){ statement; statement;... } else { statement } Conditional expressions condition expression1 ? expression2 :expression3 if (expression1) expression2 else expression3 55

condition expression1 ? expression2 :expression3 if (expression1) expression2 else expression3 if (expression1){ statement; statement;... } else if (expression2){ statement; statement;... } else { statement } Conditional expressions if (expression1){ statement; statement;... } else if (expression2){ statement; statement;... } else { statement } 56

Operator Meaning < Less than <= Less than and equal == Equal to != Not equal to >= Greater than and equal > Greater than ~ Match regular expression !~ Not matched by regular expression Relational Operators 57

Loops while Loop $ awk –F: ’{ i =1; while ( i <NF) {print NF,$ i;i ++}}’ / etc / passwd For Loop $ awk ‘{for ( i =1 ; i <NF; i ++) print NF,$ i }’ / etc / passwd 58

Examples (cont.) The variable max value is set according to compression between the first 2 fields: $ awk ‘{if ($1>$2) max=$1; else max=$2; print max}’ testing Arithmetical Operations $ awk ‘{if ($1*$2>100) print $0}’ testing 59

Examples (cont.) To display line 4 and 5 only $ awk '{if (NR==4 || NR==5) print NR ":" $0}' / etc /passwd 60

THANKS 61