Welcome to Centmin Mod Community
Become a Member

Sysadmin how to add new ips in centos 7 the proper way

Discussion in 'System Administration' started by yunos, Aug 28, 2019.

  1. yunos

    yunos Member

    63
    3
    8
    Aug 8, 2015
    Ratings:
    +9
    Local Time:
    2:25 PM
    1.8.0
    i found this in network script so it should be the right file. but how do i add new ips the right way snce last time i did, it didnt reboot properly

    # Generated by dracut initrd
    NAME="enp5s0f1"
    DEVICE="enp5s0f1"
    ONBOOT="yes"
    NETBOOT="yes"
    UUID="a0e27a16-90e5-4570-9a7c-3490b94c2d67"
    IPV6INIT="yes"
    BOOTPROTO="none"
    TYPE="Ethernet"
    PROXY_METHOD="none"
    BROWSER_ONLY="no"
    IPADDR="my main server ip"
    PREFIX="29"
    GATEWAY="GATEWAYIP"
    DNS1="127.0.0.1"
    DEFROUTE="yes"
    IPV4_FAILURE_FATAL="no"
    IPV6_AUTOCONF="yes"
    IPV6_DEFROUTE="yes"
    IPV6_FAILURE_FATAL="no"
     
    Last edited: Aug 28, 2019
  2. eva2000

    eva2000 Administrator Staff Member

    41,350
    9,279
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,236
    Local Time:
    10:25 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    I suggest you try this on a test VPS if possible or if you existing server has snapshot backups, take a backup first. If you don't know what you're doing you can mess things up so hiring/paying someone to do it is another option.

    Adding additional IPs to a server involves 2 main tasks
    1. Adding and registering additional IPs to system network configuration
    2. Configuring Nginx to use those additional IPs

    Additional IPs System Configuration



    For CentOS to add additional IP address, you need to first be assigned additional IP addresses by your web host or order the additional IPs. For instance, OVH calls additional IPs failover IPs.

    OVH has a guide for adding and registering the additional IPs for CentOS and other Linux distributions to the system outlined at Network IP Aliasing | OVH Docs.

    There's a previous discussion for this with tips at Sysadmin - How to add a IPv4 Failover Range to CentOS 7? including scripting the format for each
    /etc/sysconfig/network-scripts/ifcfg-eth0:X file you need to create.

    Example for CentOS 6 & 7



    Example IP range 192.168.2.2 to 192.168.2.10 in SSH or shell script define IPLIST variable listing all additional IP addresses in single space separated list and define i variable with starting value of 0. Then a for ip in $IPLIST loop incrementing i variable by 1 for each IP listed in IPLIST
    Code (Text):
    IPLIST="192.168.2.2 192.168.2.3 192.168.2.4 192.168.2.5 192.168.2.6 192.168.2.7 192.168.2.8 192.168.2.9 192.168.2.10"
    i=0
    for ip in $IPLIST; do
      i=$(($i+1))
    echo -e "\n/etc/sysconfig/network-scripts/ifcfg-eth0:${i}"; echo "
    DEVICE="eth0:$i"
    BOOTPROTO=static
    IPADDR="$ip"
    NETMASK="255.255.255.255"
    BROADCAST="xxx.xxx.xxx.xxx"
    ONBOOT=yes";
    echo -e "\nifup eth0:${i}";
    done
    

    Gives following display only output
    Code (Text):
    /etc/sysconfig/network-scripts/ifcfg-eth0:2
    
    DEVICE=eth0:2
    BOOTPROTO=static
    IPADDR=192.168.2.2
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:2
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:3
    
    DEVICE=eth0:3
    BOOTPROTO=static
    IPADDR=192.168.2.3
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:3
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:4
    
    DEVICE=eth0:4
    BOOTPROTO=static
    IPADDR=192.168.2.4
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:4
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:5
    
    DEVICE=eth0:5
    BOOTPROTO=static
    IPADDR=192.168.2.5
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:5
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:6
    
    DEVICE=eth0:6
    BOOTPROTO=static
    IPADDR=192.168.2.6
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:6
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:7
    
    DEVICE=eth0:7
    BOOTPROTO=static
    IPADDR=192.168.2.7
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:7
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:8
    
    DEVICE=eth0:8
    BOOTPROTO=static
    IPADDR=192.168.2.8
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:8
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:9
    
    DEVICE=eth0:9
    BOOTPROTO=static
    IPADDR=192.168.2.9
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:9
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:10
    
    DEVICE=eth0:10
    BOOTPROTO=static
    IPADDR=192.168.2.10
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:10

    Where the first entry /etc/sysconfig/network-scripts/ifcfg-eth0:2 would contain
    Code (Text):
    DEVICE=eth0:2
    BOOTPROTO=static
    IPADDR=192.168.2.2
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    

    and command to start up the interface for initial first time for eth0:2 would be
    Code (Text):
    ifup eth0:2
    


    Example for CentOS 7 Only



    For CentOS 7 there's a 2nd method for adding additional IPs to your system Linux Basics: Assign Multiple IP Addresses To Single Network Interface Card On CentOS 7 | Unixmen. Instead of separate /etc/sysconfig/network-scripts/ifcfg-eth0:X files where X increments, you add additional IPs within the same /etc/sysconfig/network-scripts/ifcfg-eth0 existing file defined by variable IPADDRx where x increments from 1 to number of IP addresses you want to add. Your file might not be named eth0 as it depends on your CentOS 7 network device naming scheme which you can find out via command below to check which network device belongs to your existing public IP address for your server:
    Code (Text):
    ifconfig
    

    So if you existing /etc/sysconfig/network-scripts/ifcfg-eth0 file contain
    Code (Text):
    DEVICE=eth0
    BOOTPROTO=static
    IPADDR=192.168.2.1
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    

    adding additional example IPs above would add these additional entries
    Code (Text):
    IPADDR1="192.168.2.2"
    IPADDR2="192.168.2.3"
    IPADDR3="192.168.2.4"
    IPADDR4="192.168.2.5"
    IPADDR5="192.168.2.6"
    IPADDR6="192.168.2.7"
    IPADDR7="192.168.2.8"
    IPADDR8="192.168.2.9"
    IPADDR9="192.168.2.10"
    

    so /etc/sysconfig/network-scripts/ifcfg-eth0 file with additional IPs would contain
    Code (Text):
    DEVICE=eth0
    BOOTPROTO=static
    IPADDR0=192.168.2.1
    IPADDR1="192.168.2.2"
    IPADDR2="192.168.2.3"
    IPADDR3="192.168.2.4"
    IPADDR4="192.168.2.5"
    IPADDR5="192.168.2.6"
    IPADDR6="192.168.2.7"
    IPADDR7="192.168.2.8"
    IPADDR8="192.168.2.9"
    IPADDR9="192.168.2.10"
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    

    again you can script for this IPLIST variable defining space separated list of additional IP addresses in a for ip in $IPLIST loop with starting i=0 variable that increments by a value of 1 for each IPLIST entry
    Code (Text):
    IPLIST="192.168.2.2 192.168.2.3 192.168.2.4 192.168.2.5 192.168.2.6 192.168.2.7 192.168.2.8 192.168.2.9 192.168.2.10"
    i=0
    for ip in $IPLIST; do
      i=$(($i+1))
      echo "IPADDR${i}=\"$ip\""
    done
    

    changing existing IPADDR line from
    Code (Text):
    IPADDR=192.168.2.1
    

    to
    Code (Text):
    IPADDR0=192.168.2.1
    

    Then restart your networking service
    Code (Text):
    service network restart
    

    check the additional IPs registered on your system via command
    Code (Text):
    ifconfig
    


    Configuring Nginx Additional IP Usage



    To configure Nginx additional IPs, you need to first do the above to add the additional IPs to the system's networking configuration and for the IPs to show up in ifconfig output and be pingable. Nginx can setup Vhosts to use either name based hosting (default) sharing the main IP address or IP based hosting where a Vhost uses a dedicated or different IP address for main IP address. This is controlled by Nginx listen directive How nginx processes a request

    Centmin Mod 123.09beta01 and higher have native support to configure Nginx vhost creation routines to use a separate second IP from the main server IP address via persistent config file /etc/centminmod/custom_config.inc set variable called SECOND_IP discussed and added at Beta Branch - update nginx vhost generator routines support second IP

    Support secondary dedicated IP configuration for centmin mod nginx vhost generator, so out of the box, new nginx vhosts generated will use the defined SECOND_IP=111.222.333.444 where the IP is a secondary IP addressed added to the server.

    You define SECOND_IP variable is centmin mod persistent config file outlined at Upgrade Centmin Mod - CentminMod.com LEMP Nginx web stack for CentOS you manually create the file at /etc/centminmod/custom_config.inc and add SECOND_IP=yoursecondary_IPaddress variable to it which will be registered with nginx vhost generator routine so that any new nginx vhosts created via centmin.sh menu option 2 or /usr/bin/nv or centmin.sh menu option 22, will have pre-defined SECOND_IP ip address set in the nginx vhost's listen directive.

    So in persistent config file you create or already created at /etc/centminmod/custom_config.inc set in it
    Code (Text):
    SECOND_IP=111.222.333.444
    


    So at centmin.sh menu option 2, 22 or nv command runs to create a fresh new Nginx vhost instead of server{} context's listen directive being listed as
    Code (Text):
    listen 80;
    

    with SECOND_IP set it will become
    Code (Text):
    listen 111.222.333.444:80;
    

    for HTTPS on port 443

    instead of
    Code (Text):
    listen 443;
    

    with SECOND_IP set it will become
    Code (Text):
    listen 111.222.333.444:443;
    

    where you defined SECOND_IP=111.222.333.444

    Thus allowing you to use a different server IP for new nginx vhosts generated automatically.

    Now if you want to use a different IP address from main server IP or the one defined in SECOND_IP variable, then right now you would need to manually edit your Nginx vhost config file's listen directive yourself defining the additional IP you added earlier and restart Nginx server afterwards
    Code (Text):
    service nginx restart
    

    or centmin mod command shortcut
    Code (Text):
    ngxrestart
    
     
    • Like Like x 1
    • Winner Winner x 1
  3. yunos

    yunos Member

    63
    3
    8
    Aug 8, 2015
    Ratings:
    +9
    Local Time:
    2:25 PM
    1.8.0
    thank you for the details explanation and since i dont use ovh/failover method im using the second method you proposed.

    i would like to add the ip manually in network config file so im assuming i have to do this


    so if i want to add more ip while keeping my main ip well "main" i would have to change

    IPADDR="X.X.188.82"

    to

    IPADDR0="X.X.188.82"


    and if i wanted to add additional ip it would be

    IPADDR1="X.X.188.83"

    just wanted to make sure i understood this correctly. Since last time i followed guide from a blog site, and resulted in server not booting at all.

    name of the network config ifcfg-enp5s0f1

    # Generated by dracut initrd
    NAME="enp5s0f1"
    DEVICE="enp5s0f1"
    ONBOOT="yes"
    NETBOOT="yes"
    UUID="a0e27a16-90e5-4570-9a7c-3490b94c2d67"
    IPV6INIT="yes"
    BOOTPROTO="none"
    TYPE="Ethernet"
    PROXY_METHOD="none"
    BROWSER_ONLY="no"
    IPADDR="X.X.188.82"
    PREFIX="29"
    GATEWAY="X.X.188.81"
    DNS1="127.0.0.1"
    DEFROUTE="yes"
    IPV4_FAILURE_FATAL="no"
    IPV6_AUTOCONF="yes"
    IPV6_DEFROUTE="yes"
    IPV6_FAILURE_FATAL="no"
     
  4. yunos

    yunos Member

    63
    3
    8
    Aug 8, 2015
    Ratings:
    +9
    Local Time:
    2:25 PM
    1.8.0
    @eva2000 2000 i followed method2 and the server is compeltely dead and cannot connect sigh.

    provider have rescuemodecd but not sure how im able to mount it
     
  5. eva2000

    eva2000 Administrator Staff Member

    41,350
    9,279
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,236
    Local Time:
    10:25 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    might be fact you set BOOTPROTO=none instead of BOOTPROTO=static I outlined but it shouldn't make a difference

    your best bet if you unsure, is to get your web host to add the ips.

    you'd have to ask your web host and consult their documentation
     
    Last edited: Sep 5, 2019
  6. yunos

    yunos Member

    63
    3
    8
    Aug 8, 2015
    Ratings:
    +9
    Local Time:
    2:25 PM
    1.8.0
    yeah i did change to static afterthat, once i remounted my ssd in rescuemode.
    im not sure why my server just simply wont work when i this time litterally copy pasted your example config. Should i create a second network script to add additional ip? My provider is unmanaged so they wont support adding ips understandbly. But im not sure if its worth hiring a sysadmin just do add additonal ip though

    like enp5s0f1:0 for my main server ip
    and my second ip i will clone the network script and add additional ip called

    enp5s0f1:1
     
  7. eva2000

    eva2000 Administrator Staff Member

    41,350
    9,279
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,236
    Local Time:
    10:25 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    you copied and pasted all ? NAME=, DEVICE=, UUID= values are all unique to your network NIC adaptor setup so if you copied and pasted example names, then that could be problem. Also depending on your server setup some have NIC device names one for public and one for private, ensure you're modifying your public one and not private. Again, your web host is the one that would best know how it's configured it could be something specific to their setups too.
    you can try the method 1 outline I did above instead too - that's the one I usually use myself (old-school).
     
  8. yunos

    yunos Member

    63
    3
    8
    Aug 8, 2015
    Ratings:
    +9
    Local Time:
    2:25 PM
    1.8.0
    i use ip address show so im pretty sure theres only public
    but let me try if method 1 works. not sure if it will since its not fail over ip

    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
    valid_lft forever preferred_lft forever
    2: enp5s0f0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
    link/ether b4:99:ba:aa:65:ba brd ff:ff:ff:ff:ff:ff
    3: enp5s0f1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether b4:99:ba:aa:65:bb brd ff:ff:ff:ff:ff:ff
    inet X.X.188.82/29 brd X.X.188.87 scope global noprefixroute enp5s0f1
    valid_lft forever preferred_lft forever
    inet6 fe80::b699:baff:feaa:65bb/64 scope link noprefixroute
    valid_lft forever preferred_lft forever

    enp5s0f0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
    ether b4:99:ba:aa:65:ba txqueuelen 1000 (Ethernet)
    RX packets 0 bytes 0 (0.0 B)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 0 bytes 0 (0.0 B)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    device memory 0xfbe60000-fbe7ffff

    enp5s0f1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet X.X.188.82 netmask 255.255.255.248 broadcast 142.54.188.87
    inet6 fe80::b699:baff:feaa:65bb prefixlen 64 scopeid 0x20<link>
    ether b4:99:ba:aa:65:bb txqueuelen 1000 (Ethernet)
    RX packets 34839 bytes 50551437 (48.2 MiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 7270 bytes 596615 (582.6 KiB)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    device memory 0xfbee0000-fbefffff

    lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
    inet 127.0.0.1 netmask 255.0.0.0
    inet6 ::1 prefixlen 128 scopeid 0x10<host>
    loop txqueuelen 1000 (Local Loopback)
    RX packets 192 bytes 17664 (17.2 KiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 192 bytes 17664 (17.2 KiB)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
     
  9. yunos

    yunos Member

    63
    3
    8
    Aug 8, 2015
    Ratings:
    +9
    Local Time:
    2:25 PM
    1.8.0
    @eva2000 thanks, your method 1 works :) you saved me money from hiring a sysadmin to do one time job haha
     
    • Like Like x 1
  10. eva2000

    eva2000 Administrator Staff Member

    41,350
    9,279
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,236
    Local Time:
    10:25 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    glad to hear !