How to Get SQL Server’s IP Address

When managing or troubleshooting SQL Server, you might need to get the IP address your SQL Server instance is listening on. While servers can listen on multiple IP addresses, SQL Server offers a straightforward way to determine which address is used for your current connection.

In this post I’ll share a few quick ways to obtain the server’s IP address directly from SQL Server.

1. Query to Get the SQL Server IP Address

We can query the sys.dm_exec_connections DMV to get IP address info for the SQL Server instance. You’ll need the VIEW SERVER STATE permission to run this:

-- Get Running SQL Server IP
SELECT local_net_address  
FROM sys.dm_exec_connections
WHERE session_id = @@SPID; -- filter to current session

This approach avoids using xp_cmdshell, a method that should be disabled or tightly controlled on production servers. While granting VIEW SERVER STATE permissions may be necessary, it’s a lower security risk compared to enabling xp_cmdshell.

2. Get SQL Server IP Using CONNECTIONPROPERTY()

Run the following query to get the IP address of the server for the current connection:

-- Get IP for SQL Server
SELECT CONNECTIONPROPERTY('local_net_address') AS 'Server IP Address'; 

-- Get SQL hostname instead
SELECT SERVERPROPERTY(N'MachineName'); 

This command will return the IP address if the connection uses the TCP transport protocol. Keep in mind that this method may not work in environments like Azure SQL Database or for connections that use other transport types, as it will return NULL in those cases.

For more details, check out MS Docs on CONNECTIONPROPERTY.

3. Checking Listening IPs & Ports for All Services

I have another post that will show you how to check all listening IPs and Ports on a Windows Computer. It’s a guide using Resource Monitor and TcpView, which might be useful for checking which service is tied to a specific port.

Hope all this helps!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *