432

I'm generating a self-signed SSL certificate to protect my server's admin section, and I keep getting this message from OpenSSL:

unable to write 'random state'

What does this mean?

This is on an Ubuntu server. I have upgraded libssl to fix the recent security vulnerability.

1
  • I am seeing that even though this error is thrown, it wont stop by generating the intended certificate format file, i was using a different openssl command to convert the .crt to .pfx, it indeed output the pfx though this error is printed
    – rinilnath
    Dec 16, 2022 at 13:22

9 Answers 9

569

In practice, the most common reason for this happening seems to be that the .rnd file in your home directory is owned by root rather than your account. The quick fix:

sudo rm ~/.rnd

For more information, here's the entry from the OpenSSL FAQ:

Sometimes the openssl command line utility does not abort with a "PRNG not seeded" error message, but complains that it is "unable to write 'random state'". This message refers to the default seeding file (see previous answer). A possible reason is that no default filename is known because neither RANDFILE nor HOME is set. (Versions up to 0.9.6 used file ".rnd" in the current directory in this case, but this has changed with 0.9.6a.)

So I would check RANDFILE, HOME, and permissions to write to those places in the filesystem.

If everything seems to be in order, you could try running with strace and see what exactly is going on.

8
  • 44
    My system was giving me this issue because the ".rnd" file was owned by root rather than my user. A quick sudo chown user:user ~/.rnd made everything work out.
    – HalfBrian
    Jun 4, 2010 at 16:19
  • 1
    I had the same problem as OP. I did the sudo thing and it worked. But why do I still have a .rnd directory iwned by root in my $HOME after I create a self-signed certificate ?
    – Luc M
    Jul 9, 2012 at 2:36
  • 3
    Yes, if you run from php web server, the user is www-data, and you should add "export" before every openssl: shell_exec('export RANDFILE=".rnd";openssl ecparam -genkey -name secp256k1'))
    – diyism
    Jun 21, 2013 at 9:04
  • 2
    If you are using a scripting language like PHP to call openssl as www-data, you can solve this by creating /var/www/.rnd and chowning it to www-data. (Assuming that /var/www is www-data's home folder, which it is on most systems. You can check www-data's home folder with cat /etc/passwd | grep www-data)
    – Nick
    Feb 15, 2017 at 14:28
  • 1
    I was using Windows - of course, I needed to run the CMD prompt as an Administrator! Doing so, got round this problem. Mar 25, 2018 at 18:48
293

I know this question is on Linux, but on windows I had the same issue. Turns out you have to start the command prompt in "Run As Administrator" mode for it to work. Otherwise you get the same: unable to write 'random state' error.

6
  • 13
    I am running on windows as administrator but still get the error
    – Smalcat
    Jun 14, 2012 at 8:24
  • 6
    Being an administrator on the machine and using "Run As Administrator" are different. "Run As Administrator" forces the program to run as an Administrator, otherwise even when you are an administrator, the prompt will run with a non-administrator security clearance.
    – Beachhouse
    Aug 28, 2012 at 21:46
  • 76
    If you are running in administrator mode and you are still receiving "Unable to write 'random state'", another solution is to set RANDFILE=.rnd before executing openssl.
    – jevon
    Apr 16, 2013 at 4:08
  • 5
    In Powershell this is $env:RANDFILE=".rnd" rather than set RANDFILE=.rnd.
    – x5657
    Sep 17, 2018 at 9:51
  • What's the downside of NOT doing that?
    – StanTastic
    Feb 26, 2019 at 14:26
47

One other issue on the Windows platform, make sure you are running your command prompt as an Administrative User!

I don't know how many times this has bitten me...

1
  • 2
    as other have suggested setting this set RANDFILE=.rnd worked for me without doing cmd line with Administrator May 7, 2019 at 6:52
18

Apparently, I needed to run OpenSSL as root in order for it to have permission to the seeding file.

1
  • 16
    It is more likely that you once ran it as root whereupon the .rnd file in your home-directory was created with permissions set for root only. This happened to me a while back. Deletion of .rnd solved the issue.
    – fotNelton
    Sep 29, 2010 at 9:03
13

I had the same thing on windows server. Then I figured out by changing the vars.bat which is:

set HOME=C:\Program Files (x86)\OpenVPN\easy-rsa

then redo from beginning and everything should be fine.

2
  • that was it! thanks. i made this change right in-between the "init-config" and "vars" commands, from the instructions (here: openvpn.net/index.php/open-source/documentation/howto.html#pki). must be because i installed the 32-bit version (which i prefer).
    – symbiont
    May 13, 2014 at 2:15
  • 4
    That did the trick, and I did not have to run as admin. Thanks! In fact, I simply used set HOME=.
    – Synetech
    May 26, 2014 at 20:36
10

I have come across this problem today on AWS Lambda. I created an environment variable:

export RANDFILE = "/tmp/.random"

That did the trick.

1
  • I was struggling with this same issue in lambda and this fixed the problem. Thank you Feb 26, 2021 at 22:04
8

You should set the $RANDFILE environment variable and/or create $HOME/.rnd file. (OpenSSL FAQ). (Of course, you should have rights to that file. Others answers here are about that. But first you should have the file and a reference to it.)

Up to version 0.9.6 OpenSSL wrote the seeding file in the current directory in the file ".rnd". At version 0.9.6a you have no default seeding file. OpenSSL 0.9.6b and later will behave similarly to 0.9.6a, but will use a default of "C:\" for HOME on Windows systems if the environment variable has not been set.

If the default seeding file does not exist or is too short, the "PRNG not seeded" error message may occur.

The $RANDFILE environment variable and $HOME/.rnd are only used by the OpenSSL command line tools. Applications using the OpenSSL library provide their own configuration options to specify the entropy source, please check out the documentation coming the with application.

6

The problem for me was that I had .rnd in my home directory but it was owned by root. Deleting it and reissuing the openssl command fixed this.

1

For anyone who is unable to open the cmd with "run as admin" option. I had the same issue. Running set RANDFILE=.rnd in the cmd worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.