ipv6privateloopback.sh
· 1021 B · Bash
Raw
#!/bin/sh
# Assuming you want to make a random private /120 prefix in IP and have all of
# those addresses assigned to the loopback interface (dev lo), and you're root,
# and have https://gitlab.com/ipcalc/ipcalc installed, and aren't in a network
# namespace other than global, run:
#
# If you also want to be able to bind to those IP addresses, run the following command:
# sysctl -w net.ipv6.ip_nonlocal_bind = 1
#
#
# IPv4 is obsolete, so IP in this document means IPv6
#
# ~jmjl
# Sets NETWORK and PREFIX with the random network
# and 120, which is the prefix we have hardcoded in this line.
eval "$(ipcalc -r 120 -np)"
# Making it such that netpref is the network + prefix so we can give it to ip
netpref="${NETWORK}/${PREFIX}"
# Adds the address to the loopback interface lo, and sets scope to host as this
# IP will only be valid inside this host
ip addr add "$netpref" dev lo scope host
# Adds a route so that traffic going to the IP range will go to the local
# machine
ip route add local "$netpref" dev lo
1 | #!/bin/sh |
2 | # Assuming you want to make a random private /120 prefix in IP and have all of |
3 | # those addresses assigned to the loopback interface (dev lo), and you're root, |
4 | # and have https://gitlab.com/ipcalc/ipcalc installed, and aren't in a network |
5 | # namespace other than global, run: |
6 | # |
7 | # If you also want to be able to bind to those IP addresses, run the following command: |
8 | # sysctl -w net.ipv6.ip_nonlocal_bind = 1 |
9 | # |
10 | # |
11 | # IPv4 is obsolete, so IP in this document means IPv6 |
12 | # |
13 | # ~jmjl |
14 | |
15 | # Sets NETWORK and PREFIX with the random network |
16 | # and 120, which is the prefix we have hardcoded in this line. |
17 | eval "$(ipcalc -r 120 -np)" |
18 | |
19 | # Making it such that netpref is the network + prefix so we can give it to ip |
20 | netpref="${NETWORK}/${PREFIX}" |
21 | |
22 | # Adds the address to the loopback interface lo, and sets scope to host as this |
23 | # IP will only be valid inside this host |
24 | ip addr add "$netpref" dev lo scope host |
25 | |
26 | # Adds a route so that traffic going to the IP range will go to the local |
27 | # machine |
28 | ip route add local "$netpref" dev lo |
29 |