Kerberos Authentication for Database Connections
Sign in to a database with your company login instead of a password: how the tickets work, what to set in the JDBC driver, and what to do when it fails.
On this page
Words in this article
Words in this article
- Ticket
- A short-lived, signed proof of who you are.
- KDC
- Key Distribution Center: the login server that hands out tickets.
- TGT
- Ticket-granting ticket: a pass for getting more tickets without your password.
- SPN
- Service principal name: the database's account name in Kerberos.
- Realm
- The name of your Kerberos network, in capitals.
- JDBC driver
- The Java library that lets a tool talk to one kind of database.
- JAAS
- The part of Java that handles logins.
- NTLM
- The older Windows login, used when Kerberos fails.
The short answer
Kerberos lets you sign in to a database with your company login instead of a database password. A central login server gives your computer a ticket: a short-lived, signed proof of who you are. To use Kerberos from a Java tool such as DbSchema, you need three things:
A ticket on your computer
On Windows, signing in to the company network gives you one. On Linux and macOS, the command
kinitasks for one.The driver settings
One to three settings per database. In DbSchema, Kerberos for SQL Server is a menu choice.
Clocks that agree
Your computer and the login server may differ by five minutes at most.
What DbSchema adds on a Kerberos network
Why DbSchema
Companies that switch on Kerberos usually want no database passwords on laptops. DbSchema connects with your ticket, so you can design and document databases inside that rule. After the login, reverse-engineer the database into an ER diagram, generate documentation, and compare the design model with the database.
No driver setup
DbSchema downloads the JDBC driver for your database. For SQL Server, Java Kerberos is a choice in a menu, so you type no settings.
One login on Windows, macOS and Linux
DbSchema runs on all three. With Java Kerberos it signs you in to SQL Server from a Mac or a Linux laptop too.
A view of what the driver receives
The eye button next to SSL & Parameters shows the full JDBC URL that DbSchema connects with. When a login fails, check this URL first.
No password on disk
With Kerberos there is no password to store. DbSchema saves one only when you tick Remember.
Then, under your company login:ER diagramDocumentationCompare model and database
How a Kerberos login works
Kerberos is the login system behind Windows company networks, and many Linux networks use it too. A trusted login server sits between you and every service you use, such as a database. So the database never needs your password[1].
Three machines take part in every login:
- Your computer. A JDBC driver runs here. A JDBC driver is a small Java library that lets a tool talk to one kind of database.
- The KDC. KDC stands for Key Distribution Center. This central server holds every account's secret key and hands out tickets. In a Windows company network, the domain controller is the KDC. The domain controller is the server that checks every Windows login.
- The database server. The database shares a secret key with the KDC. With this key, the database can check every ticket the KDC issues.
The login runs in five steps:
- You sign in once, with
kinitor your Windows login. - The KDC sends back a ticket-granting ticket, or TGT. With a TGT, you can ask for more tickets without typing your password again. Your computer stores the TGT in the ticket cache, a small file or memory area on your machine.
- When you connect, the driver asks the KDC for a ticket to one database. The driver names the database by its SPN, short for service principal name. The SPN is the database's account name in Kerberos. For SQL Server it has the form
MSSQLSvc/host:port, such asMSSQLSvc/db-prod.corp.example.com:1433. - The KDC returns a service ticket for that SPN.
- The driver hands the service ticket to the database. The database checks the ticket with its secret key and lets you in.
So no database password sits in a configuration file. When someone leaves, IT disables one account, and all their database access ends.
The driver settings for each database
The JDBC driver does the Kerberos work, and each driver has its own settings to switch it on.
SQL Server2 settings
integratedSecurity=true
authenticationScheme=JavaKerberos
Without the first setting, the driver asks for a password.
PostgreSQLno setting
# pg_hba.conf on the server
host all all 0.0.0.0/0 gss
The server asks for Kerberos. The driver follows.
Oracle3 settings
oracle.net.authentication_services=(KERBEROS5)
Plus mutual authentication and the ticket cache path, shown below.
SQL Server
Set both settings. If integratedSecurity=true is missing, the driver ignores authenticationScheme and asks for a user name and password[2].
jdbc:sqlserver://db-prod.corp.example.com:1433;databaseName=sales;integratedSecurity=true;authenticationScheme=JavaKerberos;encrypt=true
The driver builds the SPN from the host name and the port in the URL. Since driver 6.2, the driver also adds the realm. The realm is the name of your Kerberos network, written in capitals, such as CORP.EXAMPLE.COM. Write the SPN yourself only when the host name in the URL is not the server's real name, for example a second name set up in DNS: serverSpn=MSSQLSvc/db-prod.corp.example.com:[email protected].
PostgreSQL
The PostgreSQL driver needs no Kerberos setting in the URL. On the server, a line in the pg_hba.conf file with the method gss makes the database ask for Kerberos[3]. GSS, or GSSAPI, is the standard interface that programs use to speak Kerberos.
jdbc:postgresql://pg-prod.corp.example.com:5432/analytics?sslmode=require
The driver then asks for a ticket to the service postgres. If your server runs Kerberos under another service name, set kerberosServerName[4].
Oracle
The Oracle JDBC driver needs three settings[5]. The first switches Kerberos on. The second makes the database prove its identity to you as well, called mutual authentication. The third is the path of your ticket cache file; klist prints it in its first line.
oracle.net.authentication_services=(KERBEROS5)
oracle.net.kerberos5_mutual_authentication=true
oracle.net.kerberos5_cc_name=/tmp/krb5cc_1000
The two Java configuration files
Most connections work without extra files. Add these two when the driver cannot find your realm or your ticket:
- krb5.conf names your realm and its KDC. On Linux and macOS, the file usually sits at
/etc/krb5.conf. - A JAAS file tells Java where to find your ticket. JAAS is the part of Java that handles logins. Each driver reads its own block in the file:
SQLJDBCDriverfor SQL Server,pgjdbcfor PostgreSQL. The SQL Server driver reads the ticket cache even without a JAAS file[2].
This JAAS file reads the ticket cache. For PostgreSQL, rename the block to pgjdbc:
SQLJDBCDriver {
com.sun.security.auth.module.Krb5LoginModule required
useTicketCache=true
doNotPrompt=true;
};
Pass both files to Java when it starts:
-Djava.security.krb5.conf=/etc/krb5.conf -Djava.security.auth.login.config=/path/to/jaas.conf
Kerberos does not encrypt your data
Kerberos proves who you are. It does not hide what you send. Your queries and their results cross the network as readable text, unless you also switch on encryption[6].
Add the encryption setting next to the Kerberos settings:
| Database | Setting that encrypts the connection |
|---|---|
| SQL Server | encrypt=true |
| PostgreSQL | sslmode=require for TLS, or gssEncMode=require to encrypt with the Kerberos session key |
| Oracle | the network encryption settings of Oracle Net, Oracle's network layer |
The two errors almost everyone hits
Most failed Kerberos logins show one of these messages:
What it means
Java found no ticket it may use.
Fix
Run kinit as the same user that starts the tool, then try again.
What it means
Your clock and the KDC's clock differ by more than five minutes.
Fix
Sync your clock with the domain's time server.
The five-minute limit is the default clockskew of 300 seconds in krb5.conf[7]. It stops someone from reusing an old, captured ticket.
Get a fresh ticket and check it:
kinit [email protected]
klist
klist lists the tickets in your cache with their expiry times. An empty list means the driver will not find a ticket either. Sometimes klist shows a ticket and Java still finds none. Then start Java with -Djavax.security.auth.useSubjectCredsOnly=false. With this flag, Java reads the ticket cache directly.
On Windows, Java may not be allowed to read the ticket of your Windows login. To allow it, set the registry value AllowTgtSessionKey to 1 under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\Kerberos\Parameters, then restart the tool[2].
Check that Kerberos was really used
A connection can work and still skip Kerberos. SQL Server falls back to NTLM, the older Windows login method, when the KDC knows no SPN for the server. So check the session after you connect, for example in the DbSchema SQL Editor. If it shows NTLM, ask your domain administrator to register the SPN.
On SQL Server, read sys.dm_exec_connections:
SELECT auth_scheme
FROM sys.dm_exec_connections
WHERE session_id = @@SPID;
KERBEROS means the ticket was used. NTLM means the fallback happened. Run the query from your own computer. On the server itself, Windows skips Kerberos, so the result is always NTLM[8].
On PostgreSQL, read the pg_stat_gssapi view:
SELECT gss_authenticated, principal, encrypted
FROM pg_stat_gssapi
WHERE pid = pg_backend_pid();
gss_authenticated is true when Kerberos signed you in. principal shows your Kerberos account name, such as [email protected], and encrypted shows whether Kerberos also encrypts the connection[9].
Connect with Kerberos in DbSchema
DbSchema downloads the JDBC driver for your database, so you only add the Kerberos settings. Connecting needs no license: the free Community Edition connects to every database.
-
Get a ticket
Sign in to Windows, or run
kinit. -
Open the connection dialog Community
Choose your database and the Method Standard. Fill in the host, the port and the database.
-
Open the Network and Driver tab
The tab is shown in the screenshot below.
-
Switch Kerberos on
For SQL Server, press the pencil next to SSL & Parameters. A dialog with the driver's settings opens. Choose Authentication → Integrated Security Scheme → Java Kerberos. For Oracle, type the three settings into the Properties field as
key=value;key=value. PostgreSQL needs nothing here. -
Test and connect
Press Test Connection, then Connect.
Then run the check query from Check that Kerberos was really used in the SQL Editor.
DbSchema keeps the connection without a password. Next time, a fresh ticket is all you need. Download DbSchema and connect with your company login.
Frequently asked questions
Do I need a keytab file?
Not for your own logins: the ticket from kinit or your Windows login is enough. A keytab is a file that stores an account's secret key. Services and scheduled jobs use one to get tickets without a password.
Can I use Kerberos from macOS or Linux to reach SQL Server?
Yes: add authenticationScheme=JavaKerberos to integratedSecurity=true, and the login works on every operating system. Without JavaKerberos, the driver uses a Windows-only library for the login[2].
Sources
Connect over Kerberos and see the schema
DbSchema downloads the JDBC driver for your engine, takes your Kerberos settings on the connection dialog's Network and Driver tab, and draws the schema as an ER diagram. Every edition covers every supported database engine, and the Community Edition is free.