54

When I create an ssh key with ssh-keygen, it includes the username and hostname of the machine it was created on. Is there any way to avoid this? I read the manpage and didn't see anything.

4 Answers 4

67

The user name is a comment, you can delete it or set it with the -C option

I do not see a host name anywhere in the keys, what file are you looking at ?

ssh-keygen -f test -C noname

Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in test.
Your public key has been saved in test.pub.
The key fingerprint is:
a8:fa:78:ea:bb:c6:70:67:4c:79:ac:0d:a7:78:5a:40 noname
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|  E              |
| .   o           |
|  . + +.         |
|   = B. S        |
|. o O..          |
| + *.            |
|  +o.            |
| oB*.            |
+-----------------+

cat test
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,6BD3B77199F57BCD0BBC4BD4D717931C  

+hko6XxmbmqLlV32dFLTuS6vYSh5OGOBR9Q29OVewRwS6XN2+jbuCBviOdN/CnLh
k/Dzpjt9bPinFLI73RsfYMGUZ+kiBKIotYBqjJrkfMLNkZV0xvzk//tIEQatKyYI
W3OczGVt4EXfXozTvAQSpbEA5pYS5nLCbWSwx98Ilsqej+bnHJDflAhY5kFnrvOm
N66ODWa8FXpmJmJnIU4E8X+w33NO0nxXEiK8EUoRKFHnopwE+uQVkC6z3gPzqCd0
gCJrCDZ7zBaz947jlebH91liygrj1wTnuUbJOk0VnHyHMO1nO0piPOmjPZbL/C4Q
R8Z7C2Hm4cCaTRYbzZCcLvXCdlisCnGmM2a6dIGU5JECYuCB14k0npYqlY0dSJfb
XJx07z0wlkBWzOOUrWkc4U21+BBmYZe8DgHX/p6TNPxkugumIEUgu+xSL8RhwM6Y
lF4miW1s30g4/BpzN5GHTrpJGyyDBLojGR+P42LmsfiuCojU0NFbmrNlX23dSGj0
3wTSwuw179P6FDAEhJo8Znly8W+XP1pKeRn/ozdXW3WJWICDFZzVIe6VSlRES95Q
tcSeWDQRKhZWsGR+yriRwQSYROVje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RwCZFPYUMNdzL7zCRvijpd4PaEbK4/zcXb6ns/9Eg2URJEVIDEBbKFrNnCQU48VNjiGG4yDyrn7COHLzWKgvLjraYV8gD0MnzqE52U5L85iuuhXcgVhc0i4Jqu3k4XoUNHGqtYMZstBhS39MCD2h8bkfKuz4eycWlyfKpktd8yIJjeUsV39mw7U7/qh/508RGuwriYe8AYNqgivnL0PmZaDwZGTCnEFaP0IPLbqKyFTcmSuVChmTYttE1Kcj4DNI57APaq2EzSGUNrBVGSJ7Q6KbXC4xiSF+RCDy8ruOk7hj2x2MGOwBZdvhy55kmMkhUMCC7BUcSiRA9F noname

No username or hostname in either file, test or test.pub

5
  • The comment was being set to my username@host. I didn't realize that that was the "comment". Thanks! Nov 16, 2014 at 6:16
  • OK, ~/.ssh/known_hosts does track hostname and ip (of the server) as it should, which is why I asked
    – Panther
    Nov 16, 2014 at 7:06
  • 7
    I would prefer to use ssh-keygen -f amp_generic.pem -C "" so that I do not have any name in the SSH public file
    – vikas027
    Apr 26, 2017 at 12:35
  • I just deleted my email at the end of test.pub, will it work?
    – Cirelli94
    Jan 30, 2020 at 11:52
  • 1
    @Cirelli94 Yes you can just manually delete the hostname at the bottom and it will work fine Mar 18, 2020 at 17:04
5

Apparently you can just delete the username@hostname at the end. I just tested it by removing that bit from one of the public keys in one of my server's authroized_keys files and it didn't seem to affect my ability to access that server with that key.

before:

ssh-rsa AAAAB... ...Kzwaw== myUser@somehost

after:

ssh-rsa AAAAB... ...Kzwaw==
1
  • 3
    Note that the comment (username@hostname) still remains in the private key and will appear in, for example, ssh-agent key listing. To remove the comment from both private and public keys you need to use ssh-keygen -c -C "" -f <private key>
    – IanB
    May 3, 2023 at 23:59
0

Upon investigation, it appears that the user@hostname is just a comment. In trying to delete and import the key, it shows as invalid. BUT, editing the id_rsa.pub and replacing user@hostname with noname worked. This way you keep your existing pub key and no longer broadcast user/hostname to those that may inspect your key.

0

Additionally, if you wanted a "different comment" instead of "no comment" (what I searched for to find this post)...

The user@hostname is the default comment. You can also use -C to make that comment into anything you like.

For my purposes, I gave it the same name as the file, so I more easily recognize it in the API areas of sites I upload it to or so I know which key is referred to on remote servers with many keys in the file ~/.ssh/authorized_keys.

I use...

ssh-keygen -t rsa -N "" -f ~/.ssh/My_key_nickname -C My_key_nickname

Then, ~/.ssh/My_key_nickname.pub has = My_key_nickname at the end of the key line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .