Linux/ip

From Omnia
Revision as of 18:07, 15 April 2026 by Kenneth (talk | contribs) (→‎VLAN)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Install ip tool

apt install iproute2

Add static IP

sudo ip addr add 192.168.1.2/24 dev eth0;
sudo ip route add default via 192.168.1.1 dev eth0;

Remove static IP

sudo ip addr del 192.168.1.2/24 dev eth0;

Bring interface online

ip link set dev <interface> up
ip link set dev <interface> down

Interface Alias

Change name: (change enp39s0 to eth0)

ip link set enp39s0 name eth0

Add Alias: (add eth0 to enp39s0)

ip link property add dev enp39s0 altname eth0
ip link property del dev enp39s0 altname eth0

Add Temporary IP to Interface

ip a add 192.168.178.2/24 dev eth0

Route

Print routes:

ip route

Add default route:

sudo ip route add default via 192.168.1.1 dev eth0;

Add route:

sudo ip route add 172.10.1.0/24 via 192.168.1.1 dev eth0;

VLAN

The 8021q module is often loaded by default. If necessary, you can make sure that the module is loaded by issuing the following command as root:

# modprobe --first-time 8021q
modprobe: ERROR: could not insert '8021q': Module already in kernel
To display information about the module, issue the following command:
$ modinfo 8021q

802.1Q VLAN Tagging using ip command:

ip link add link enp1s0 name enp1s0.8 type vlan id 8
# or
ip link add link enp1s0 name vlan100 type vlan id 100
# Syntax: sudo ip link add link [parent] name [vlan_name] type vlan id [vlan_id]
sudo ip link add link eth0 name eth0.100 type vlan id 100

Activate interface:

sudo ip link set dev eth0.100 up
sudo ip link set dev eth0 up
sudo ip link set dev vlan100 up

Assign ip:

sudo ip addr add 192.168.100.10/24 dev eth0.100

OR DHCP:

dhclient -v eth0.100
# to release
dhclient -r eth0.100

To view the VLAN:

ip -d link show enp1s0.8

Show general details:

ip addr show eth0.100

To remove:

sudo ip link set dev eth0.100 down
sudo ip link delete eth0.100
sudo ip link delete enp1s0.8

To use multiple interfaces belonging to multiple VLANs, create locally enp1s0.1 and enp1s0.2 with the appropriate VLAN ID on top of a physical interface enp1s0:

# ip link add link enp1s0 name enp1s0.1 type vlan id 1
   ip link set dev enp1s0.1 up
# ip link add link enp1s0 name enp1s0.2 type vlan id 2
   ip link set dev enp1s0.2 up

tcpdump capture tagged frames:

tcpdump -nnei enp1s0 -vvv

To make permanent: [1]

# install vlan tool:
sudo apt-get install vlan
# load module:
sudo modprobe 8021q
# add interface:
sudo vconfig add eth0 100
# assign ip
sudo ip addr add 10.0.0.1/24 dev eth0.100
# add to modules:
sudo bash -c 'echo "8021q" >> /etc/modules'
/etc/network/interfaces
 auto eth0.100
 iface eth0.100 inet dhcp
# or
auto eth0.100
iface eth0.100 inet dhcp
   vlan-raw-device eth0

netplan version: [2] [3]

network:
    version: 2
    ethernets:
        eno1:
            ... eno1 config ...
    vlans:
        vlan100:
            id: 100
            link: eno1
            addresses: [10.0.0.100/24]
            # dhcp4: true

This will make an interface called vlan100@eno1

ref:

Ways to get Private IP Address

ifconfig

ifconfig

ip addr

# will need to be parsed
ip addr
ip a
ip addr show
ip -br -c a
# parsed
ip route get 1 | awk '{print $(NF-2);exit}' 
ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p'
ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p'
read -r _{,} gateway _ iface _ ip _ < <(ip r g 1.0.0.0) ; printf '%-12s %s\n'  gateway $gateway iface $iface ip $ip # [4]

hostname

# will need to be parsed
hostname -I
# parsed
hostname -I | cut -d' ' -f1

Ways to Get IPv4 Public IP Address

curl

curl ip.ixota.com
curl ifconfig.me
curl api.ipify.org
curl icanhazip.com

wget

wget -qO- ipecho.net/plain

dig

dig +short myip.opendns.com @resolver1.opendns.com

Ways to Get IPv6 Public IP Address

curl

curl https://i-p.show
curl https://ipv6.icanhazip.com
curl http://ip6only.me/api/   # not as clean
curl -6 icanhazip.com
curl -6 https://ifconfig.co
curl -6 ip.sb
curl -6 https://ipv6.icanhazip.com
curl https://api64.ipify.org

keywords