Examples of NMCLI Scripts

 


Examples of NMCLI Scripts



Assigning a Static IP Address

#!/bin/bash

IP_ADDRESS="$1"

GATEWAY="$2"

if [ -z "$IP_ADDRESS" ] || [ -z "$GATEWAY" ]; then

    echo "Usage: $0 <IP_ADDRESS> <GATEWAY>"

    exit 1

fi

nmcli con mod eth0 ipv4.method manual

nmcli con mod eth0 ipv4.address "$IP_ADDRESS"/24

nmcli con mod eth0 ipv4.gateway "$GATEWAY"

nmcli con up eth0

 

Configuring DHCP Settings


#!/bin/bash nmcli con mod eth0 ipv4.method auto nmcli con up eth0


Adding a Secondary IP Address


#!/bin/bash IP_ADDRESS="$1" if [ -z "$IP_ADDRESS" ]; then echo "Usage: $0 <IP_ADDRESS>" exit 1 fi nmcli con mod eth0 +ipv4.address "$IP_ADDRESS"/24 nmcli con up eth0


Configuring DNS Servers


#!/bin/bash DNS_SERVERS="$1" if [ -z "$DNS_SERVERS" ]; then echo "Usage: $0 <DNS_SERVER_1 DNS_SERVER_2 ...>" exit 1 fi nmcli con mod eth0 ipv4.dns "$DNS_SERVERS" nmcli con up eth0


Add a Virtual Bridge Using NMCLI


 nmcli connection show ## Show existing Connection

 nmcli connection add type bridge ifname br0 ## Create bridge connection

 nmcli connection add type bridge-slave ifname enp0s1 master br0 ## Adding device to Bridge

 nmcli connection add type bridge-slave ifname enp0s2 master br0

  nmcli connection up br0   ## Activate the bridge connection to apply the changes.

 nmcli connection show  ## Check the Connection

 nmcli con show --active   ## Check active Connection

 nmcli connection modify br0 ipv4.addresses 192.168.2.2/24  ## Adding IP to Bridge

 nmcli connection modify br0 ipv4.method manual

 nmcli connection up br0

 nmcli con show br0 | grep addresses  ## Check the details 



Comments

Popular posts from this blog

RHEL - How to back out a failed patch

Vathsa's- Linux - SysOps and DevOps

Local Yum Repository for Oracle Linux 8