Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux Keyrings: What They Are & Why Use Them

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Linux keyrings are a kernel-level feature that securely stores cryptographic keys, passwords, and other sensitive data in memory, and you should use them because they keep secrets out of plain text files, automatically lock or clear them when not needed, and provide a central, permission-controlled way to manage credentials for applications and services. The keyring system is built into the Linux kernel, so it’s available on virtually every distribution without extra software.

What You'll Need

  • A Linux system with a standard kernel (any modern distribution works).
  • Basic comfort with the command line (you’ll use terminal commands).
  • For the examples, you may need to install the keyutils package (on Debian/Ubuntu: sudo apt install keyutils; on Fedora: sudo dnf install keyutils).
  • Optional: a desktop environment with a keyring daemon (like GNOME Keyring or KWallet) for GUI integration.

Step-by-Step

  1. Understand the keyring hierarchy. The kernel maintains several keyrings: the process keyring (per-program), the session keyring (per-login session), the user keyring (per-user), and the group keyring. Most applications use the session or user keyring by default.
  2. Check if keyutils is installed. Run keyctl --version in a terminal. If it’s not found, install it using your package manager as shown above.
  3. List your current keyrings. Type keyctl show to see the keyrings associated with your session. You’ll see lines like Session keyring with a number and a list of keys (if any).
  4. Add a secret to the session keyring. Use keyctl add user mykey "mysecretvalue" @s to store a key named mykey with the value mysecretvalue in the session keyring (@s). The command returns a key ID (a number).
  5. Read the secret back. Run keyctl print (replace with the number from the previous step). You’ll see the secret value. Note that only processes in the same session (or with appropriate permissions) can read it.
  6. Set permissions. Use keyctl setperm 0x3f000000 to give read/write to the owner only. For more granular control, see the keyctl man page (man keyctl).
  7. Use a keyring in a script. Instead of hardcoding a password, your script can request it from the keyring. For example, in bash: secret=$(keyctl print $(keyctl search @s user mykey)) then use $secret.
  8. Integrate with a desktop keyring daemon. If you use GNOME or KDE, the desktop keyring (like GNOME Keyring) is automatically unlocked at login and stores Wi-Fi passwords, browser credentials, and SSH keys. You can manage it with the secret-tool command (part of libsecret) or the GUI “Passwords and Keys” app.
  9. Clear a key when done. After your application finishes using a secret, unlink it with keyctl unlink @s to remove it from the keyring. This reduces the window of exposure.
  10. Set key expiration. Use keyctl expire 60 to make the key expire after 60 seconds. This is useful for temporary credentials.

Common Mistakes

  • Storing secrets in world-readable files. Keyrings are not a replacement for proper file permissions, but they are safer than a plaintext file with mode 644.
  • Using the wrong keyring scope. If you add a key to the process keyring, it disappears when the process exits. If you need it across sessions, use the user or session keyring.
  • Forgetting to set permissions. By default, other users on the same system might be able to read your keys if they have access to the same keyring. Always restrict permissions.
  • Assuming keyrings are encrypted at rest. Keyrings live in kernel memory, not on disk. They are not encrypted when the system is off. For persistent encrypted storage, use a password manager or encrypted file.
  • Not clearing keys after use. Leaving secrets in memory longer than necessary increases risk if the system is compromised.

Tips

  • Use the keyctl command’s @u for the user keyring, @s for session, and @p for process. Know which one fits your use case.
  • For application development, use libraries like libkeyutils or higher-level bindings (Python’s keyutils, Rust’s keyring crate) to integrate keyrings directly.
  • When using SSH, consider ssh-agent which uses a keyring-like mechanism to hold your private keys in memory. It’s a good practice over storing keys on disk without a passphrase.
  • For system services, use the kernel keyring to store service credentials, and set the keyring to be cleared on service stop via keyctl clear.
  • If you’re on a headless server, you can still use keyrings; just be aware that the session keyring is tied to the login session, so use the user keyring for long-lived keys.

Troubleshooting

If keyctl is not found: Install the keyutils package. On Debian/Ubuntu: sudo apt install keyutils. On Fedora: sudo dnf install keyutils. On Arch: sudo pacman -S keyutils.

If you get “Key has been revoked” or “Permission denied”: Check the key’s permissions with keyctl show and adjust using keyctl setperm. Also ensure you are in the same session (for session keyring) or the same user.

If a key disappears after reboot: Keyrings are volatile—they are cleared on reboot. For persistent storage, use a password manager or encrypt a file with a key that is unlocked at login (e.g., via PAM).

If GUI apps prompt for a password repeatedly: The desktop keyring daemon may not be unlocked automatically. Check that the daemon is running (e.g., gnome-keyring-daemon) and that you have the right packages installed.

If a service cannot access a key: Services often run under a different user or session. Use the user keyring (@u) and ensure the service user has permission to read the key, or use a dedicated keyring for the service.

Frequently Asked Questions

Are Linux keyrings encrypted?

Keyrings themselves are not encrypted; they are stored in kernel memory. However, the kernel protects them from being swapped to disk (unless explicitly configured otherwise). For encryption at rest, use an encrypted filesystem or a password manager.

What is the difference between a keyring and a password manager?

A keyring is a low-level kernel facility for holding secrets in memory, while a password manager (like KeePass or Bitwarden) is an application that encrypts and stores secrets on disk. They can work together—password managers often use keyrings to store the master password or database key.

Can I use keyrings to store API tokens for scripts?

Yes, that’s a common use case. You can add a token to the session keyring and have your script retrieve it, avoiding hardcoding the token in the script file.

How do I list all keys in my keyring?

Use keyctl show to list all keyrings and their keys. For a specific keyring, use keyctl list .

What happens to keys when I log out?

The session keyring is destroyed when the session ends. The user keyring persists as long as the user is logged in, but is cleared on reboot. The process keyring is destroyed when the process exits.

Is it safe to store passwords in a keyring?

Yes, for short-term use in memory. It’s safer than storing them in plaintext files. However, because keyrings are not encrypted at rest, don’t rely on them for long-term storage—use a proper password manager for that.