wiki:interface_renaming_scripts
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
wiki:interface_renaming_scripts [2018/06/19 10:51] – walkeradmin | wiki:interface_renaming_scripts [2023/03/09 22:35] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Interface Renaming Scripts ====== | ====== Interface Renaming Scripts ====== | ||
<color darkorange> | <color darkorange> | ||
- | \\ | ||
\\ | \\ | ||
\\ | \\ | ||
---- | ---- | ||
+ | <color # | ||
+ | ---- | ||
+ | \\ | ||
+ | \\ | ||
For anyone that has used CentOS/Red Hat prior to version 7, or indeed other versions of Linux, the norm for network interface naming is to use **eth0, eth1, eth2** etc. | For anyone that has used CentOS/Red Hat prior to version 7, or indeed other versions of Linux, the norm for network interface naming is to use **eth0, eth1, eth2** etc. | ||
\\ | \\ | ||
Line 37: | Line 40: | ||
Now execute the file using the following syntax: | Now execute the file using the following syntax: | ||
\\ | \\ | ||
- | ./ | + | ./ |
\\ | \\ | ||
\\ | \\ | ||
A file network_mapping.csv will have been created. you can view this file using: | A file network_mapping.csv will have been created. you can view this file using: | ||
- | \\ | ||
\\ | \\ | ||
cat network_mapping.csv | cat network_mapping.csv | ||
\\ | \\ | ||
Here is an example of what I have: | Here is an example of what I have: | ||
- | \\ | ||
\\ | \\ | ||
enp4s0f0; | enp4s0f0; | ||
Line 56: | Line 57: | ||
\\ | \\ | ||
The file should contain the following: | The file should contain the following: | ||
- | \\ | ||
\\ | \\ | ||
interface; | interface; | ||
Line 64: | Line 64: | ||
etc. | etc. | ||
\\ | \\ | ||
+ | There are two issues here: | ||
+ | \\ | ||
+ | 1. The interface names need to be eth0, eth1 etc. | ||
+ | 2. The MAC Addresses are in the incorrect order, and this will break the mapping. | ||
+ | \\ | ||
+ | Both of these are easy to fix. Firsly you can manually edit the file, so that the interface names are correct, but more importantly, | ||
+ | \\ | ||
+ | \\ | ||
+ | Use <color # | ||
+ | \\ | ||
+ | ip link show | ||
+ | | ||
+ | 1: lo: < | ||
+ | link/ | ||
+ | 2: ens513f0: < | ||
+ | link/ether 00: | ||
+ | 3: ens513f1: < | ||
+ | link/ether 00: | ||
+ | 4: ens513f2: < | ||
+ | link/ether 00: | ||
+ | 5: ens513f3: < | ||
+ | link/ether 00: | ||
+ | 6: enp4s0f0: < | ||
+ | link/ether 00: | ||
+ | 7: enp4s0f3: < | ||
+ | link/ether 00: | ||
+ | \\ | ||
+ | From here we can see which MAC is associated with which interface (so I know that enp4s0f0 is the first interface on my server) this allows me to build the following file: | ||
+ | \\ | ||
+ | eth0; | ||
+ | eth1; | ||
+ | eth2; | ||
+ | eth3; | ||
+ | eth4; | ||
+ | eth5; | ||
+ | \\ | ||
+ | So now the <color # | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | ---- | ||
+ | ===rename_network_interfaces.sh=== | ||
+ | \\ | ||
+ | The next part is much simpler. Copy the rename_network_interfaces.sh to your server, and give it execute privileges using the following syntax: | ||
+ | \\ | ||
+ | chmod 777 rename_network_interfaces.sh | ||
+ | \\ | ||
+ | Now execute the script using the following syntax: | ||
+ | \\ | ||
+ | ./ | ||
+ | \\ | ||
+ | \\ | ||
+ | If successful the following output will be observed (depending on what interface scripts already existed). | ||
+ | \\ | ||
+ | < | ||
+ | *** Getting macaddr to register *** | ||
+ | *** Using network_mapping.csv for network interface mapping *** | ||
+ | Backup of / | ||
+ | Backup of / | ||
+ | Backup of / | ||
+ | Backup of / | ||
+ | Backup of / | ||
+ | Backup of / | ||
+ | Generating file / | ||
+ | Generating file / | ||
+ | Generating file / | ||
+ | Generating file / | ||
+ | Generating file / | ||
+ | Generating file / | ||
+ | </ | ||
+ | \\ | ||
+ | Check the / | ||
+ | \\ | ||
+ | Note: No configuration information is copied from the old interface scripts, so before you reboot, you need to re-add all of your IP/ | ||
+ | \\ | ||
+ | \\ | ||
+ | Reboot the server to check the interfaces have been renamed, and the mapping is correct. | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===The Scripts=== | ||
+ | \\ | ||
+ | <color # | ||
+ | \\ | ||
+ | < | ||
+ | #!/bin/bash | ||
+ | |||
+ | # this script is based on infos got from blog page: | ||
+ | # http:// | ||
+ | |||
+ | # exit at first error | ||
+ | set -o errexit | ||
+ | |||
+ | export IP_INTERFACES=" | ||
+ | export INTERFACE_ANCHOR=" | ||
+ | export MACADDR_ANCHOR=" | ||
+ | |||
+ | export OUTPUT_FILE | ||
+ | |||
+ | function show_help() { | ||
+ | cat <<- EOF | ||
+ | Generate csv file with interface name; mac addr. Output file is a mandatory parameter. | ||
+ | --output | ||
+ | --help|-h | ||
+ | |||
+ | EOF | ||
+ | } | ||
+ | |||
+ | while [ $# -gt 0 ] ; do | ||
+ | case $1 in | ||
+ | --output) OUTPUT_FILE=" | ||
+ | --help|-h) show_help; | ||
+ | *) if ! [ -z " | ||
+ | esac | ||
+ | done | ||
+ | |||
+ | [ -z " | ||
+ | |||
+ | rm -f " | ||
+ | |||
+ | echo " | ||
+ | interface_name_list=$($IP_INTERFACES | grep " | ||
+ | interface_name_list=($interface_name_list) | ||
+ | macaddr_list=$($IP_INTERFACES | grep " | ||
+ | macaddr_list=($macaddr_list) | ||
+ | cnt=0 | ||
+ | arraySize=" | ||
+ | |||
+ | while [ " | ||
+ | echo " | ||
+ | cnt=$(( cnt + 1 )) | ||
+ | done | ||
+ | |||
+ | </ | ||
+ | <color # | ||
+ | \\ | ||
+ | < | ||
+ | #!/bin/bash | ||
+ | |||
+ | # this script is based on infos got from blog page: | ||
+ | # http:// | ||
+ | |||
+ | # exit at first error | ||
+ | set -o errexit | ||
+ | |||
+ | IP_INTERFACES=" | ||
+ | MACADDR_ANCHOR=" | ||
+ | NET_NAME_TEMPLATE=" | ||
+ | |||
+ | CSV_FILE= | ||
+ | |||
+ | NETWORK_SCRIPT_DIR="/ | ||
+ | NETWORK_BACKUP_DIR="/ | ||
+ | NETWORK_SCRIPT_TEMPLATE=' | ||
+ | DEVICE="< | ||
+ | ONBOOT=yes | ||
+ | NETBOOT=yes | ||
+ | BOOTPROTO=dhcp | ||
+ | TYPE=Ethernet | ||
+ | # HWADDR key is mandatory to use custom interface name: do not delete it | ||
+ | HWADDR=< | ||
+ | ' | ||
+ | |||
+ | function show_help() { | ||
+ | cat <<- EOF | ||
+ | Update network interfaces names. A csv file with the expected mapping can be given as arg. | ||
+ | --csv Csv file containing < | ||
+ | --help|-h | ||
+ | |||
+ | EOF | ||
+ | } | ||
+ | |||
+ | while [ $# -gt 0 ] ; do | ||
+ | case $1 in | ||
+ | --csv) CSV_FILE=" | ||
+ | --help|-h) show_help; | ||
+ | *) if ! [ -z " | ||
+ | esac | ||
+ | done | ||
+ | |||
+ | echo "*** Getting macaddr to register ***" | ||
+ | macaddr_list=$($IP_INTERFACES | grep " | ||
+ | |||
+ | if ! [ -z " | ||
+ | echo "*** Using $CSV_FILE for network interface mapping ***" | ||
+ | ! [ -d " | ||
+ | for old_script_file in $(find $NETWORK_SCRIPT_DIR -maxdepth 1 -name " | ||
+ | echo -e " | ||
+ | mv -f " | ||
+ | done | ||
+ | while read interface_data | ||
+ | do | ||
+ | [ -z " | ||
+ | interface_name=$(echo " | ||
+ | mac_addr=$(echo " | ||
+ | [ -z " | ||
+ | [ -z " | ||
+ | | ||
+ | # check that given mac addr exist | ||
+ | if ! echo " | ||
+ | echo " | ||
+ | exit 1 | ||
+ | fi | ||
+ | echo -e " | ||
+ | echo " | ||
+ | sed -i " | ||
+ | sed -i " | ||
+ | done < " | ||
+ | |||
+ | else | ||
+ | echo "*** Using default naming (${NET_NAME_TEMPLATE}x) for network interface mapping ***" | ||
+ | ! [ -d " | ||
+ | cnt=0 | ||
+ | for old_script_file in $(find $NETWORK_SCRIPT_DIR -maxdepth 1 -name " | ||
+ | echo -e " | ||
+ | mv -f " | ||
+ | done | ||
+ | for mac_addr in ${macaddr_list[*]}; | ||
+ | mac_addr=$(echo " | ||
+ | echo -e " | ||
+ | echo " | ||
+ | sed -i " | ||
+ | sed -i " | ||
+ | cnt=$(( cnt + 1 )) | ||
+ | done | ||
+ | fi | ||
+ | |||
+ | echo "*** Please check consistency of generated files in directory $NETWORK_SCRIPT_DIR ***" | ||
+ | |||
+ | |||
+ | </ | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
wiki/interface_renaming_scripts.1529401909.txt.gz · Last modified: 2023/03/09 22:35 (external edit)