Skip to content

Instantly share code, notes, and snippets.

@evandrocoan
Last active February 1, 2023 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandrocoan/31bf27bdbdeab05237e82476b09cc0d2 to your computer and use it in GitHub Desktop.
Save evandrocoan/31bf27bdbdeab05237e82476b09cc0d2 to your computer and use it in GitHub Desktop.
Change vmware ip address remotely with ssh with auto recover in case things go wrong
  1. Run df -h to see if you have the volume /vmfs/volumes/datastore1 or datastore1

  2. Create the file /vmfs/volumes/datastore1/backup.sh to test it calling /vmfs/volumes/datastore1/backup.sh to know this script is compatible with your sh version

  3. Before testing, check if your /etc/rc.local shell bang is #! /bin/sh, and update it here if not

  4. Check if your WMware is running on UTC or in your local time zone with date command

  5. Create the backup file with cp -v /etc/vmware/esx.conf /vmfs/volumes/datastore1/esx.conf

    • Put files backup files on /vmfs/volumes/xxx because else where vmware will delete them after reboot
    • Run chmod +x /vmfs/volumes/datastore1/backup.sh
    • Edit /etc/rc.local.d/local.sh and add the line /vmfs/volumes/datastore1/backup.sh
    • Edit /vmfs/volumes/datastore1 to your correct data volume
    #! /bin/sh
    set -x;
    
    RUN_UNTIL_DATE='2023-12-25 08:00:00'  # yyyy-mm-dd HH:MM:SS
    
    if [[ "$(date +%s)" -gt "$(date -d "$RUN_UNTIL_DATE" +%s)" ]];
    then
        cp -v /vmfs/volumes/datastore1/esx.conf /etc/vmware/esx.conf;
        printf 'Restoring backup because date now "%s" is greater than "%s".\n' "$(date +%s)" "$RUN_UNTIL_DATE";
    else
        printf 'Keeping backup original, because date now "%s" is less than "%s".\n' "$(date +%s)" "$RUN_UNTIL_DATE";
    fi;
  6. After backing up the file, edit the all ip address related settings. Please, check the full file because your vmware version may repeat the same value in more places.

    [root@localhost:~] diff /vmfs/volumes/datastore1/esx.conf /etc/vmware/esx.conf
    --- /etc/vmware/esx.conf
    +++ /etc/vmware/esx.conf.old
    @@ -161,15 +161,15 @@
     /firewall/services/sshServer/enabled = "true"
     /firewall/services/HBR/allowedall = "true"
     /firewall/services/HBR/enabled = "true"
    -/adv/Net/ManagementAddr = "10.3.22.1"
    +/adv/Net/ManagementAddr = "172.40.22.1"
     /adv/Net/ManagementIface = "vmk0"
     /adv/UserMem/UserMemASRandomSeed = "238754299"
     /adv/Misc/HostName = "localhost.khomp.corp"
     /adv/Misc/DiskDumpSlotSize = "2560"
    -/adv/Misc/HostIPAddr = "10.3.22.1"
    +/adv/Misc/HostIPAddr = "172.40.22.1"
     /adv/UserVars/HostClientCEIPOptIn = "1"
     /net/routes/kernel/child[0000]/netstackInstance = "defaultTcpipStack"
    -/net/routes/kernel/child[0000]/gateway = "10.254.254.254"
    +/net/routes/kernel/child[0000]/gateway = "172.40.255.254"
     /net/routes/kernel/child[0000]/source = "MANUAL"
     /net/pnic/child[0001]/mac = "00:90:fb:5b:ed:09"
     /net/pnic/child[0001]/virtualMac = "00:50:56:55:ed:3d"
    @@ -223,8 +223,8 @@
     /net/vswitch/child[0000]/name = "vSwitch0"
     /net/vswitch/child[0000]/cdp/status = "listen"
     /net/vmkernelnic/child[0000]/netstackInstance = "defaultTcpipStack"
    -/net/vmkernelnic/child[0000]/ipv4netmask = "255.0.0.0"
    -/net/vmkernelnic/child[0000]/ipv4address = "10.3.22.1"
    +/net/vmkernelnic/child[0000]/ipv4netmask = "255.255.0.0"
    +/net/vmkernelnic/child[0000]/ipv4address = "172.40.22.1"
     /net/vmkernelnic/child[0000]/dhcpv6 = "false"
     /net/vmkernelnic/child[0000]/tags/1 = "true"
     /net/vmkernelnic/child[0000]/portgroup = "Management Network"
    @@ -232,8 +232,8 @@
     /net/vmkernelnic/child[0000]/mac = "00:90:fb:5b:ed:08"
     /net/vmkernelnic/child[0000]/routAdv = "true"
     /net/vmkernelnic/child[0000]/ipv6 = "true"
    -/net/vmkernelnic/child[0000]/ipv4gateway = "10.254.254.254"
    -/net/vmkernelnic/child[0000]/ipv4broadcast = "10.255.255.255"
    +/net/vmkernelnic/child[0000]/ipv4gateway = "172.40.255.254"
    +/net/vmkernelnic/child[0000]/ipv4broadcast = "172.40.255.255"
     /net/vmkernelnic/child[0000]/dhcpDns = "false"
     /net/vmkernelnic/child[0000]/macFromPnic = "vmnic0"
     /net/vmkernelnic/child[0000]/tsoMss = "0"
  7. After editing the setting file /etc/vmware/esx.conf, just restart your vmware with reboot command and it will take the new ip on the next boot.

  8. After connecting to machine, please remove the file /vmfs/volumes/datastore1/backup.sh, otherwise, it will revert you ip in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment