Kamailio SIP Registrar Setup with usrloc
Set up a Kamailio SIP registrar with usrloc: save() and lookup() codes, db_mode options, auth_db, RPC commands, and the default kamailio.cfg values.
By Arkay · Updated · Published

What do the registrar and usrloc modules do?

The registrar module contains the REGISTER processing logic. The registrar module documentation states that the actual location database is managed by the USRLOC module.
The usrloc module documentation says usrloc keeps the user location table for other modules. Your routing script calls registrar functions. Those functions add, remove or modify the usrloc records behind them.
Which registrar parameters control expiry times and contacts per AOR?
Registrar parameters are set with modparam lines in the configuration file. The block below uses the example values from the registrar documentation. They show the syntax. They are not recommendations for your network.
modparam("registrar", "default_expires", 1800)
modparam("registrar", "max_expires", 120)
modparam("registrar", "max_contacts", 10)
default_expires sets the expiry for new records when the REGISTER has no Expires header and no expires contact parameter. Its module default is 3600 seconds, which is one hour. The example lowers it to 1800 seconds, or half an hour.
min_expires is the minimum value accepted for a Contact expires setting. Its module default is 60 seconds. max_expires works in the other direction. Any Contact value higher than the maximum is set to the maximum. Its module default is 0, and the example sets it to 120 seconds.
max_contacts limits the number of contacts per AOR (Address of Record) in the user location database. Its module default is 0. The example sets it to 10 contacts per AOR.
append_branches defaults to 1 and controls how lookup handles multiple contacts. method_filtering defaults to 0 and filters contacts by their supported methods during lookup. gruu_enabled defaults to 1.
use_path defaults to 0 and enables Path header handling. path_mode defaults to 2. It has three modes, and each one treats the Path header differently.
- Mode 0 saves the Path header into usrloc but leaves it out of the reply.
- Mode 1 includes it in the reply only if path support is indicated.
- Mode 2 saves it only if path support is indicated.
In the default kamailio.cfg, use_path is 1 and path_mode is 0. Kamailio therefore saves the Path header into usrloc but leaves it out of the reply. The default config also sets method_filtering to 1, so lookup filters contacts by their supported methods.
Our compiled comparison lists RFC 3327 for mode 2, and mode 2 replies 420 Bad Extension if the client has no path support.
How do the registrar defaults compare with the default kamailio.cfg?
We compiled this comparison on November 14, 2026, from the Kamailio 6.0.x registrar, usrloc and auth_db documentation. We also compiled it from the default kamailio.cfg in the Kamailio GitHub repository, master branch, which is labelled v6.2. Nothing was tested. Every row comes from the pages listed.
| Parameter | Module default | Default kamailio.cfg |
|---|---|---|
| default_expires | 3600 | not set |
| min_expires | 60 | not set |
| max_expires | 0 | 3600 |
| max_contacts | 0 | commented out (example 10) |
| method_filtering | 0 | 1 |
| append_branches | 1 | commented out (example 0) |
| gruu_enabled | 1 | 0 |
| use_path | 0 | 1 |
| path_mode | 2 | 0 (save Path, not in reply) |
| received_avp | NULL | $avp(RECEIVED) under WITH_NAT |
The default config changes five registrar values from their module defaults. It sets max_expires to 3600 and gruu_enabled to 0. It sets method_filtering and use_path to 1, and path_mode to 0.
The default config does not set default_expires or min_expires, so the module defaults apply. It comments out max_contacts and append_branches. Check each value against your own service before you copy the file.
How do you handle REGISTER in the routing script?

In the default config, one route block handles REGISTER. The block returns for any request that is not a REGISTER. For a REGISTER, it calls save("location"). If that call fails, it sends an error reply and exits.
This is the opening line of the if-statement that checks save("location") in the default config:
if (!save("location")) {
The first argument to save() is the domain, which the default config sets to "location". Save() processes a REGISTER and adds, removes or modifies location records. It reads the Contact and Expires header fields to decide which change to make.
What do the save() flags and return codes mean?
The full call is save(domain[, flags[, uri]]). The optional flags change how save() works.
| Flag | Meaning |
|---|---|
| 0x01 | Memory cache only |
| 0x02 | No SIP reply |
| 0x04 | One contact per AoR |
| 0x08 | Skip expires ranges |
| 0x10 | Prepare reply headers (only with 0x02) |
Save() returns a code that tells the script what happened.
| Return code | Meaning |
|---|---|
| 1 | Inserted |
| 2 | Updated |
| 3 | Deleted |
| 4 | Returned |
| -1 | Error |
| -2 | Too many contacts |
The 0x10 flag only has an effect together with 0x02. Use the return code in your script when a failed REGISTER needs a different reply from a successful one.
How do calls find registered users with lookup()?
lookup(domain[, uri]) finds all contacts for the Request-URI user in usrloc. It returns 1 when contacts are found. It returns -1 when no contact exists, and -2 when contacts are found but the method is not supported. It returns -3 on an internal error.
The default config answers -1 and -3 with 404 Not Found. It answers -2 with 405 Method Not Allowed. Those replies are the default config's choice, so your script can use different ones.
registered(domain [, uri ...]) returns true if the AOR is registered, and false otherwise. lookup_branches(domain) runs lookup on the Request-URI and on additional branches. It passes on the codes that lookup returns.
How do you keep registrations after a restart?
The usrloc db_mode parameter decides where location records are kept. Its module default is 0.
| db_mode | Behavior |
|---|---|
| 0 | Memory only. This is the module default. |
| 1 | Write-through. Every change goes to the database at once. |
| 2 | Write-back. The default kamailio.cfg uses this under WITH_USRLOCDB. |
| 3 | Database only, with no memory cache. |
| 4 | Loaded from the database at startup, memory only at runtime, never written back. |
The default config makes location records persistent only when MySQL is enabled and WITH_USRLOCDB is defined. It then sets db_url to DBURL and uses this line:
modparam("usrloc", "db_mode", 2)
The module default for db_url points to a local MySQL database named kamailio. Set db_url to your own database. The timer_interval parameter sets the number of seconds between two timer runs. Its default is 60 seconds, which is one minute.
The handle_lost_tcp parameter has a default of 0. When it is 1, usrloc removes TCP, TLS, WS and WSS records when the connection is lost. This setting does not apply in DB-only mode.
How do you require authentication for a REGISTER?
Load the auth_db module to check credentials against a database. The function www_authenticate(realm, table [, method]) verifies credentials per RFC 2617. The function www_authorize(realm, table) is the same function under its older name. The name is kept for backward compatibility.
The function auth_check(realm, table, flags) combines www_authenticate and proxy_authenticate. Flag 1 checks that the authentication username matches the To or From username. The documentation gives this example:
if (!auth_check("$fd", "subscriber", "1")) {
auth_challenge("$fd", "1");
}
Under WITH_AUTH, the default config has a route[AUTH] block. It challenges REGISTER and local requests with auth_check and auth_challenge, using "0" as the second argument of auth_challenge. It replies 403 Not relaying when neither From nor Request-URI is local.
The auth_db module defaults are password_column set to ha1 and calculate_ha1 set to 0. Under WITH_AUTH, the default config sets calculate_ha1 to yes and password_column to "password". Check which column your database uses before you copy this setting.
How do you see and delete registered contacts?
Usrloc provides RPC commands for inspecting and changing the location table at runtime.
| Command | What it does |
|---|---|
ul.dump | Dumps the whole location table. |
ul.lookup table AOR | Shows one AOR. |
ul.rm table AOR | Deletes an entire AOR record, including all of its contacts. |
ul.rm_contact table AOR contact | Deletes one contact. |
ul.flush | Writes the memory cache to the database. |
ul.add | Adds a contact. |
Use ul.rm_contact when one device should be removed. Use ul.rm when the whole AOR must be cleared. Run ul.dump first when you want to see the current state before you change anything.
More guides in Open-Source PBX and SIP Servers.
Frequently asked questions
- Which modules must be loaded for registration to work?
- The registrar module needs the usrloc (User Location) and sl (Stateless Replies) modules loaded before it. Usrloc keeps the location table, and registrar processes the REGISTER requests.
- How do registrations behave behind NAT (received_avp, nat_bflag)?
- The received_avp parameter names an AVP whose value registrar stores in the received column of the location database. Under WITH_NAT, the default kamailio.cfg sets received_avp to $avp(RECEIVED) and the usrloc nat_bflag to FLB_NATB.
Sources
- The registrar module contains the REGISTER processing logic; the location database itself (www.kamailio.org)
- usrloc keeps the user location table for other modules and exports no functions for the ro (www.kamailio.org)
- auth_db's www_authenticate(realm, table [, method]) verifies credentials per RFC 2617; www (www.kamailio.org)
- The default kamailio.cfg (master, labelled v6.2) sets registrar method_filtering 1, max_ex (github.com)