Steve Litt via plug on 26 Dec 2023 19:18:44 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Configuration of manual IP with commands without ifconfig


Michael Lazin via plug said on Tue, 26 Dec 2023 13:59:56 -0500

>I am hoping someone can give me hints on configuration of a manual IP
>using the "ip" command instead of ifconfig.  I don't want to make a
>confrontation file just configure it manually like I used to with the
>ifconfig command. Is this possible?  Thanks for your help.
>
>Michael Lazin

Hi Michael,

I can give you simple confrontrations and complex confrontrations. :-)

The following is a simple configuration to simply install a static IP
address:

====================================================
#!/bin/sh
hostname=`grep -v "^\s*#"  /etc/hostname | head -n1`
ip link set dev lo up
ip link set dev enp6s1 down
ip addr add 192.168.100.188/24 dev enp6s1
ip link set dev enp6s1 up
ip route add default via 192.168.100.96
====================================================

Please understand the preceding is so simple that it can error out if
rerun: It's meant to be run once at boot time. This being said, I've
usually been able to get networking running again by simply running it
again and ignoring the error messages.


The following is a complex ip command based configuration shellscript,
with provisions to completely erase the old network config before
resetting the network, suitable for a qemu LAN-Peer:

===========================================
#!/bin/sh

use_bridge=1
use_tap=0

dev="enp40s0"
ipaddr_major="192.168.0.2"
ipaddr_minor="192.168.0.102"
gateway="192.168.0.1"

error_tap_without_bridge(){
   echo -n "ERROR: Can\'t set TAP without "
   echo -n "BRIDGE! "
   echo Aborting...
   exit 1
}


enable_ip_forwarding(){
   echo 1 > /proc/sys/net/ipv4/ip_forward
}

unset_everything(){
   dev=$1
   ip_maj=$2
   ip_min=$3
   gateway=$4
   ip link set dev lo down
   echo "Unsetting everything for $dev, $ip_maj and $ip_min"
   ip link set dev tap0 down
   brctl delif br0 tap0
   ip link del tap0
   ip link set dev br0 down
   ip addr del $ip_min/24 dev br0
   ip addr del $ip_maj/24 dev br0
   brctl delbr br0
   ip link set dev $dev down
   ip addr del $ip_min/24 dev $dev
   ip addr del $ip_maj/24 dev $dev
   echo ""
}

set_hostname_and_localhost(){
   echo "Setting hostname and localhost"
   hostname=`grep -v "^\s*#"  /etc/hostname | head -n1`
   ip link set dev lo up
   echo ""
}

create_phys_device_link(){
   dev=$1
   echo Creating device link for $dev
   ip link set dev $dev up
   echo ""
}

set_phys_device_addr(){
   dev=$1
   ip_maj=$2
   ip_min=$3
   gateway=$4
   echo -n "Setting physical device addresses "
   echo -n "$ip_maj "
   echo -n "and $ip_min "
   echo -n "for $physdev "
   echo "with gateway $gateway"
   ip link set dev $dev down
   ip addr add $ip_maj/24 dev $dev
   ip addr add $ip_min/24 dev $dev
   ip link set dev $dev up
   ip route add default via $gateway
   echo ""
}

set_bridge(){
   dev=$1
   ip_maj=$2
   ip_min=$3
   gateway=$4
   echo Setting bridge for $dev
   echo -n "Creating and setting bridge addresses "
   echo -n "$ip_maj "
   echo -n "and $ip_min "
   echo -n "for $physdev "
   echo "with gateway $gateway"

   ip link add name br0 type bridge
   ip link set dev $dev master br0
   ip addr add $ip_maj/24 dev br0
   ip addr add $ip_min/24 dev br0
   ip link set dev br0 up
   ip route add default via $gateway
   echo ""
}

set_tap(){
   echo Setting tap
   ip tuntap add tap0 mode tap
   brctl addif br0 tap0
   ip link set dev tap0 up
   echo ""
}

show_networking(){
   echo -n "Networking follows in 3 seconds..."
   sleep 3
   echo "\n"
   echo "========================================"
   echo "========================================"
   ip -4 link
   echo "......................"
   ip -4 addr
   echo "......................"
   ip -4 route
   echo "========================================"
   echo "========================================"
}

echo "\nBegin upnet.sh"

[ "$use_tap" = "1" ] && [ "$use_bridge" != "1" ] && \
   error_tap_without_bridge

unset_everything $dev $ipaddr_major $ipaddr_minor $gateway

enable_ip_forwarding

set_hostname_and_localhost

create_phys_device_link $dev $ipaddr_major $ipaddr_minor $gateway

[ "$use_bridge" = "1" ] || \
   set_phys_device_addr $dev $ipaddr_major $ipaddr_minor $gateway

[ "$use_bridge" = "1" ] && set_bridge $dev \
   $ipaddr_major $ipaddr_minor $gateway

[ "$use_tap" = "1" ] && \
   set_tap $dev $ipaddr_major $ipaddr_minor $gateway

show_networking
===========================================

SteveT

Steve Litt 

Autumn 2023 featured book: Rapid Learning for the 21st Century
http://www.troubleshooters.com/rl21
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug