connect /as sysdba is failing with error ORA-12545

The Problem

When trying to connect to a database as sysdba, ORA-12545 is occured and connection is failed. For Example:

SQL*Plus: Release 11.2.0.4.0 Production on Mon Nov 25 10:03:02 2019

Copyright (c) 1982, 2013, Oracle. All rights reserved.

ERROR:
ORA-12545: Connect failed because target host or object does not exist

Enter user-name:

In general, ORA-12545 is caused when trying to connect to a database via a TNS listener. Since connecting “/as sysdba” is a local connection without using a TNS listener, ORA-12545 should not be caused. A similar issue can be caused when running DBCA.

The Solution

An environment variable TWO_TASK (for Windows platform, an environment variable is LOCAL) is configured and non-exsisting hostname is described in HOSTNAME parameter of TNS string.

(tnsnames.ora)
[TNS ENTRY] =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = <non-existing hostname="">)(PORT = <port>))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = <service_name>)
    )
)</service_name></port></non-existing>

For example:

$ export TWO_TASK=[TNS ENTRY]
$ sqlplus "/as sysdba"
SQL*Plus: Release 11.2.0.4.0 Production on Mon Nov 25 10:03:02 2019
Copyright (c) 1982, 2013, Oracle. All rights reserved.
ERROR:
ORA-12545: Connect failed because target host or object does not exist
Enter user-name:

Unsetting an environment variable TWO_TASK would resolve the issue mentioned above. The command to unset the TWO_TASK may be different according to the operating system or SHELL used. Here is an example to unset the TWO_TASK variable in bash shell in Linux operating system:

$ unset TWO_TASK