---
name: "nbd_get_uri"
section: "3"
platform: "linux"
package: "libnbd"
version: "1.24.4-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/nbd_get_uri"
---

# nbd\_get\_uri(3)

construct an NBD URI for a connection

## NAME

nbd\_get\_uri - construct an NBD URI for a connection

## SYNOPSIS

```
 #include <libnbd.h>
 char * nbd_get_uri (
          struct nbd_handle *h
        );
```

## DESCRIPTION

This makes a best effort attempt to construct an NBD URI which could be used to connect back to the same server (using [**nbd\_connect\_uri**(3)](/nbd_connect_uri/3.md)).

In some cases there is not enough information in the handle to successfully create a URI (eg. if you connected with [**nbd\_connect\_socket**(3)](/nbd_connect_socket/3.md)).  In such cases the call returns `"NULL"` and further diagnostic information is available via [**nbd\_get\_errno**(3)](/nbd_get_errno/3.md) and [**nbd\_get\_error**(3)](/nbd_get_error/3.md) as usual.

Even if a URI is returned it is not guaranteed to work, and it may not be optimal.

[**nbdinfo**(1)](/nbdinfo/1.md) *--uri* option is a way to access this API from shell scripts.

## RETURN VALUE

This call returns a string.  The caller must free the returned string to avoid a memory leak.

## ERRORS

On error `"NULL"` is returned.

Refer to "ERROR HANDLING" in [**libnbd**(3)](/libnbd/3.md) for how to get further details of the error.

The following parameters must not be NULL: `"h"`. For more information see "Non-NULL parameters" in [**libnbd**(3)](/libnbd/3.md).

## HANDLE STATE

nbd\_get\_uri can be called when the handle is in the following states:

```
 ┌─────────────────────────────────────┬─────────────────────────┐
 │ Handle created, before connecting   │ ❌ error                │
 │ Connecting                          │ ✅ allowed              │
 │ Connecting & handshaking (opt_mode) │ ✅ allowed              │
 │ Connected to the server             │ ✅ allowed              │
 │ Connection shut down                │ ✅ allowed              │
 │ Handle dead                         │ ✅ allowed              │
 └─────────────────────────────────────┴─────────────────────────┘
```

## VERSION

This function first appeared in libnbd 1.8.

If you need to test if this function is available at compile time check if the following macro is defined:

```
 #define LIBNBD_HAVE_NBD_GET_URI 1
```

## SEE ALSO

[**nbd\_aio\_connect\_uri**(3)](/nbd_aio_connect_uri/3.md), [**nbd\_connect\_socket**(3)](/nbd_connect_socket/3.md), [**nbd\_connect\_uri**(3)](/nbd_connect_uri/3.md), [**nbd\_create**(3)](/nbd_create/3.md), [**nbd\_get\_errno**(3)](/nbd_get_errno/3.md), [**nbd\_get\_error**(3)](/nbd_get_error/3.md), [**nbd\_is\_uri**(3)](/nbd_is_uri/3.md), [**nbd\_supports\_uri**(3)](/nbd_supports_uri/3.md), [**libnbd**(3)](/libnbd/3.md), [**nbdinfo**(1)](/nbdinfo/1.md).

## AUTHORS

Eric Blake

Richard W.M. Jones

## COPYRIGHT

Copyright Red Hat

## LICENSE

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
