9.)Lecture-5_Netfjxjjxjdjrworking Commands
9.)Lecture-5_Netfjxjjxjdjrworking Commands
• Static IP Address
(Permanent)
• For Debian-based
systems, you can
configure a static IP
in the
/etc/network/in
terfaces file:
• Restart the
networking service:
Configuring Networking with Command-line
Utilities
• Several utilities can help configure and view network settings:
• ip Command
o View IP addresses
o Assign an IP
Configuring Networking with Command-line
Utilities
• Several utilities can help configure and view network settings:
• nmcli (NetworkManager Command Line Interface)
o Show connections
o Configure a static IP
nmcli connection modify eth0 ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns
"8.8.8.8 8.8.4.4" ipv4.method manual
o Restart NetworkManager:
Important Files
• /etc/network/interfaces
o Used in older versions of Debian and Ubuntu for configuring network
interfaces.
• /etc/netplan/
o For newer Ubuntu versions, network configuration is managed using Netplan
YAML files.
• /etc/hosts
o This file maps IP addresses to hostnames locally, useful for resolving custom
hostnames without DNS.
Important Files
• /etc/resolv.conf
o Specifies DNS servers for name resolution. Manually editing this file is
discouraged if using tools like NetworkManager, as it can overwrite your
changes.
• /etc/hostname
o Stores the system’s hostname. You can change the hostname by editing this
file or using:
Tools and Network Performance Analysis
• ping
o Sends ICMP Echo Requests to test connectivity:
• traceroute
o Displays the path packets take to a network destination:
Tools and Network Performance Analysis
• netstat / ss (netstat is deprecated in favor of ss.)
o View open network connections and listening ports:
• tcpdump
• Network packet analyzer:
Tools and Network Performance Analysis
• iperf
o Network bandwidth testing tool between two systems:
1. Start the server
• Explanation:
o -t nat: Use the NAT (Network Address Translation) table to modify packets.
o -A POSTROUTING: Add a rule to the POSTROUTING chain, which processes packets just before they leave
the system.
o -o eth0: Match packets going out through the eth0 interface (usually the wired network).
o -j MASQUERADE: This targets the MASQUERADE action, which rewrites the source IP address of packets to
the IP address of the outgoing interface (i.e., eth0). This is useful when sharing internet access from one
interface to another (like NAT in routers).
• Use case:
o You’re enabling NAT so that devices on your internal network (like wlan0) can access the internet through the
eth0 interface.
Commands for Connectivity, ARP, Routing,
Switching, VLAN, and NAT Firewall
• Command
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
• Explanation:
o -A FORWARD: Append a rule to the FORWARD chain, which manages packets being routed between different
network interfaces.
o -i eth0: Match packets coming in through eth0.
o -o wlan0: Match packets going out through wlan0 (likely your wireless interface).
o -m state: Use the state module to track the connection state.
o --state RELATED,ESTABLISHED: This allows packets that are part of existing connections or related
connections.
o -j ACCEPT: Accept the packet (allow forwarding).
• Use case:
o This rule ensures that responses to outbound connections (like replies from a web server) can come back
through eth0 and reach the internal network via wlan0.
Commands for Connectivity, ARP, Routing,
Switching, VLAN, and NAT Firewall
• Command
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
• Explanation:
• -A FORWARD: Append a rule to the FORWARD chain to allow packet forwarding.
• -i wlan0: Match packets coming in through the wlan0 interface.
• -o eth0: Match packets going out through the eth0 interface.
• -j ACCEPT: Accept the packet (allow forwarding).
• Use case:
o This rule allows devices on your wireless network (connected to wlan0) to send
packets through the eth0 interface (e.g., to access the internet or other networks
via NAT).
Summary of NAT Commands
• These commands are configuring your system to act as a gateway
between two network interfaces (wlan0 and eth0), enabling internet
sharing and routing. Specifically:
• NAT allows outbound packets to appear as if they are coming from
eth0.
• FORWARD rules allow traffic to flow freely between wlan0 and
eth0.
Summary and More
Concepts
Linux Networking
Understanding Network Devices in Linux
27
Understanding Network Devices in Linux
(continued)
• Kernel modules can be loaded or unloaded while
Linux is running
• /dev/eth0
• First Ethernet card installed on system
• Media Access Control (MAC) address
• Unique address assigned by Ethernet card manufacturer
28
Understanding Network Devices in Linux
(continued)
29
Understanding Network Devices in Linux
(continued)
• arp command
• Display ARP cache
• Mapping of IP addresses to hardware addresses
• Used mainly for troubleshooting network connectivity
• Refreshed frequently
30
Configuration NIC IP address
• NIC: Network Interface Card
• Use “ifconfig” command to determine IP address, interface devices, and
change NIC configuration
• Any device use symbol to determine
• eth0: Ethernet device number 0
• eth1: ethernet device number 1
• lo : local loopback device
• Wlan0 : Wireless lan 0
31
Determining NIC IP Address
[root@tmp]# ifconfig -a
33
Permanent IP configuration
34
File formats for network-scripts
root@network-scripts]# less ifcfg-eth0
DEVICE=eth0
IPADDR=192.168.1.100
NETMASK=255.255.255.0
BOOTPROTO=static
ONBOOT=yes
#
# The following settings are optional
#
BROADCAST=192.168.1.255
NETWORK=192.168.1.0
[root@network-scripts]# 35
Getting the IP Address Using DHCP
[root@tmp]# cd /etc/sysconfig/network-scripts
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
[root@network-scripts]#
36
Activate config change
• After change, the values in the configuration files for the NIC you
must deactivate and activate it for the modifications to take effect.
• The ifdown and ifup commands can be used to do this:
37
Multiple IP Addresses on a Single NIC(1)
[root@tmp]# ifconfig –a
• Shutting down the main interface also shuts down all its aliases
too. Aliases can be shutdown independently of other interfaces 40
The process for creating an IP alias
• Admin should also create a /etc/sysconfig/network-
scripts/ifcfg-wlan0:0 file
• so that the aliases will all be managed automatically
with the ifup and ifdown commands
DEVICE=wlan0:0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.99
NETMASK=255.255.255.0
43
How to Change Default Gateway
[root@tmp]# route add default gw 192.168.1.1 wlan0
NETWORKING=yes
HOSTNAME=bigboy
GATEWAY=192.168.1.1 44
How to Delete a Route
45
Linux router
46
Configuring IP Forwarding
• For your Linux server to become a router, you have
to enable packet forwarding.
• In simple terms packet forwarding enables packets
to flow through the Linux server from one network
to another.
• The Linux kernel configuration parameter to activate
this is named net.ipv4.ip_forward and can be found
in the file /etc/sysctl.conf.
• Remove the "#" from the line related to packet
forwarding.
47
/etc/sysctl.conf changing
Before: # Disables packet forwarding
net.ipv4.ip_forward=0
net.ipv4.ip_forward=1
192.168.1.101 smallfry
• You can also add aliases to the end of the line which enable
you to refer to the server using other names.
• Here we have set it up so that smallfry can also be accessed
using the names tiny and littleguy.
51
Using ping to Test Network Connectivity
• The Linux ping command will send continuous pings, once a second, until
stopped with a Ctrl-C.
• Here is an example of a successful ping to the server bigboy at 192.168.1.100
[root@smallfry tmp]# ping 192.168.1.101
PING 192.168.1.101 (192.168.1.101) from 192.168.1.100 : 56(84) bytes of data.
64 bytes from 192.168.1.101: icmp_seq=1 ttl=128 time=3.95 ms
64 bytes from 192.168.1.101: icmp_seq=2 ttl=128 time=7.07 ms
64 bytes from 192.168.1.101: icmp_seq=3 ttl=128 time=4.46 ms
64 bytes from 192.168.1.101: icmp_seq=4 ttl=128 time=4.31 ms
[root@smallfry tmp]# 52
Using ping to Test Network Connectivity
•Most servers will respond to a ping query it
becomes a very handy tool.
•A lack of response could be due to:
• A server with that IP address doesn't exist
• The server has been configured not to respond to pings
• A firewall or router along the network path is blocking ICMP
traffic
• You have incorrect routing. Check the routes and subnet masks
on both the local and remote servers and all routers in
between.
• Either the source or destination device having an incorrect IP
address or subnet mask.
53
Configuring Networking with Command-line
Utilities
• ifconfig command
• Set up network configuration in Linux kernel
• Parameters include:
• Network interface
• IP address assigned to interface
• Network mask
• Syntax
• ifconfig device ip_address netmask address broadcast address
• $ ifconfig eth0
54
Configuring Networking with Command-line
Utilities (continued)
55
Configuring Networking with Command-line
Utilities (continued)
• View status of interface: ifconfig eth0
• Stop Ethernet interface: ifconfig eth0 down
• Start Ethernet interface: ifconfig eth0 up
• Routing table tells networking software where to send packets that are not
part of local network
• A real example of configuring an Ethernet card at the command line might
look like this:
• # ifconfig eth0 192.168 . 100.1 netmask 255.255.255.0 broadcast 192. 168.100.255
56
Configuring Networking with Command-line
Utilities (continued)
• route command
• View or configure routing table within kernel
• Executed at boot time when networking initialized
• Output information for addresses
• 192.168.100.0 (eth0 IP address)
• 127.0.0.0
• Other
57
Configuring Networking with Command-line
Utilities (continued)
• Route command output
• Destination – Ref
• Gateway – Use
• Genmask – Iface
• Flags
• Add route example:
• route add -net 192.168.100.0 netmask 255.255.255.0 dev eth0
• This command adds a default gateway route,
• # route add default gw 192.168.100.5
58
Configuring Networking with Command-line
Utilities (continued)
• service command
• Start or stop networking
• Relies on script /etc/rc.d/init.d/network
• /etc/sysconfig/networking/devices configuration directory
• Contains file for each network device
• ifcfg-eth0 file
• Used by /etc/rc.d/init.d/network script
• As it executes ifconfig and route commands
59
Changing IP Address/Other Parameters
60
Configuring Networking with Command-line
Utilities (continued)
• ifup and ifdown scripts manage single interface, rather than all network
interfaces
• Example:
• # ./ifup eth0
• # ./ifdown eth0
• Some systems have two or more physical network devices
61
Configuring Networking with Command-line
Utilities (continued)
• IP forwarding
• Allows packets to be passed between network interfaces
• Required for any router
• To enable:
• # echo 1 > /proc/sys/net/ipv4/ip_forward
62