Generate a JWT secret key in the terminal by running openssl rand -hex 32, which prints 32 cryptographically random bytes as a 64-character hexadecimal value suitable for an HS256 secret.

For an HS256 signature, the shared secret must be at least 32 bytes. RFC 7518 defines the HS256 key-size floor as 256 bits, or 32 bytes. The hexadecimal output recommended below contains 64 UTF-8 bytes when entered literally, so it comfortably meets that minimum. The secret signs a token; it is not included in the token and does not encrypt its claims.

how to generate jwt secret key in terminal
How to Generate a JWT Secret Key in the Terminal

Generate the secret with OpenSSL

On macOS, Linux, and systems that include OpenSSL, the direct command is:

openssl rand -hex 32

OpenSSL prints one line containing 64 hexadecimal characters. Those characters represent 32 random bytes, with two hexadecimal characters used for each byte. When you enter the printed text into an application, each ASCII character occupies one UTF-8 byte, making the literal secret 64 bytes long.

Hexadecimal is convenient for environment variables, configuration files, and copied terminal output because it contains no spaces, quotation marks, padding characters, or shell separators. Keep the result as one uninterrupted line. Do not capitalize it, remove characters, insert spaces, or let one system decode it while another uses the encoded text; any such difference can create a different signing key.

OpenSSL also offers a base64 form:

openssl rand -base64 32

This prints the base64 representation of 32 random bytes. If an application uses the printed base64 characters literally, those characters become the secret rather than the underlying 32 bytes. If the application decodes the value first, it receives a different 32-byte key. Decide on one canonical representation and use it consistently. A redirected base64 file may also retain a trailing newline, so hexadecimal with an explicit no-newline write is easier to control.

For a library that accepts a raw binary key, openssl rand 32 writes exactly 32 random bytes. That binary is not necessarily valid UTF-8 and can contain characters that are awkward in shell variables or text configuration, so hexadecimal is generally the safer terminal representation.

Create and protect a terminal secret

  1. Confirm that OpenSSL is available. Run openssl version. If the command is unavailable, use the Node.js or Python alternatives below.
  2. Generate the value once. Run openssl rand -hex 32. Do not rerun it and expect the same result; every execution creates a new secret.
  3. Write it without a trailing newline. In a POSIX-style shell, run umask 077, followed by printf '%s' "$(openssl rand -hex 32)" > .jwt-secret, and then run chmod 600 .jwt-secret. The command stores the generated value rather than placing it directly in the command line.
  4. Check its length and format without displaying it. Run wc -c < .jwt-secret && grep -Eq '^[0-9a-f]{64}$' .jwt-secret && echo "format ok". The byte count should be 64, and the final message confirms the expected lowercase hexadecimal pattern.
  5. Load it into a local development session when needed. Run export JWT_SECRET="$(cat .jwt-secret)". This avoids placing the literal value in shell history, although child processes can still inherit the environment variable, so it is not ideal for production secret distribution.
  6. Move production secrets into a managed store. Provision, audit, restrict, rotate, and retire the production key through the system’s approved secret-management process. A temporary local file should also be excluded from version control and removed after the test is complete.

Permissions are only one part of key hygiene. Avoid placing the secret in application configuration committed to Git, CI command output, support tickets, shell tracing, or diagnostic logs. If a real secret is pasted or printed outside its intended boundary, rotate or revoke it rather than assuming the disclosure was harmless.

Generate the same format with Node.js or Python

Node.js can produce the same 64-character hexadecimal shape with its cryptographic random generator:

node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

Python’s standard secrets module provides the equivalent command:

python3 -c "import secrets; print(secrets.token_hex(32))"

Both commands print 64 hexadecimal characters representing 32 random bytes. They are useful when OpenSSL is not installed or when a project already standardizes on one runtime. When saving their output, remove the terminal newline just as described in the file-writing step. Also, do not replace either command with a general-purpose pseudo-random function such as JavaScript’s Math.random(); it is not designed to protect signing keys.

Keep signing and verification on one canonical secret

The issuer and verifier must use the exact same secret bytes. “The same secret” does not mean merely looking similar: a final newline, a Unicode normalization difference, a changed hex letter, or one side decoding base64 while the other uses the encoded text can all cause verification to fail. Document the canonical format, such as “64 lowercase hexadecimal characters, no newline,” and apply that same rule on both sides.

A compact signed JWT uses the JWS compact form with three dot-separated segments: the protected header, payload, and signature. RFC 7515 defines that serialization. The header describes the signing operation, but it is not an instruction the verifier should blindly trust. Configure the target JOSE library with an explicit algorithm allowlist that permits only HS256 for this key, then apply the application’s issuer, audience, lifetime, and key-rotation policies.

The generator does not make those policy decisions for you. If you include an exp claim, its NumericDate value is seconds since the Unix epoch, not JavaScript milliseconds. Define whether claims such as iss, aud, sub, iat, nbf, and jti are required, and determine clock-skew tolerance, replay controls, and revocation behavior. During key rotation, accept an old key only for a deliberate overlap period; never implement an unrestricted fallback to any available secret.

Use a terminal-generated key for a local test token

After generating a key in the terminal, the JWT Generator can perform the next supported step: signing a development token from strict JSON claims in the browser. Use this workflow only with disposable, non-production claims and secrets. Claims and key material remain in the current browser tab and are processed with Web Crypto.

  1. Prepare a strict JSON claims object. The input must be a JSON object, not an array or null. Arrays, null, comments, trailing commas, undefined, NaN, Infinity, and JavaScript expressions are rejected.
  2. Enter the terminal-generated test secret. The generator requires at least 32 UTF-8 bytes. A 64-character hexadecimal secret contains 64 UTF-8 bytes when entered as text, so it satisfies that requirement. Do not paste a production key into the page.
  3. Generate and inspect the compact token. The protected header is fixed to alg: HS256 and typ: JWT. Review the claims, the three segment lengths, and the absence of confidential information before copying the token.
  4. Verify the copied result in the target JOSE library. Use an explicit HS256 algorithm setting and check the required issuer, audience, lifetime, and key policy. The generator creates the signature but does not independently verify the token or contact an identity provider.

The fixed algorithm prevents the input from silently selecting an unsecured token or a mismatched asymmetric algorithm. The header and claims are UTF-8 encoded and base64url encoded without padding. The secret signs the exact header.payload byte sequence using HMAC-SHA-256.

HS256 provides integrity and authenticity, not confidentiality. Anyone holding the token can decode its header and claims without the secret. Do not place passwords, private keys, personal records, or confidential application data in the payload merely because the token is signed. The page also does not add expiration, issuer, audience, or any other claim automatically.

Avoid mistakes that weaken an otherwise valid setup

  • Do not invent a memorable secret. A short phrase, repeated word, default example, or predictable application name is easier to guess and unsuitable for protecting a signature.
  • Do not change the encoding between systems. Decide whether the canonical key is hexadecimal text, base64 text, or raw bytes, and document whether a trailing newline is excluded.
  • Do not assume a valid signature makes a token safe. A syntactically correct token can still have the wrong audience, an unsuitable lifetime, excessive privileges, or missing replay controls.
  • Do not treat clipboard handling as harmless. Clipboard history, screenshots, browser extensions, logs, chat messages, and support tickets can expose bearer credentials. Reconsider tokens before copying them into an unapproved system.
  • Do not treat JWT claims as encrypted. Base64url is an encoding, not encryption. Keep sensitive information out of the payload.
  • Do not rely on the browser page to provision a production key. A securely generated value can still become unsafe when displayed in a browser tab. Production keys need controlled storage, distribution, auditing, and rotation.

Compare terminal and browser workflows

Method Secret representation Best fit Main boundary
OpenSSL terminal command 64 hexadecimal characters representing 32 random bytes Provisioning a shared HS256 key Standard output can reach scrollback, CI logs, or copied text
Node.js or Python terminal command 64 hexadecimal characters representing 32 random bytes Environments that already provide the relevant runtime The secret is still displayed unless captured carefully
Browser JWT Generator An entered secret or a generated test value shown as hexadecimal Local, non-production HS256 token testing Claims and key material are visible in the browser tab and are not automatically provisioned or stored

Use the terminal to create and control the secret, then load it into the application through an approved mechanism. Use a maintained JOSE library in production so algorithm allowlists, key discovery, rotation, validation, and error handling remain beside the application’s trust policy. For local testing, the terminal-generated non-production key can flow into the JWT Generator and then into the same explicit verification path used by the destination application.