Every Rails secret key base generator does one job: it hands you a long string of random characters that your app uses to sign and protect browser data. Rails ships the generator built in, so you can create a production-ready value in a few seconds and never paste an unsafe placeholder into your Ruby on Rails project again. Below you will find every way to create the value, where each Rails environment expects it, and how to replace it later.
What secret_key_base does in your Ruby on Rails app
A Rails application needs one private input from which it derives all of its signing and encryption material. That input is secret_key_base. Rails feeds it to a key generator, which builds every message verifier and message encryptor the application uses, including the ones that guard cookies. If a stranger learns the value, they can forge a login, so treat it with the same care you give a database password.
Security teams like this design because Rails keeps one secure secret on disk and derives many purpose-specific values from it, rather than reusing a single raw string in every feature of the app.
How the key generator derives signing keys
Under the hood, Rails wraps OpenSSL's PBKDF2 implementation. You hand it a salt and a length, and it returns a derived value. The default length of 64 bytes matches what ActiveSupport::MessageVerifier expects, which is why values derived for one feature never collide with those of another.
Cookies and sessions depend on one secret
Your login session lives inside a signed browser token. Change the underlying input and every existing token fails verification, because authentication data signed with the old value no longer matches. Rails cannot repair those logins, so keep this in mind before you swap the value on a live site.
Rails secret key base generator: run the secret command
The quickest route is the built-in task. From the root of your project, run bin/rails secret. Rails generates a cryptographically strong string and prints it, and you can paste the result into a configuration file. Prefix the call with bundle exec if your shell picks up the wrong gem versions. For a quick integration test, output from the free random token generator can be hex, base64url or alphanumeric, and bulk mode returns many at once.
Run the command in your terminal
Open a terminal in the project folder and enter the task. Rails prints one line of 128 hexadecimal characters and exits. It does not touch your database, start a rails server process or change any file, so you can repeat it as often as you like and discard what you do not use. The same command line works on macOS, Linux and Windows with WSL.
3f9a1c…(128 hexadecimal characters in total)Generate a key without the Rails command
Sometimes the Rails executable is missing, for example on a build server that only ships your compiled image. You can create an equivalent secret base key with plain tools instead. The one-liner openssl rand -hex 64 prints the same shape of value, and ruby -rsecurerandom -e 'puts SecureRandom.hex(64)' does the same inside Ruby. Hackers cannot guess either result, since both draw from the operating system's entropy pool, and you do not need a Rails checkout at all.
Generate a secret in the Rails console
You can also ask a running Rails console to do the work. Start it with bin/rails console, then call SecureRandom.hex(64). It prints the same kind of value as the command above, which makes it handy when a console is already open. Quit the session afterwards so the output does not linger in your shell history.
Copy the value into your environment
Once you have the string, decide where it belongs: a protected file, an environment variable or a hosting dashboard. Never commit it to a plain text file in your Rails repository, and never email it to yourself. A password manager is a better scratch pad than a chat window.
Where Rails looks for the secret_key_base
Rails resolves the value differently depending on where the app runs. The table summarises the lookup order so you can see which setup applies to your Rails application.
| Environment | Where Rails reads the value | What you must do |
|---|---|---|
| Local work | A generated file named tmp/local_secret.txt | Nothing; Rails creates it for you |
| Live site | The SECRET_KEY_BASE variable, then the encrypted file | Provide one of the two before boot |
| Asset build step | The SECRET_KEY_BASE_DUMMY=1 flag | Set the flag only for precompiling |
Whichever row applies, remember that each deployed application should own its value. Sharing one string between a staging application and a live application means a leak in the weaker environment compromises the stronger one, so generate a separate value for every application you run, and record where each one is stored so a teammate can find it during an incident.
Development and test behave differently
In development and test, Rails generates a random value on first boot and stores it in a temporary file inside your project. You never configure it, which is why a fresh rails new project just works, and why a second rails new run produces a different value. Version control ignores the file by default, and each developer on a Rails team ends up with a different value on their own machine.
Use a placeholder during asset builds
Precompiling assets inside a Dockerfile does not need the real value. Setting SECRET_KEY_BASE_DUMMY=1 tells Rails to use a throwaway string for that one step, so your production environment value never enters the build layer. Create the new Rails application image once, and supply the real value only when the container starts.
Store the signing key in encrypted credentials
For most apps the correct home is the file at config/credentials.yml.enc. Run bin/rails credentials:edit and add the value under the matching entry; Rails then scrambles the file when you save. This approach lets you keep encrypted credentials in the repository safely, because nobody can read the file without its matching decryption value.
That decryption value lives in config/master.key on your laptop, or in the RAILS_MASTER_KEY variable on a server. Either way, the app reads the stored string at boot with no extra code from you.
Edit the encrypted file
The edit command opens your $EDITOR, decrypts the file in memory and re-encrypts it on close. If you prefer Visual Studio Code, run EDITOR="code --wait" bin/rails credentials:edit. Rails never writes the readable text to disk, so no leftover plain file needs cleaning up.
Keep the master file out of GitHub
Rails lists the master file in .gitignore when you generate a project. Check that line before your first push. A leaked repository on GitHub is exactly how credentials escape, and automated scanners find exposed files within minutes.
Pass secret_key_base into Docker containers
A container starts with a clean file system, so the app cannot find a master file unless you supply one. The simplest option is a variable in your deployment manifest or Docker Compose file. Rails reads that variable first, before it ever opens the encrypted file, and the same rule applies wherever you run your Rails application.
When you build the image, pin and document the Ruby version and Rails version, so the generator you run today produces values compatible with the framework inside the image. Pull the actual value from a secret manager at run time rather than baking it into a layer, and give each one only the access it needs.
A Docker recipe for the signing value
Mount the value as a file, point a variable such as SECRET_KEY_BASE_FILE at the mount path, and have your entrypoint export it before starting Rails. With Docker you declare the file under a top-level secrets block and reference it from the service. This keeps the string out of docker inspect output and out of your shell history, and the app sees it only while it runs.
Fix a missing secret_key_base error in a Rails application
The most common symptom of a missing value is an exception at startup saying the app needs one for the live environment. The message appears when Rails finds neither a variable nor a stored entry, so the fix is simply to supply one using any method above.
Check what Rails can see
Open the console in the same environment as the failing process and inspect Rails.application.secret_key_base. A blank result means your variable did not reach the process, which usually happens when a service manager, not your login shell, starts the Rails app. Export the variable in the service definition rather than a profile script.
Confirm the master file matches
If you store the value in the encrypted file, the error can also mean the decryption value on the server is stale. Compare it against your local copy, and regenerate the file with bin/rails credentials:edit only when you are certain nobody else relies on the old contents.
Generating a new secret_key_base after a failed Friday deploy
Maria Okafor is shipping a small invoicing app, Harbor Ledger, to a container host at 4:40 pm, and the deploy fails with a missing secret_key_base error. She needs a new value, and she needs to know it is long enough that she can trust it.
She opens a shell in the project, runs bin/rails secret and gets 128 hexadecimal characters. Because each character is 4 bits, that is 512 bits of randomness, which she confirms by counting the characters in Python rather than eyeballing them. She sets SECRET_KEY_BASE in the host's dashboard, redeploys, and the app boots.
Before closing the laptop she reruns the idea with one input changed, to see whether a shorter value would have been fine:
| Bytes requested | Hex characters | Bits |
|---|---|---|
| 16 | 32 | 128 |
| 32 | 64 | 256 |
| 64 | 128 | 512 |
Even 32 bytes would resist guessing, but the 64-byte default matches the length Rails derives for its message verifier, so she keeps it. My judgement: the length is not the number to watch; where the value lives is. A 512-bit string pasted into a chat thread is weaker than a 128-bit one kept in a protected store.
One edge case catches her: her staging app and her live app both started from the same pasted value. A leak from staging would therefore sign valid logins on the live site, so she generates a second value and rotates the live one, accepting that current users will be signed out. She schedules the swap for 9 pm, when traffic is lowest, and restarts all three containers together so no half-updated server rejects the others' cookies.
Rotate a secret base key without downtime
You should rotate the value after a suspected leak, when a team member with access leaves, or on a schedule your security policy sets. The procedure is short: generate a new string with the same command you used originally, update the variable or the stored entry, then restart every Rails process so they all pick it up at once.
Restarting only half your servers is the classic mistake, because the two halves then reject each other's tokens and users bounce between logged-in and logged-out states.
Plan for signed-out users
Swapping the value invalidates every active login, because each cookie was signed with the old secret_key_base. Apps that must avoid forced logouts, such as a busy store or a banking dashboard, can configure Rails to accept the old value for verification while signing with the new one, then drop the old value after the token lifetime passes. Note the cutover date next to the stored entry, so the next person knows which generated value is current.
Frequently Asked Questions
- How is this different from rails secret?
- It produces an identical format -- 64 random bytes encoded as 128 lowercase hex characters -- computed in your browser instead of via the Rails command line. Use this when you need a value quickly without a local Rails installation or terminal access.
- Where should I put the generated value?
- As of Rails 5.2+, store it via encrypted credentials (bin/rails credentials:edit, under secret_key_base:) rather than a plain environment variable or config/secrets.yml -- the credentials system is the current recommended approach and keeps the value encrypted at rest in your repository.
- What does secret_key_base actually protect?
- It's the root key Rails uses to derive signing and encryption keys for session cookies, CSRF tokens, and any data processed through ActiveSupport::MessageEncryptor or MessageVerifier. Anyone who obtains it can forge signed cookies or decrypt encrypted session data for your application.
- What happens if I change secret_key_base?
- Every existing session and any previously signed or encrypted cookie value becomes invalid immediately -- all logged-in users will be signed out. Treat rotation the same way you'd treat any other credential rotation: deliberate, and expected to force re-authentication.
- Is secret_key_base the same as the Rails master key?
- No -- the master key (config/master.key) is used once at boot to decrypt your encrypted credentials file, which is where secret_key_base itself is typically stored. They're related but distinct: rotating the master key alone doesn't invalidate sessions, but rotating secret_key_base always does.