-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-static-ip.sh
More file actions
executable file
·81 lines (63 loc) · 1.89 KB
/
set-static-ip.sh
File metadata and controls
executable file
·81 lines (63 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env bash
# For a given interface and an IP/Mask fixes the IP of the interface to that using also enables autoconnect
# JavierRibaldelRío
# H11
set -e
# ==============================
# Usage check
# ==============================
if [ "$#" -ne 2 ]; then
echo "Usage: sudo $0 <interface> <ip/mask>"
echo "Example: sudo $0 eth0 192.168.0.1/24"
exit 1
fi
INTERFACE="$1"
IPADDR="$2"
CONN_NAME="${INTERFACE}-static"
# ==============================
# Root check
# ==============================
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (sudo)."
exit 1
fi
# ==============================
# Check NetworkManager
# ==============================
if ! systemctl is-active --quiet NetworkManager; then
echo "Error: NetworkManager is not running."
exit 1
fi
# ==============================
# Check interface exists
# ==============================
if ! ip link show "$INTERFACE" > /dev/null 2>&1; then
echo "Error: Interface $INTERFACE does not exist."
exit 1
fi
echo "Configuring $INTERFACE with static IP $IPADDR..."
# ==============================
# Delete existing connections on this interface
# ==============================
nmcli -t -f NAME,DEVICE connection show | grep ":$INTERFACE$" | cut -d: -f1 | while read -r name; do
echo "Removing existing connection: $name"
nmcli connection delete "$name"
done
# ==============================
# Create new static connection
# ==============================
nmcli connection add \
type ethernet \
ifname "$INTERFACE" \
con-name "$CONN_NAME" \
ipv4.method manual \
ipv4.addresses "$IPADDR" \
ipv4.never-default yes \
connection.autoconnect yes \
connection.wait-device-timeout 0
# ==============================
# Activate connection
# ==============================
nmcli connection up "$CONN_NAME"
echo "Done."
echo "$INTERFACE is now permanently configured as $IPADDR"