ORA-12162: TNS: net service name is incorrectly specified
ORA-12162 is a Oracle Net Services (TNS) error that occurs when the client cannot determine the net service name to use for a database connection. It most commonly appears when the ORACLE_SID environment variable is empty, missing, or incorrectly set, leaving TNS with no valid service name to resolve. This error is especially common in Linux/Unix environments and automated scripts where environment variables are not properly inherited.
Top 3 Causes and Fixes
Cause 1: ORACLE_SID or TWO_TASK Environment Variable Not Set
The most frequent cause is an unset or empty ORACLE_SID variable. When you run sqlplus / as sysdba without setting it, Oracle has no idea which instance to connect to.
-- Check current instance name from inside the DB
SELECT INSTANCE_NAME, STATUS
FROM V$INSTANCE;
-- Check registered services
SELECT NAME, NETWORK_NAME
FROM V$SERVICES
ORDER BY NAME;
Fix: Set the environment variable before connecting.
-- In bash shell before launching sqlplus:
-- export ORACLE_SID=ORCL
-- export ORACLE_HOME=/u01/app/oracle/product/19c/dbhome_1
-- export PATH=$ORACLE_HOME/bin:$PATH
-- Correct connection syntax
-- sqlplus scott/tiger@ORCL
-- sqlplus scott/tiger@//hostname:1521/ORCL
Cause 2: Typo or Syntax Error in tnsnames.ora
A malformed tnsnames.ora entry — mismatched parentheses, extra spaces in the alias name, or a missing SERVICE_NAME — will cause ORA-12162 even if the alias exists.
-- Correct tnsnames.ora entry format:
-- ORCL =
-- (DESCRIPTION =
-- (ADDRESS = (PROTOCOL = TCP)(HOST = dbserver01)(PORT = 1521))
-- (CONNECT_DATA =
-- (SERVER = DEDICATED)
-- (SERVICE_NAME = ORCL)
-- )
-- )
-- Find tnsnames.ora location from the DB
SELECT VALUE
FROM V$PARAMETER
WHERE NAME = 'tns_admin';
Fix: Validate the entry using tnsping after every edit.
-- Run from OS shell:
-- $ tnsping ORCL
-- Expected output: OK (xx msec)
-- Also verify listener-registered services:
-- $ lsnrctl services
Cause 3: Incorrect NAMES.DIRECTORY_PATH in sqlnet.ora
If sqlnet.ora has a missing or wrong NAMES.DIRECTORY_PATH, the TNS resolver cannot find any naming method and throws ORA-12162.
-- Correct sqlnet.ora setting:
-- NAMES.DIRECTORY_PATH = (TNSNAMES, EZCONNECT)
-- Bypass tnsnames.ora entirely with Easy Connect for quick testing:
-- sqlplus scott/tiger@//192.168.1.100:1521/ORCL
-- Check relevant DB parameters
SELECT NAME, VALUE
FROM V$PARAMETER
WHERE NAME IN ('service_names', 'instance_name',
'db_name', 'db_unique_name');
Fix: Set TNS_ADMIN explicitly to point to the correct config directory.
-- export TNS_ADMIN=/u01/app/oracle/network/admin
-- Then verify with:
-- $ tnsping ORCL
Quick Fix Checklist
-- Step 1: Confirm ORACLE_SID is set (OS shell)
-- echo $ORACLE_SID
-- Step 2: Test Easy Connect (bypasses tnsnames.ora)
-- sqlplus user/pass@//host:1521/service_name
-- Step 3: Validate TNS alias
-- tnsping <alias_name>
-- Step 4: Check listener status
-- lsnrctl status
-- Step 5: Verify services registered with the listener
SELECT NAME, NETWORK_NAME, PDB
FROM V$SERVICES;
Prevention Tips
Standardize environment profiles: Define all Oracle environment variables (
ORACLE_SID,ORACLE_HOME,TNS_ADMIN) in a centralized profile script managed under version control. Use configuration management tools like Ansible to deploy these profiles consistently across all servers.Validate tnsnames.ora after every change: Always run
tnsping <alias>immediately after modifyingtnsnames.ora. Keep a dated backup (tnsnames.ora.YYYYMMDD) before each change, and enforce a two-DBA peer-review policy for production edits.
Related Errors
| Error Code | Description |
|---|---|
| ORA-12154 | TNS could not resolve the connect identifier — most similar to ORA-12162 |
| ORA-12541 | No listener running at the specified host/port |
| ORA-12514 | Listener does not know the requested service |
| ORA-12170 | Connect timeout occurred |
📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.
Top comments (0)