Practical guides to business calling, messaging and AI voice.

Asterisk PBXFEATUREDLatestVoIP and SIP

SIP 401 Unauthorized and 407: Causes and Fixes

A SIP 401 Unauthorized response is a question, not a refusal. The server is asking the client to prove who it is, and the request will succeed once the client answers with valid credentials. RFC 3261 defines it in section 21.4.2 in one line: “The request requires user authentication.” Its twin, 407 Proxy Authentication Required (section 21.4.8), means the same thing, except that a proxy is asking rather than the final destination.

That is why the first 401 or 407 in a trace is almost never the problem. In fact, nearly every successful registration and most outbound trunk calls begin with one. The problem is the second challenge: the client sent credentials and the server asked again. This guide shows how to tell the normal challenge from the failing one, what causes the failing one on Asterisk, FreePBX, FreeSWITCH, SIP trunks and browser phones, and how to fix each cause. For what every other code means, see the SIP response codes lookup; for the code that follows a rejected login at many providers, see SIP 403 Forbidden.

SIP 401 Unauthorized or 407: who is asking

RFC 3261 (section 22.1) splits the job by role. User agents, registrars and redirect servers challenge with 401. Proxies must not use 401; they challenge with 407. Each code comes with its own pair of headers:

401 Unauthorized 407 Proxy Authentication Required
Sent by The destination: a registrar, a PBX, the called user agent A proxy on the path: a carrier’s edge proxy, a gateway
Challenge header WWW-Authenticate Proxy-Authenticate
Client answers with Authorization Proxy-Authorization
Typically seen on REGISTER, and INVITEs to Asterisk INVITEs to SIP trunk providers and to FreeSWITCH

In practice, however, you read them the same way. Asterisk’s PJSIP stack always challenges with 401, for calls as well as registrations: its request authenticator (pjsip_distributor.c) builds every challenge as a 401. FreeSWITCH challenges registrations with 401 and calls with 407, as its users’ manual shows. Many trunk providers answer an unauthenticated INVITE with 407 for the same reason: the challenge comes from their edge proxy, not from the far end of the call.

What a normal challenge looks like

A healthy registration is four messages:

REGISTER (no credentials)        --->
                                 <---  401 Unauthorized
                                       WWW-Authenticate: Digest realm="pbx.example.com",
                                         nonce="...", algorithm=MD5, qop="auth"
REGISTER (Authorization: Digest
  username="1001", realm="pbx.example.com", nonce="...", response="...")  --->
                                 <---  200 OK

First, the client hashes its username, the realm and its password together with the server’s nonce, and sends the result. Then the server does the same calculation with the password it holds. If the two results match, the request goes through. As a result, the password itself never crosses the wire. Two details matter when you read a trace:

  • The CSeq goes up. RFC 3261 (section 22.2) requires the resent request to carry a higher CSeq. Same Call-ID, CSeq one higher, an Authorization header added: that is the retry.
  • The realm names the credentials. The realm string defines which set of usernames and passwords applies. A client that holds credentials for one realm will not use them for another.

When a SIP 401 Unauthorized is a real failure

The failure signature is a second challenge in reply to a request that already carried credentials. RFC 3261 says that a server rejecting credentials “may repeat its challenge, or it may respond with a 403 Forbidden”, and that the client “MUST NOT re-attempt requests with the credentials that have just been rejected.” That is why softphones give up with messages such as “authentication loop” and why Asterisk stops a failed outbound registration (see below).

There is, however, one exception: a stale nonce. If the second challenge carries stale=true, the server accepted the password but the nonce had expired, and the client is allowed to retry at once with a fresh nonce. That is not a failure. Asterisk’s nonces live for 32 seconds by default (nonce_lifetime), so a slow client or a long network path can occasionally meet one.

What you see What it means
401/407, then the request again with credentials, then 200 or 100 Trying Normal. Nothing to fix.
401/407, request with credentials, 401/407 again (no stale=true) Credentials rejected: wrong username, wrong password, wrong realm, or the server does not know the user at all
401/407, request with credentials, 403 Credentials rejected (common at SIP providers), or accepted and then refused by policy. See SIP 403 Forbidden
401/407, and the client never resends The client has no credentials for that realm, or does not support the offered algorithm
Second challenge with stale=true Expired nonce. The client should retry automatically

Repeated SIP 401 Unauthorized from Asterisk

On Asterisk, a wrong password and an unknown user look exactly the same from the phone: both get another 401. This is deliberate, because when no endpoint matches, Asterisk challenges the request with an internal “artificial” endpoint rather than revealing that the account does not exist; the comment in its source code says the aim is to avoid enabling “the discovery of SIP accounts”. The difference shows only in the Asterisk log (/var/log/asterisk/full), where each failure produces a NOTICE line of the form Request 'REGISTER' from '<sip:...>' failed for '<ip:port>' (callid: ...) - <reason>. The reason at the end is the diagnosis:

  • Failed to authenticate: Asterisk found the endpoint, and the digest did not match. Check the password, and check the username the phone puts in its Authorization header against the username in the endpoint’s auth section. They must match exactly.
  • No matching endpoint found: Asterisk could not work out which endpoint the request belongs to, so no password was ever checked. By default PJSIP identifies an endpoint by the user part of the From header (identify_by=username). If the phone’s From user is not the endpoint’s name (an extension number versus a name, or a display value), set identify_by=auth_username on the endpoint, or make the From user match. By contrast, trunks are normally identified by IP address through an identify section instead.

A real case: a browser phone stuck in a 401 loop

A user on the Asterisk forum spent weeks on a WebRTC client built with SIP.js 0.21 that looped on 401 (SIP.js reported “401 apparently in authentication loop, cannot authenticate”), while UDP phones on the same server worked. The Asterisk log said No matching endpoint found, and the REGISTER came from sip:anonymous.3c5i34@anonymous.invalid. Joshua Colp, the Asterisk project lead, pointed out that the client was putting “anonymous” in the From user instead of the real username.

In other words, the password was never the problem. SIP.js creates an anonymous user agent when it is given no address of record, and the poster’s code passed username, password and uri as top-level options, names that SIP.js 0.21’s SimpleUser does not read. It expects the address as aor and the credentials as userAgentOptions.authorizationUsername and authorizationPassword. As a result, Asterisk simply had no idea who was asking. If a browser phone loops on 401, check the From header of its REGISTER before touching the password. For a comparison of the main JavaScript SIP stacks, see JsSIP vs SIP.js vs Browser-Phone.

FreePBX extensions and scanner noise

On FreePBX, the extension number is both the endpoint name and the auth username, and the password is the extension’s Secret. A phone set up with a different auth username, or a secret copied with a trailing space, gives Failed to authenticate.

Logs full of 401s you did not cause are usually scanners. Internet-facing SIP ports are probed constantly, and every probe is challenged and logged as No matching endpoint found or Failed to authenticate from addresses you do not recognise. In other words, that is the artificial endpoint doing its job. Even so, restrict who can reach the port with a firewall or ACL, and use a tool such as fail2ban to ban repeat offenders.

401 or 407 from your SIP provider

When your PBX registers to a provider, or places a call through it, the provider is the one asking. Check, in this order:

  1. The authentication username. Many providers use an auth username that differs from your phone number or account number. VoIP.ms sub-accounts, for example, take the form 123456_name. In Asterisk it is the username in the auth section used by outbound_auth; in a FreeSWITCH gateway it is auth-username when it differs from username (see the gateway parameter reference). Some clients keep the two apart: Siperb’s Outbound Trunk settings, for instance, have a SIP Username for the To and From headers and a separate auth Username for the digest.
  2. The password, which is best retyped rather than pasted, in case of hidden characters.
  3. The realm. When Asterisk is the client, leave the auth section’s realm empty or set it to *. The Asterisk documentation is explicit: set a realm only if you know the exact realm the server will send. For example, a realm left over from a copied inbound auth section means the challenge never matches, and Asterisk cannot answer it. For the same reason, do not share one auth section between an endpoint’s auth and its outbound_auth.
  4. The right auth on the right request. On Asterisk, the registration and the endpoint are separate objects: the registration section’s outbound_auth answers challenges to REGISTER, and the endpoint section’s outbound_auth answers challenges to INVITE. A trunk that registers but whose calls fail with 401 or 407 is usually missing outbound_auth on the endpoint.

An Asterisk detail that catches people out: once a provider rejects the credentials, Asterisk stops trying. The outbound registration option auth_rejection_permanent defaults to yes, so “registration will not be attempted again until the configuration is reloaded”, and the log shows a “Fatal response” for the 401. So, after fixing the credentials, reload (module reload res_pjsip_outbound_registration.so, or pjsip send register for one registration) and check with pjsip show registrations.

MD5, SHA-256 and the algorithm mismatch

Digest authentication was built on MD5. RFC 7616 added SHA-256 and SHA-512/256, and RFC 8760 brought them to SIP. A server that supports several algorithms sends one challenge header per algorithm, most preferred first, and the client answers the first one it supports.

Asterisk gained SHA-256 and SHA-512-256 in 20.12.0, 21.7.0 and 22.2.0 (February 2025), with PJProject 2.15.1. Two new auth options control it, and both default to MD5:

  • supported_algorithms_uas: which challenges Asterisk sends to phones, in order.
  • supported_algorithms_uac: which challenges Asterisk will answer when a provider asks. If a provider challenges only with SHA-256 and this list holds only MD5, Asterisk cannot build an answer and the request fails again.

Two practical points follow from this. If you enable SHA-256 towards your phones, keep MD5 in the list and test every phone model, because a client that only knows MD5 depends on an MD5 challenge being offered as well. And if you store pre-hashed credentials (password_digest) instead of a plain password, you need one for each algorithm you list; a hash made with one algorithm cannot be converted to another. On a running system, pjproject show buildopts shows which algorithms your build supports.

Capturing the exchange

To diagnose a SIP 401 Unauthorized properly, you need both requests and both responses, not just the code on the phone’s screen.

  • On Asterisk: pjsip set logger on (or pjsip set logger host 203.0.113.10 for one device), reproduce, then pjsip set logger off, and read the matching NOTICE line in /var/log/asterisk/full.
  • On FreeSWITCH: sofia profile internal siptrace on (or external for trunks), reproduce, then turn it off.
  • On any Linux server: sngrep draws the exchange as a ladder diagram, which makes a second challenge obvious.
  • On a browser phone: the developer console (F12). Most JavaScript SIP libraries can log every SIP message there.

Compare the username and realm in the client’s Authorization (or Proxy-Authorization) header with what the server expects. If a SIP proxy sits between you and the provider, check the Via and Server headers to see which hop sent the challenge.

Frequently asked questions

Is a SIP 401 Unauthorized an error?

Not on its own. The first 401 or 407 is the normal start of digest authentication. It is a problem only when the server challenges again after the client has sent credentials.

What is the difference between SIP 401 and 407?

Who is asking. A 401 comes from the destination (a registrar, a PBX or the called user agent) and uses the WWW-Authenticate and Authorization headers. A 407 comes from a proxy on the path and uses Proxy-Authenticate and Proxy-Authorization.

Why does my phone keep getting 401 when the password is right?

On Asterisk, the most common reason is that the server cannot identify the endpoint at all, which the log shows as No matching endpoint found. Check that the From user (or, with identify_by=auth_username, the auth username) matches the endpoint’s name.

What is the difference between SIP 401 and 403?

A 401 asks for credentials and the request can succeed once they are supplied. A 403 refuses the request outright, and repeating it unchanged will not help.


Next in this series: SIP 503 Service Unavailable.

Leave a Reply

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