Changing the Hostname and IP Address on a CentOS server and re-configuring Oracle and ORDS

I use VirtualBox quite a lot. Rather than going through the rigmarole of installing the software I need every time I want a new environment, I simply clone the VM I’ve already installed everything on.
One drawback with this approach is that, because I’ve already configured the network settings on this baseline VM, I can’t run two clones concurrently as they both have the same hostname and IP address.

What I’ll be covering here is :

The approach I’ve taken is to execute each step on the command line without the need for any interactive input. Therefore, it’s possible to take the steps described here as building blocks for a bash script (or scripts) to accomplish these tasks.
The exception is where I edit the contents of files. If you wanted to automate this, you can use something like…

sed -i s/192.168.56.220/192.168.56.225/g file_to_edit

…for the IP address and…

sed -i s/frea./rincewind./g file_to_edit

…for the hostname where file_to_edit is the file you want to change.

If you’ve found your way here in search of simply changing the hostname and/or the IP address on a CentOS7 server, then you can just skip all the database related stuff and start right here.

By the way, I’ve decided upon a new naming convention for my servers which makes use of Discworld characters. There may be the odd reference to this in what follows…

Environment Details

As far as the software is concerned, I’m running :

It’s also worth noting that, whilst all of the steps that follow can be executed in an ssh terminal, it’s probably a good idea to have access the the server’s console, just in case one of the networking changes don’t quite work as expected.

Disabling the Oracle and ORDS services

We can see that these services are currently up and running :

systemctl status oracle-xe-18c.service

systemctl status ords

The database is accessible via TNS :

sqlplus system@xe
select sys_context('userenv', 'server_host') 
from dual;


The ORDS page is also available :

curl -kISs https://frea.virtualbox:8443/ords/apex_admin

Now to stop everything. Whilst we could simply stop the service, I’ve taken the approach of stopping the database and then the listener so that we can check everything has been shutdown as expected. Database first…

sudo su oracle

sqlplus / as sysdba
shutdown immediate

…which should result in :

Database closed.
Database dismounted.
ORACLE instance shut down.

And now the TNSListener :

lsnrctl stop

…which should come back with…

LSNRCTL for Linux: Version 18.0.0.0.0 - Production on 08-AUG-2019 18:04:12

Copyright (c) 1991, 2018, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=frea.virtualbox)(PORT=1522)))
The command completed successfully

At this point, we should now be able to stop and disable the services without any issues. First ORDS…

sudo systemctl stop ords
sudo systemctl disable ords

Removed symlink /etc/systemd/system/multi-user.target.wants/ords.service.

…and then the Oracle database…

sudo systemctl stop oracle-xe-18c.service
sudo systemctl disable oracle-xe-18c.service

oracle-xe-18c.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig oracle-xe-18c off

At this point, if we reboot the server…

sudo reboot now

…these services will no longer start on server startup…

Changing the Server Name

In line with my new naming convention, I’m going to call this server “rincewind” in the hope that it will always run fast…

sudo nano /etc/hostname

Now I simply edit the file to replace the old hostname with the new one :

We need to check that the old server name isn’t stored anywhere in the network config files :

grep frea.virtualbox /etc/hosts
grep frea.virtualbox /etc/sysconfig/network
sudo grep frea.virtualbox /etc/sysconfig/network-scripts/*

In my case, this confirms that there are no references to the old host name :

For the change to take effect, we’ll need to re-start the server.

NOTE – I’ve seen some articles which suggest that you can change the hostname by simply running…

hostname new_host_name

…where new_host_name is the hostname you want to change to.

After trying this a couple of times and wondering why the hostname reverted to it’s original value after reboot, I was reminded of The Librarian’s sage advise to “Ook” which, translated from Orangutan means, “RTFM”…

man hostname
...
SET NAME
       When  called  with one argument or with the --file option, the commands
       set the host name  or  the  NIS/YP  domain  name.   hostname  uses  the
       sethostname(2)  function,  while all of the three domainname, ypdomain‐
       name and nisdomainname use setdomainname(2).  Note, that this is effec‐
       tive  only  until  the  next  reboot.  Edit /etc/hostname for permanent
       change.
...

Anyhow, once the server is restarted…

sudo reboot now

…we can confirm that the new hostname is now permanent :

Changing the Server’s IP Address

For this server, the static IP address is specified in the Network config for the active network card.
The current IP address is 192.168.56.220.
We want to change this to 192.168.56.225
First, we need to find out where the existing IP address is defined…

sudo grep  192.168.56.220 /etc/sysconfig/network-scripts/*

In my case, this returns :

/etc/sysconfig/network-scripts/ifcfg-en0s8:IPADDR=192.168.56.220

…which presently contains :

I’m just going to edit the file to change the IP address…

sudo nano /etc/sysconfig/network-scripts/ifcfg-en0s8

…so that it now looks like this :

TYPE=ETHERNET
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
DEVICE=enp0s8
ONBOOT=yes
IPADDR=192.168.56.225
PREFIX=24
GATEWAY=192.168.56.254
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no

ZONE=public

Once I restart the server…

sudo reboot now

…I should be able to ping the new IP address from a remote machine on the same network – once it’s rebooted obviously…

To verify that this is indeed the correct host, we can now ssh into the server and check the hostname

ssh mike@192.168.56.225
The authenticity of host '192.168.56.225 (192.168.56.225)' can't be established.
ECDSA key fingerprint is SHA256:yLr5qppfy07E8SEUPnKwDTVxny20sGE7S8oSP4xK44Q.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.225' (ECDSA) to the list of known hosts.
mike@192.168.56.225's password: 
Last login: Fri Aug  9 10:35:09 2019 from 192.168.56.200
[mike@rincewind ~]$ hostname
rincewind.virtualbox
[mike@rincewind ~]$ 

If all you want to accomplish is to rename the server and change it’s IP address then at this point, you’re pretty much done.
Of course you may have some housekeeping to do – in my case, I need to edit my local /etc/hosts

sudo nano /etc/hosts

…to add an entry for the new IP address :

192.168.56.225 rincewind.virtualbox

You’re work may now be completed in the Dungeon Dimensions of networking stuff. For me however, there’s still a bit more to sort out.

Re-configure the Database Listener to work with the new server details

In Oracle terms, it’s most likely that the changes we’ve made will impact on the TNS Listener – the daemon that services database connection requests.

First of all, we need to switch to the oracle user and go to the directory containing the TNS configuration files :

sudo su oracle
cd $ORACLE_HOME/network/admin

Now we need to see which files contain references to the old hostname…

grep -l frea.virtualbox *.ora
listener.ora
tnsnames.ora

… or IP address…

grep -l 192.168.56.225 *.ora

In my case, I can see that the hostname is referenced in tnsnames.ora and listener.ora.

Before we edit these files, let’s take a look at the current status of the listener :

lsnrctl status

LSNRCTL for Linux: Version 18.0.0.0.0 - Production on 09-AUG-2019 13:56:10

Copyright (c) 1991, 2018, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=frea.virtualbox)(PORT=1522)))
TNS-12545: Connect failed because target host or object does not exist
 TNS-12560: TNS:protocol adapter error
  TNS-00515: Connect failed because target host or object does not exist
   Linux Error: 2: No such file or directory
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1522)))
TNS-12541: TNS:no listener
 TNS-12560: TNS:protocol adapter error
  TNS-00511: No listener
   Linux Error: 2: No such file or directory

As expected, it’s not up. That’s not what’s interesting though.
Let’s just edit it’s config file…

nano listener.ora

…so that the contents of the file now look like this :

# listener.ora Network Configuration File: /opt/oracle/product/18c/dbhomeXE/network/admin/listener.ora
# Generated by Oracle configuration tools.
# Mike 9/8/19 - Modified to reflect new host name
DEFAULT_SERVICE_LISTENER = XE

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = rincewind.virtualbox)(PORT = 1522))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1522))
    )
  )

If we check the status of the listener again, it’s still down but it has picked up the new hostname :

lsnrctl status

LSNRCTL for Linux: Version 18.0.0.0.0 - Production on 09-AUG-2019 14:51:55

Copyright (c) 1991, 2018, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=rincewind.virtualbox)(PORT=1522)))
TNS-12541: TNS:no listener
 TNS-12560: TNS:protocol adapter error
  TNS-00511: No listener
   Linux Error: 111: Connection refused
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1522)))
TNS-12541: TNS:no listener
 TNS-12560: TNS:protocol adapter error
  TNS-00511: No listener
   Linux Error: 2: No such file or directory

Next, we need to edit the tnsnames.ora so that any TNS connection request on the host will be routed correctly by the listener…

nano tnsnames.ora

Once edited, my file now contains :

# tnsnames.ora Network Configuration File: /opt/oracle/product/18c/dbhomeXE/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
# Mike 9/8/19 - modified with new hostname
XE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rincewind.virtualbox)(PORT = 1522))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = XE)
    )
  )

LISTENER_XE =
  (ADDRESS = (PROTOCOL = TCP)(HOST = rincewind.virtualbox)(PORT = 1522))


Now we need to enable and start the oracle service. To do this, we should be root ( which means closing the oracle user session as the oracle user won’t normally have sudo privileges) :

sudo -s
systemctl enable oracle-xe-18c.service
systemctl start oracle-xe-18c.service
systemctl status oracle-xe-18c.service

If all goes well, it should look something like this…

…and we can stop being root.

Connecting as oracle again, we can see that the listener is now up and running :

As a result, we can now connect to the database using TNS :

To connect to the database remote machine, the remote tnsnames.ora entries will need to change to reflect the new hostname.

For example, this is now the entry for the database that’s in my client tnsnames.ora :

WIZZARD =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rincewind.virtualbox)(PORT = 1522))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = XE)
    )
  )

I can therefore connect to the database from my client using this :

sqlplus system@wizzard

You may pause for a moment here and wonder whether Rincewind’s lack of magical ability is connected to his bad spelling 🙂

Re-configuring ORDS and generating new SSL certificates

Generating new SSL Certificates

When installed, ORDS was configured to use self-signed SSL certificates. The first thing we’ll need to do is to generate new certificates for the new hostname. So…

sudo -s

mkdir -p /etc/ssl/private/rincewind.virtualbox

chmod -R 700 /etc/ssl/private/rincewind.virtualbox

cd /etc/ssl/private/rincewind.virtualbox

Now we’re ready to generate the new certificates. I’ll use the same information as I did for the original configuration :

openssl req -newkey rsa:2048 -nodes -keyout rincewind.virtualbox.key -x509 -days 3650 -out rincewind.virtualbox.crt -subj "/C=UK/ST=England/L=./O=The Anti-Kyte/CN=rincewind.virtualbox"

…which results in…

Generating a 2048 bit RSA private key
...+++
.............+++
writing new private key to 'rincewind.virtualbox.key'
-----

We can now stop being root :

exit

Pointing ORDS at the new certificates

Now we’ve generated the new certificate, we need to tell ORDS to use it so switch to the oracle user and navigate to the ORDS files…

sudo su oracle
cd $ORACLE_BASE/ords/ords/standalone

…then remove the existing certificate files…

rm self-signed.*

These files should now be re-generated the next time ORDS starts.

…and edit the standalone.properties file to point to the new certificates :

#Fri Feb 22 11:48:35 GMT 2019
#Mike 11/8/19 - amended	SSL settings for server	rename
jetty.secure.port=8443
ssl.cert=/etc/ssl/private/rincewind.virtualbox/rincewind.virtualbox.crt
ssl.cert.key=/etc/ssl/private/rincewind.virtualbox/rincewind.virtualbox.key
ssl.host=rincewind.virtualbox
standalone.context.path=/ords
standalone.doc.root=/opt/oracle/ords/ords/st	andalone/doc_root
standalone.scheme.do.not.prompt=true
standalone.static.context.path=/i
standalone.static.path=/opt/oracle/ords/apex/images

Next we need to go up one directory in the tree…

cd $ORACLE_BASE/ords/ords

…and edit the defaults.xml file so that it now references the new server name :

<entry key="db.hostname">rincewind.virtualbox</entry>

We can now stop being oracle…

exit

…and re-enable the ORDS service…

Re-starting ORDS

The process for re-enabling the ORDS service is pretty much the same as that for the database :

sudo -s
systemctl enable ords
Created symlink from /etc/systemd/system/multi-user.target.wants/ords.service to /etc/systemd/system/ords.service.

Now re-start ords…

systemctl start ords

We can confirm that ORDS is now up and running…

And that’s magic !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.