Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate a GPG encryption key. #2372

Closed
ndhambi opened this issue Aug 4, 2017 · 13 comments
Closed

Generate a GPG encryption key. #2372

ndhambi opened this issue Aug 4, 2017 · 13 comments

Comments

@ndhambi
Copy link

ndhambi commented Aug 4, 2017

I get a permission denied error when i generate a GPG encryption key using the command line. Below is a copy of what i did on the terminal and the error that i got.

===========================================================================

mapetla@mapetla:/var/www/MISP$ sudo -u www-data mkdir /var/www/MISP/.gnupg
mkdir: cannot create directory ‘/var/www/MISP/.gnupg’: File exists
mapetla@mapetla:/var/www/MISP$ sudo chmod 700 /var/www/MISP/.gnupg
mapetla@mapetla:/var/www/MISP$ sudo -u www-data gpg --homedir /var/www/MISP/.gnupg --gen-key
gpg (GnuPG) 2.1.15; Copyright (C) 2016 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Note: Use "gpg --full-gen-key" for a full featured key generation dialog.

GnuPG needs to construct a user ID to identify your key.

Real name: mapetla
Email address: mapetla.users@gmail.com
You selected this USER-ID:
"mapetla mapetla.users@gmail.com"

Change (N)ame, (E)mail, or (O)kay/(Q)uit? o
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.
gpg: agent_genkey failed: Permission denied
Key generation failed: Permission denied

@iglocska
Copy link
Member

iglocska commented Aug 4, 2017

No worries, just start the pgp generation from scratch:

rm -rf /var/www/MISP/.gnupg
sudo -u www-data mkdir /var/www/MISP/.gnupg
sudo chmod 700 /var/www/MISP/.gnupg
sudo -u www-data gpg --homedir /var/www/MISP/.gnupg --gen-key
sudo -u www-data sh -c "gpg --homedir /var/www/MISP/.gnupg --export --armor YOUR-KEYS-EMAIL-HERE > /var/www/MISP/app/webroot/gpg.asc"

@ndhambi
Copy link
Author

ndhambi commented Aug 4, 2017

I started the pgp from scratch as you have advised , and i still the the same error

mapetla@mapetla:/var/www/MISP$ sudo rm -rf /var/www/MISP/.gnup
mapetla@mapetla:/var/www/MISP$ sudo -u www-data mkdir /var/www/MISP/.gnupg
mapetla@mapetla:/var/www/MISP$ sudo chmod 700 /var/www/MISP/.gnupg
mapetla@mapetla:/var/www/MISP$ sudo -u www-data gpg --homedir /var/www/MISP/.gnupg --gen-key
gpg (GnuPG) 2.1.15; Copyright (C) 2016 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

gpg: keybox '/var/www/MISP/.gnupg/pubring.kbx' created
Note: Use "gpg --full-gen-key" for a full featured key generation dialog.

GnuPG needs to construct a user ID to identify your key.

Real name: mapetla
Email address: mapetla.users@gmail.com
You selected this USER-ID:
"mapetla mapetla.users@gmail.com"

Change (N)ame, (E)mail, or (O)kay/(Q)uit? o
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.
gpg: agent_genkey failed: Permission denied
Key generation failed: Permission denied

@SHSauler
Copy link
Contributor

SHSauler commented Aug 4, 2017

Maybe this here? But be careful with messing with your tty!

@ndhambi ndhambi closed this as completed Aug 18, 2017
@debernal
Copy link

Hello,

I am having the same problem, also tried the commands provided by iglocska and it did not work, is there any solution? Thanks.

@OopsIMadeStool
Copy link

OopsIMadeStool commented Oct 18, 2017

I was having this problem trying to re-issue the key on a working instance. Tried a fresh install, that install had the issue as well.

Edit: this was also corrected by temporarily changing tty ownership to www-data and doing a clean generation (I also installed rng-tools)

@eCrimeLabs
Copy link
Contributor

I solved this by using the guide and temporarly setting:
chown www-data $(tty)
and then after the generation moving it back

Checking with ls -l $(tty)

@pardhu247
Copy link

Change (N)ame, (E)mail, or (O)kay/(Q)uit? o
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.
gpg: agent_genkey failed: Permission denied
Key generation failed: Permission denied

i got the same problem...can some one help me

@Gumnos
Copy link

Gumnos commented Jan 9, 2019

While the issue is closed, I thought I'd add that the easiest way (and least-dangerous way, and doesn't require root privs to chown the tty) I've found to resolve this is to use a program like tmux or GNU screen which allocates a new ptty for the sued user:

gumnos@localhost$ su - demo
demo@localhost$ gpg --gen-key
⋮
Key generation failed: Permission denied
demo@localhost$ ls -l `tty`  # note still owned by "gumnos"
crw--w---- 1 gumnos tty 136, 13 Jan  9 11:37 /dev/pts/13
demo@localhost$ tmux
[tmux: demo@localhost]$ ls -l `tty` # now owned by "demo"
crw--w---- 1 demo tty 136, 22 Jan  9 11:38 /dev/pts/22
[tmux: demo@localhost]$ gpg --gen-key  # this works as desired

I forget this every time, do a web search for this information, and end up in this thread or one much like it that advises chowning the tty to the secondary user, generating the key, and then chowning the tty back to the original user (all that chowning also requires root privs). Hopefully by leaving this here, it's useful to others, including my future forgetful self.

@raphbaph
Copy link

Thanks a ton! This really helped.
One thing I'm uncertain of, and excuse me for posting on a closed subject, is that WakandaKing, the original poster, run gpg --gen-key as SU.
That's unnecessary and actually counterproductive, IMO, but please correct me here.
I want a GPG keyring for me, not for the SU (edge cases excluded).
So better to tmux, then run commands as me, not as SU.
So insted of sudo gpg --gen-key, just run gpg --gen-key

@chivakaa
Copy link

chivakaa commented Sep 7, 2020

While the issue is closed, I thought I'd add that the easiest way (and least-dangerous way, and doesn't require root privs to chown the tty) I've found to resolve this is to use a program like tmux or GNU screen which allocates a new ptty for the sued user:

gumnos@localhost$ su - demo
demo@localhost$ gpg --gen-key
⋮
Key generation failed: Permission denied
demo@localhost$ ls -l `tty`  # note still owned by "gumnos"
crw--w---- 1 gumnos tty 136, 13 Jan  9 11:37 /dev/pts/13
demo@localhost$ tmux
[tmux: demo@localhost]$ ls -l `tty` # now owned by "demo"
crw--w---- 1 demo tty 136, 22 Jan  9 11:38 /dev/pts/22
[tmux: demo@localhost]$ gpg --gen-key  # this works as desired

I forget this every time, do a web search for this information, and end up in this thread or one much like it that advises chowning the tty to the secondary user, generating the key, and then chowning the tty back to the original user (all that chowning also requires root privs). Hopefully by leaving this here, it's useful to others, including my future forgetful self.

Perfect solution Gumnos, it works!

@admcudo
Copy link

admcudo commented Oct 18, 2022

Just to add to the possible means of escape, if you log into the server using ssh as the user that needs to use gpg, the tty is automatically owned by that user and there is no need to chown the tty device.

If you log in as root and then use either sudo or su the tty is still owned by root and the above solutions will be needed.

@fourpastmidnight
Copy link

While the issue is closed, I thought I'd add that the easiest way (and least-dangerous way, and doesn't require root privs to chown the tty) I've found to resolve this is to use a program like tmux or GNU screen which allocates a new ptty for the sued user:

gumnos@localhost$ su - demo
demo@localhost$ gpg --gen-key
⋮
Key generation failed: Permission denied
demo@localhost$ ls -l `tty`  # note still owned by "gumnos"
crw--w---- 1 gumnos tty 136, 13 Jan  9 11:37 /dev/pts/13
demo@localhost$ tmux
[tmux: demo@localhost]$ ls -l `tty` # now owned by "demo"
crw--w---- 1 demo tty 136, 22 Jan  9 11:38 /dev/pts/22
[tmux: demo@localhost]$ gpg --gen-key  # this works as desired

I forget this every time, do a web search for this information, and end up in this thread or one much like it that advises chowning the tty to the secondary user, generating the key, and then chowning the tty back to the original user (all that chowning also requires root privs). Hopefully by leaving this here, it's useful to others, including my future forgetful self.

Thanks, this really helped, but not in the way that it was probably intended. I had su - <some-user> to switch to another user. Then tried to generate a GPG key for that user. And I got the error described by this issue. After reading this comment, I realized my mistake--the tty was owned by the user who sued to <other-user>. So the solution was to logout and login as <other user>. Of course, using tmux works, too, if you already have it installed! 😉

@Gumnos
Copy link

Gumnos commented Oct 30, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests