Saturday, June 4, 2022
HomeNetworkingUse bash to vary an IP deal with from dynamic to static...

Use bash to vary an IP deal with from dynamic to static on Linux


Altering the IP deal with of a Linux system from dynamic to static isn’t tough, however requires slightly care and a set of instructions that you just seemingly not often use. This publish gives a bash script that may run by the method, accumulate the wanted data after which problem the instructions required to make the adjustments whereas asking as little as doable from the individual operating it.

The majority of the script focusses on ensuring that the proper settings are used. For instance, it collects the 36-charater universally distinctive identifier (UUID) from the system so that you just by no means must sort it in or copy and paste it into place.

After asking solely a query or two, the script runs a collection of nmcli (Community Supervisor) instructions that may change the IP deal with if requested and set the deal with to be static. Discover that quite a few the fields within the instructions that it’ll run as proven under are variables derived from earlier instructions within the script.

sudo nmcli connection modify $UUID IPv4.deal with $IP/$sz
sudo nmcli connection modify $UUID IPv4.gateway $router
sudo nmcli connection modify $UUID IPv4.technique guide
sudo nmcli connection down $UUID
sudo nmcli connection up $UUID

If a brand new IP deal with is requested, the script will test the format of the deal with requested and make it possible for it’s suitable with the present community.

Previous to operating the instructions proven above, the script collects the variables used to ensure the instructions are arrange appropriately—largely by operating instructions that report on the present settings. The outstanding UUID proven in every of them is collected from the output of this command:

$ nmcli connection present
NAME                UUID                                  TYPE      DEVICE
Wired connection 1  ec33e575-8059-3a20-b7a5-58393deb1783  ethernet  enp0s25

The UUID is saved within the script by the command like that proven under in order that it may be used within the nmcli instructions proven earlier.

$ UUID=`nmcli connection present | tail -1 | awk ‘{print $4}’`
$ echo $UUID
ec33e575-8059-3a20-b7a5-58393deb1783

The command under pulls the subnet specification from an ip a command like that proven under. The outcome shall be one thing like “192.168.0.4/24” the place the 24 (i.e., 24 bits) tells us that the primary three bytes of the deal with (192.168.0) signify the community.

$ subnet=`ip a | grep “inet “ | tail -1 | awk ‘{print $2}’`
$ echo $subnet
192.168.0.11/24

Getting almost all of the wanted data from the system fairly than asking the person to offer it makes the method lots simpler and, doubtlessly, extra dependable.

The script additionally provides you the choice of adjusting the present IP deal with in case you don’t prefer it. Whereas this can go away you disconnected from the system by the point the script ends, you possibly can instantly log again in utilizing the brand new IP deal with.

Notice that the script makes use of sudo for the ultimate instructions as a result of these instructions will change system settings.

Right here is the script which I name “set_static_IP”:

#!/bin/bash

# get subnet
subnet=`ip a | grep “inet “ | tail -1 | awk ‘{print $2}’`

# get router/gateway
router=`ip route present | head -1 | awk ‘{print $3}’`

# get dimension of community portion of deal with in bytes
sz=`echo $subnet | awk -F / ‘{print $2}’`
bytes=`expr $sz / 8`
prefix=`echo $subnet | minimize -d. -f1-$bytes`      # e.g., 192.168.0

# get IP deal with to be set
IP=`hostname -I | awk ‘{print $1}’` # present IP
echo -n “Maintain IP deal with?—$IP [yn]> “
learn ans if [ $ans =="n" ]; then echo -n “Enter new IP deal with: “ learn IP # test if specified IP is correctly formatted if [[ ! $IP =~ ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]]; then echo Invalid IP fi # test if specified IP works for native community if [[ ! $IP =~ ^$prefix ]]; then echo “ERROR: Specified IP not usable for native community” exit fi fi # test if specified IP is correctly formatted if [[ ! $IP =~ ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]]; then echo Invalid IP fi # fetch the UUID UUID=`nmcli connection present | tail -1 | awk ‘{print $4}’` # UUID=`nmcli connection present | head -2 | tail -1 | awk ‘{print $3}’` # Mint # run instructions to arrange the everlasting IP deal with sudo nmcli connection modify $UUID IPv4.deal with $IP/$sz sudo nmcli connection modify $UUID IPv4.gateway $router sudo nmcli connection modify $UUID IPv4.technique guide sudo nmcli connection up $UUID

The script was examined on each Fedora 35 and Linux Mint. Discover that the command for fetching the UUID on Mint is completely different as a result of the output of the “nmcli connection present” command is completely different. The command for Mint is included within the script however commented out for a simple swap.

Interplay with the script will seem like this:

$ ./set_static_IP
Maintain IP deal with?—192.168.0.18 [yn]> y
[sudo] password for yourid:
Connection efficiently activated (D-Bus lively path: /org/freedesktop/NetworkManager/ActiveConnection/24)

After operating the script, I ran a discover command to search for very not too long ago up to date recordsdata and located this one that features the “guide” (i.e., static) setting specified within the thrid command proven above.

$ sudo cat “/and so forth/NetworkManager/system-connections/Wired connection 1.nmconnection”
[connection]
id=Wired connection 1
uuid=ec33e575-8059-3a20-b7a5-58393deb1783
sort=ethernet
autoconnect-priority=-999
interface-name=enp0s25
permissions=
timestamp=1653746936

[ethernet]
mac-address-blacklist=

[ipv4]
address1=192.168.0.11/24,192.168.0.1
dns-search=
technique=guide

[ipv6]
addr-gen-mode=stable-privacy
dns-search=
technique=auto

[proxy]

To search out solely very not too long ago created or up to date recordsdata, you should utilize a command like this that appears for recordsdata which were modified within the final 14.4 minutes:

$ sudo discover . -mtime -.01 -print

In case this isn’t clear, the -mtime variable when set to 1 means “1 day”. Since a day is 1440 minutes lengthy, .1 would imply 144 minutes and .01 means 14.4 minutes.

Wrap-Up

Dynamic addressing is nice besides when it isn’t. It isn’t when the system in query is one you must hook up with pretty usually fairly than one you solely join from. Realizing the best way to change an IP deal with from dynamic to static generally is a massive deal once you want an IP deal with that doesn’t change.

Be part of the Community World communities on Fb and LinkedIn to touch upon matters which might be high of thoughts.

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments