ssh-keygen生成密钥对

时间:2022-03-10 14:26:42

ssh-keygen -t rsa -C www1707 -f filename -P 123456 

-t rsa 设置加密类型
-C www1707 设置标注
-f filename 定义生成的文件名称
-P 123456 用密码123456加密私钥

[root@centos66 ~]# ssh-keygen -t rsa -C www1707 -f filename -P 123456                          Generating public/private rsa key pair.
Your identification has been saved in filename.
Your public key has been saved in filename.pub.
The key fingerprint is:
82:4b:49:83:c9:6a:25:83:c8:e3:fa:63:b0:2e:38:31 www1707
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|+. o             |
|o=+.o            |
|..=. +           |
|.o  + . S        |
|E  . . .         |
|o=  .            |
|=.o              |
|o+..             |
+-----------------+
[root@centos66 ~]# ll
total 12
-rw-------. 1 root root  1743 Jul 29 23:34 filename
-rw-r--r--. 1 root root   389 Jul 29 23:34 filename.pub

查看私钥文件:

[root@centos66 ~]# cat filename-----BEGIN RSA PRIVATE KEY-----Proc-Type: 4,ENCRYPTEDDEK-Info: DES-EDE3-CBC,E59C93328E0C7EF9sluyNeFLBtMNcAW7gXlfaoLCQn8cVsSHCsMx05SFkoB136N0zL6Lnistwzr8+5Do53uUF7WMLMaHd8pwqJimKUyQ0JhxGHaSySdMgeErqbIsI3HuyEYjfoyLmbv/qtSEX2HuKNCgj4QgFIkQhU85j4fJYR7PxCxaEtr1iwRLUWp84aEhoLcev/jF8fYnaNN+QnmnXslDgLryGPuNKxD694lG1CK5XnDPzXPtfY6LgemHPCQThqXkDY4UGp0Zl0YaGixE7m7n2L6XpbXz3kJHBSnOHVAv+GEgBykVHXEvBEQagOHSaQvG+ivFtvIoBAklMLfhpSJuGUv1CA8wpnNT+rF7dIogLmMaoc/N2FaORsL4EeImaQhcxn6r9dWvuehVnJuCvQd6+XeSap4Vd/AgQ+BqlDwAPqWBuEkJZoisTCbybcqs4jb5ij8mkFU8RrqccZ1/Fwv+ycG8g8LSidfsus2L7KRVgpBSOCpMc2ietixnOEiFL0STeuldB4wcghbAnMVyWmzawIc6TP+53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PRIVATE KEY-----

查看公钥文件:

[root@centos66 ~]# cat filename.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsaD9gvSVHypRTCjlCw5U1l4M5Qs3N+fojWa6kEleWw+WGEp0NxdW3VdjO9zEhumC7c6yTtuMIBQe6tHnNR/aUFASFIKUAn4qZx8hkyySFg7cGAlJyoPKbOpc0iQqFHeJB36LLLSS/6mhSzu2M5Zz02kC5V7YY2kCaI2qmfByObDXfQk+TdflyIHoMREHUHyhGlZk+JbGkgCme5Dluwwg3Zy/CysHhQubyizsULwIjUv2p/Q+StxX37ZyMaH6+FpEBmDzCcPqzCeNdiKyX5UzzDFcfp1MFlVRdmOGj7cIptPsh+YeLjbOt29CYichl0RZ62Z1UsC4ls5KT/Gaedrl0w== www1707

将生成的公钥文件追加到/root/.ssh/authorized_keys文件中,添加信任关系

[root@centos66 ~]# cat /root/.ssh/authorized_keysssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsaD9gvSVHypRTCjlCw5U1l4M5Qs3N+fojWa6kEleWw+WGEp0NxdW3VdjO9zEhumC7c6yTtuMIBQe6tHnNR/aUFASFIKUAn4qZx8hkyySFg7cGAlJyoPKbOpc0iQqFHeJB36LLLSS/6mhSzu2M5Zz02kC5V7YY2kCaI2qmfByObDXfQk+TdflyIHoMREHUHyhGlZk+JbGkgCme5Dluwwg3Zy/CysHhQubyizsULwIjUv2p/Q+StxX37ZyMaH6+FpEBmDzCcPqzCeNdiKyX5UzzDFcfp1MFlVRdmOGj7cIptPsh+YeLjbOt29CYichl0RZ62Z1UsC4ls5KT/Gaedrl0w== www1707

最后将私钥文件 filename 拷贝到需要连接该linux的机器上使用即可,使用时需要输入私钥的加密密码 123456

#########################################################################################

创建密钥对,不加密私钥

[root@localhost .ssh]# ssh-keygen -t rsa -C www1707 -f www1707Generating public/private rsa key pair.Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in www1707.Your public key has been saved in www1707.pub.The key fingerprint is:48:bb:88:50:ce:5d:5f:d3:65:38:89:bc:e3:0f:38:7b www1707The key's randomart image is:+--[ RSA 2048]----+|         . . oo  ||          o.+o   ||  .   o   o...   || + . o + .o.     ||. o . o So .     || . . . .o o      ||  . . .  o o     ||        . E .    ||         .       |+-----------------+[root@localhost .ssh]# ll-rw-------. 1 root root 1675 Jul 30 15:43 www1707-rw-r--r--. 1 root root  389 Jul 30 15:43 www1707.pub[root@localhost .ssh]# cat www1707-----BEGIN RSA PRIVATE KEY-----MIIEoQIBAAKCAQEArbE76r8P1YBaNqlRu6159TlAUPa9ql+tl8dRFFfPIzGzhuOGDFPXbbZv5itz6FO+DSlPFoIsQGTeQQkDnMjDA5qUchkEnj5bINckgSKVcT/82t1r+F17BKjQVJGhxGcl5qvLJSggzyUtcdYEi4fE0pGPGI/taOsxUwNPGeBNQ7vngVbE0mi3xsJ7cMjXr/PbSaiTaG8+0pzGQUYSjWbU7Vx/2zG2dcE2it1/UexgYp/rBI1g9/ydCf/eyvGJl1Xi/IqhjkDwW6y59agtICSkNHfHg12QpMROq6YqvMTunfAEjiHXCx9xqUVj7/zL+NfDVahTeVb6WcHuQoeoPfC+RQIBIwKCAQA7jTkdSNI6kmgSvbWfbq14peoqYzm2wbfcRFZQHhsiAmlwE35qoGceTS2uAET/MqePe+CaAL62/gMO+8a5aWdu8yuU1WCrSJRFxhsk9eoYMzIfJ1g35YlDbRQ6QI88Bhufi1uXtfyeyusCdUNjCfpW0tH5yvJP25vh8oGFNvXrVc1AjWFHiR730dtSL+xzIf6PXsK8RDnjahR41qs9AeMnalJcHYqciPXQV3+zKlNcAAspQchVsCO0jmBerKSdvkbtp8fBm10xwG9f8UEgS95HcUfoi+xcg1a7Dc3ri05vGeWoSGYo8QXctvqR3Wgu/v0RERcj21hY5YWxGOydz/ULAoGBANzYhzONyrgv6LHwSQLGfv9/Zc5teErvfSBfIeO1i+Af+2liZLaniJgzovbDHcXYA4AcXcO/PvHGLXXeNW1+oCGeA6JrCB3sKu5hq3HR34XArTYAxw6RbEfTxidlghbKFOyFA4+19vrvrOKukOYVpAGemkjS+ew51LNw+kSsIEq9AoGBAMlXMzWJTg/utbR7dA3u/F/9bmJV2qBhKj+BshC7QB5FFaZBdtYwU9FiO1QZkydo8Z+v2mo+0UMf3c/Ymeah9XJ+sviD3lfzuzFm0mgFZ9YjatUcU7ta2PSLomq9lzMPz4+I9cgIaXr12CZtUMtaIV+anDHTHUxx+nTiWgbFhz4pAoGBAKpd5KQc7Nc65rUnE8BP+5Hl73NbwzJ+O/RmpR1gKhNL3zQKEy3RsoQKk6hqofBkz4AV4e7Gu4dPvKtiRnkJ6T6BNgDze0pIeOPHsCSaln0fm5APOnjz1zAYZalc71NosQ46uZq/kqRSfg30YSaFuQiQSyI8WmXGNl6KWqoBIDmnAoGAbUyYJGB61XL07PKPdUc4i+FZLhFZbQGL9pbdAcS8aDQhsgZH01wQPncnhWz4HLVP95n6Oa0Sg4ZT1zsRt7cBle0CEeEvjtTEtGsEgZyI01UOG+rOXmSEZ38AZdSiiW77pbC/8D8UsHbUbKG+JT+OdbpUypctKX+z2Q0pjqW+cjMCgYAl+Z99QRphxBcU/Q/rjqfJRinEHNacWfsr+DAQBxMp5nk6t/GlLSZJ9tscXQm6U/dtn26Ihm4Nn14R7KGat9DZpYuktUMIs+MpoGm8WdLmKCcW1I0jjDs0twGcGpHOW+mTQ2X69ECcIYTnYcVP2SRLVXQBXXZCeD+XxDp88O4mJA==-----END RSA PRIVATE KEY-----

给不带密码的私钥添加密码123456

[root@localhost .ssh]# ssh-keygen -p -N 123456 -f www1707Key has comment 'www1707'Your identification has been saved with the new passphrase.[root@localhost .ssh]# cat www1707-----BEGIN RSA PRIVATE KEY-----Proc-Type: 4,ENCRYPTEDDEK-Info: DES-EDE3-CBC,F1A9E5C1E0574BC2pK8VfEvBgwdfd/AdsJ6oQTLXvDwSxkx3fwkhyn6V9eOI3oHch0t5kMAq2mK5MlKMUbWLhxlHsvbWjCaF2imOsFa0iSpRKmLBOWHoLqWDz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5Rl8QDZrCQE4Y+YWIhe7INsJd5YS6KIJXUZibivfeE54UEfBqLh59FB/rzKC6+hbH6RbVzV7zdYEnQ1SnpX7TAb3xWVajXh1BwoUAF00SwksB9YE1/jvZ6p756IKiv6EUOtUTBcXH1nlpaeaQcn3wEKTFXKoBuGzNGusPUipe1PtfChBC3wQvDOETGYvzY1kxEZP0O60XlRc1C2xxqMTZIwklBLscqzJgiDrb4ayXlllyTCH+UxEsJvXSTpSQWAE5/hKRceEoJx3qX8R/r/eMb7mMUm0KKvnE6AvW56GF0uCKClaQGeP6pdtNv1IDt96L6TyZ1bsKGFlaWpOQWfU2CwbRbary2OMCDbEQeVf39qrdiLMaWct6L59FYP+g==-----END RSA PRIVATE KEY-----

给加密的私钥更换密码为654321

[root@localhost .ssh]# ssh-keygen -p -P 123456 -N 654321 -f www1707Key has comment 'www1707'Your identification has been saved with the new passphrase.[root@localhost .ssh]# cat www1707-----BEGIN RSA PRIVATE KEY-----Proc-Type: 4,ENCRYPTEDDEK-Info: DES-EDE3-CBC,45E3FD5100382745NEXlZn6Ss6XETYu9E5vDftex0fEbR1PDYIl0DBhR+PfdiTBZ+cNE4x9v3AWr3gFBnzjFtn9oPZkukSMbPBgELS2mHXiFOmndmDWvu6tqxZa2aPygm468ELrFGzHUK5QJn4ptwOTbxcjb8gvV2WtxFnG0qI+3B3/1HbyjWnnEyXl1wr3j3Nvlg5SFHANa+UBxbuHfsjLa9GG650/YzcRGmWrY6UJ/aH4eQfVDhwJ3yd+FT/VwXBWE1p73nfiCqnnYcDeXFFPwcxoeimYsI1hIsqcx/MJDir2MxG38LgPvjCXIwJSdNGVyRzUKsNJkNHi+/afJ4y1mQXZFa1Us0ZrhsN/GSEsg0B8gkyPW9xAmNhoS2OXbTt5G90AMX/SSxSq32ifAKXMXrH3pI+UHG720SZHto5w8Te2QThms0hZBIS9QkM824B78+cFSiFdHjau9xCbfd+x7KFDPmjXiIUT3EiqnORd3CvvqBNlhxQlH5nx+IFOZOq2d1qY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K9M2mQ==-----END RSA PRIVATE KEY-----



本文出自 “三角形” 博客,请务必保留此出处http://www1707.blog.51cto.com/1184226/1679845