65

Trying to generate a key for a server.

gpg --gen-key

We need to generate a lot of random bytes. It is a good idea to perform some other action (type on the keyboard, move the mouse, utilize the disks) during the prime generation; this gives the random number generator a better chance to gain enough entropy.

and it just hangs there.

There is another error:

can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory

which seems to go away after:

gpg-agent --daemon
GPG_AGENT_INFO=/tmp/gpg-4c5hyT/S.gpg-agent:1397:1; export GPG_AGENT_INFO;

#GPG_AGENT_INFO=/tmp/gpg-4c5hyT/S.gpg-agent:1397:1; export GPG_AGENT_INFO;
gpg --gen-key
...

but again, it hangs at "...gain enough entropy".

There are no "++++++++++++++++++++++++++++++++++++++++++"'s which from forum posts looks like should be expected as the key is generated.

I have tried reinstalling the package, but seemingly everything depends on gpg.

I've read other people having problems with this on centos 6 too (whereas centos 5 works fine).

There is nothing remarkable in /var/log/*.

Any ideas on where to go from here?

Thanks.

stormdrain
  • 1,377
  • 7
  • 28
  • 51
  • rng-tools is only a solution if you have an HSM, answers which recommend this will fail on systems without this. You will see a message like: Starting Hardware RNG entropy gatherer daemon: (Hardware RNG device inode not found) – JohnErinthen Oct 14 '17 at 11:01

9 Answers9

66

When the gpg --gen-key command hangs like this, log in to another shell and perform the following command:

dd if=/dev/sda of=/dev/zero

(This command basically reads from your hard drive and discards the output, because writing to /dev/zero will do nothing.)

After a few seconds / minutes, the key generation command should complete.

James
  • 154
  • 1
  • 8
John
  • 8,920
  • 1
  • 28
  • 34
  • 2
    Awesome. Thank you. I can't believe I missed that part of the manual :/ – stormdrain Jan 22 '13 at 15:59
  • 2
    It would be a much better idea to grab different entropy each time. If you're system is constantly running out of entropy then something is terribly wrong with your configuration or you're using up entropy very quickly (to the point where you should have a hardware RNG). If you need more entropy on a regular basis there are valid places to simply download more, like [Humboldt-Universität's Quantum RNG](http://qrng.physik.hu-berlin.de/). – Chris S Jan 23 '13 at 16:38
  • @ChrisS Thanks. I only needed to generate the key once for use with an authentication system; it won't be an ongoing thing. Should it become more frequent an issue, I will use the HSM that the company sells (which I planned to get anyway). This was just confusing because there was no indication at all that the process was still running. Even `ps` seemed to indicate it was just sitting there... – stormdrain Jan 23 '13 at 17:20
  • 29
    I actually tried this, but since I didn't have root I couldn't access /dev/sda directly. What worked for me instead was `find / | xargs file` – carl.anderson Mar 15 '16 at 22:09
  • 3
    I was more comfortable running `find / | xargs file` instead of `dd if=/dev/sda of=/dev/zero` and after a minute it was done. Thanks! – Lea Oct 16 '16 at 07:10
  • 1
    What does `find / | xargs file` do? I don't want to just blindly type something in my terminal lol. As well as the other command: `dd if=/dev/sda of=/dev/zero` – StackOverflowed Nov 11 '16 at 16:57
  • @StackOverflowed `dd` is akin to copy/paste. `find` does what it says: it finds things. Both of the commands you reference are completely safe to run. To learn more about the commands, in linux, you can always type `man ` e.g. `man find` and it will bring up the manual entry for the given command. – stormdrain Dec 13 '16 at 14:16
  • 4
    Do you mean `of=/dev/null`? – maxschlepzig Feb 14 '17 at 16:51
  • I tried `cd / && find . -type f` in a separate console, but no joy. – RonJohn Oct 15 '19 at 14:43
  • my server just hanged irrecoverably after this command :( – Nic Wanavit Dec 16 '19 at 13:40
  • I use something like `sudo dd if=/dev/sda of=/tmp/foo bs=1M count=100` (a few times if necessary. This writes to disk, which gpg suggests. I use `count=100` because dd can be impossible to interrupt. – Moondoggy Jan 21 '20 at 04:24
  • 1
    @StackOverflowed Underrated comment -- IMO better to understand a command like this completely before running it. And the response didn't explain it. – 6005 May 26 '22 at 20:59
  • What it's doing is finding all files on your system, then the `xargs` passes the file (as a path/string) to the `file` command. The `file` command simply prints information, e.g. `my_dir: directory`, `README.md: ASCII text`. There are some weird cases, for example files whose names start with `--` being interpreted as arguments, but as far as I can tell so far I don't think anything "bad" happens. – 6005 May 26 '22 at 21:01
26

For a more reliable solution you could install random number generator related utilities, which will make sure that you always have enough random bytes.

yum install rng-tools

and then edit /etc/sysconfig/rngd and add EXTRAOPTIONS="-r /dev/random"

Start the service

 service rngd start

Voila and you live happily ever after :)

golja
  • 1,611
  • 10
  • 14
  • 7
    If you don't want to start the service, you can simply run ``rngd -r /dev/random`` as root once ``rng-tools`` is installed. Your key generation will take off immediately. – davidjb May 07 '15 at 12:23
  • 2
    But that itself doesn't generate entropy. – Otheus Sep 19 '16 at 14:18
12

https://gist.github.com/franciscocpg/1575d286548034113884c3185ca88681

Open a ssh session sudo apt-get install rng-tools In another SSH window open gpg --gen--key Go back to your first SSH session and run sudo rngd -r /dev/urandom Let this run till gpg generates your keys!

Then you can kill rngd sudo kill -9 $(pidof rngd)

rhinoceros.xn
  • 251
  • 2
  • 4
11

Both comments given before are perfectly fine. But here is just my 2 cents.

The problem with RHEL/centos 6 and entropy is that they are tickless kernels. So, by themselves, these kernels don't generate enough entropy. You have to get some keyboard attached or even some mouse movement or use dd as mentioned.

rngd daemon is awesome and most commercial entities use it.

However, the best approach I have seen is use of dedicated TPM device. They are small hardware which are quite expensive. You put them and rngd utilizes random true entropy from the hardware source. As far as I know, Fujitsu has some good TPM device.

Yeah, these three methods pretty much cover the entropy part.

Soham Chakraborty
  • 3,534
  • 16
  • 24
  • Very interesting. Thank you. As I mentioned to Chris, I will have access to an HSM soon which comes with an RNG. – stormdrain Jan 23 '13 at 17:22
7

Twist on other responses but at least one liner and not root.

((find / | xargs file) &> /dev/null &); gpg2 --gen-key --batch --passphrase-file output-key.txt key-gen-options.txt

Key-gen-options contains

Key-Type: 1
Key-Length: 2048
Subkey-Type: 1
Subkey-Length: 2048
Name-Real: myuser
Name-Email: myuser@email.com
Expire-Date: 0

Output-key.txt contains my super secret key.

Dave Brunkow
  • 222
  • 5
  • 11
4

EXTRAOPTIONS="-r /dev/urandom" worked for me instead of EXTRAOPTIONS="-r /dev/random"

4

I've tried all the solutions, and found that haveged works the best even when others don't work (especially on a headless server that doesn't have much user input or activity).

yum install haveged

apt install haveged

It starts the haveged daemon service that will keep /dev/random full of entropy. --key-gen should complete in less than a minute.

You can verify by running cat /dev/random. Normally, it will quickly run out of entropy and pause. That's why the --key-gen hangs. But after installing haveged, cat /dev/random should provide output continuously.

wisbucky
  • 969
  • 9
  • 9
2

How I did it:

  1. pacman -S community/rng-tools
  2. vim /etc/conf.d/rngd to add RNGD_OPTS="-r /dev/urandom"
  3. systemctl enable --now rngd
  4. gpg-agent --daemon
  5. gpg --full-gen-key

Worked even when $GNUPGHOME is set to point to a custom directory.

0

Switching to gpg2 worked for me.

None of the other solutions did, because of permissions issues.