{"id":5357,"date":"2024-11-11T17:13:05","date_gmt":"2024-11-11T20:13:05","guid":{"rendered":"https:\/\/bureau-it.com\/artigos\/how-to-generate-a-public-key\/"},"modified":"2024-11-11T20:20:48","modified_gmt":"2024-11-11T23:20:48","slug":"how-to-generate-a-public-key","status":"publish","type":"post","link":"https:\/\/bureau-it.com\/en\/artigos\/how-to-generate-a-public-key\/","title":{"rendered":"Complete Guide: How to Generate and Configure Public SSH Keys on macOS, Linux and Windows"},"content":{"rendered":"\n
The public key<\/strong> is a fundamental component in asymmetric cryptography, also known as public key cryptography. In this system, each user has a pair of keys: a public key, which can be shared freely, and a private key, which must be kept secret. The public key is used to encrypt messages or verify digital signatures, while the private key is used to decrypt messages or create digital signatures. <\/p>\n\n For example, when sending a confidential message, the sender uses the recipient’s public key to encrypt it. Only the recipient, with their corresponding private key, can decrypt and access the contents of the message. <\/p>\n\n Public key cryptography is widely used in various applications, such as digital certificates, internet security protocols (SSL\/TLS) and digital signatures, guaranteeing the security and integrity of electronically transmitted information. <\/p>\n\n Here at the Bureau of Technology we use keys mainly to access remote servers using the shell in SSH connections. Below are the instructions for generating a public SSH key on each operating system and then how to make it easier to use on a day-to-day basis to connect to remote servers via the shell: <\/p>\n\n Open the terminal and run the following command to generate a key pair:<\/p>\n\n Explanation of the parameters:<\/p>\n\n After the command:<\/p>\n\n Your public key will be saved as To view the generated public key, you can use:<\/p>\n\n In Windows 10 and higher, OpenSSH is built-in. To generate the SSH key, follow these steps: <\/p>\n\n To display the generated public key, run:<\/p>\n\n After generating the public key, copy its contents and paste them where necessary to configure SSH access:<\/p>\n\n Attention! <\/strong>The private key must be kept in a safe place, as any compromise of this key allows third parties to access protected systems without additional authentication. In contrast, the public key can be shared freely without negative consequences. <\/p>\n\n I recommend setting up the predefined connections file at The Follow the steps below to create and configure the SSH configuration file:<\/p>\n\n Explanation of the parameters<\/strong>:<\/p>\n\n After configuring the file, connect to the server using the defined alias:<\/p>\n\n SSH will automatically apply the settings specified to Suppose you manage two servers with different configurations. The file With this configuration, you can connect to the servers using only:<\/p>\n\n or<\/p>\n\n 6. Safety considerations<\/strong><\/p>\n\n By implementing the use of the Generating key pairs<\/h2>\n\n
MacOS and Linux<\/h3>\n\n
ssh-keygen -t ed25519 -C \"nome@seudominio.com\"<\/pre>\n\n
\n
-t ed25519<\/code>Specifies the type of key to be generated.<\/li>\n\n\n\n
-C \"nome@seudominio.com\"<\/code>: Adds a comment (usually your e-mail address) to the key.<\/li>\n<\/ul>\n\n
\n
~\/.ssh\/id_rsa<\/code>). <\/li>\n\n\n\n
~\/.ssh\/id_ed25519.pub<\/code>.<\/p>\n\n
cat ~\/.ssh\/id_ed25519.pub<\/pre>\n\n
Windows (with OpenSSH)<\/h3>\n\n
\n
ssh-keygen -t ed25519 -C \"nome@seudominio.com\"<\/pre>\n\n
\n
C:\\Users\\SeuUsuario\\.ssh\\id_ed25519<\/code>).<\/li>\n\n\n\n
type $HOME\\.ssh\\id_ed25519.pub<\/pre>\n\n
Public key sharing<\/h3>\n\n
\n
cat ~\/.ssh\/id_ed25519.pub<\/code><\/li>\n\n\n\n
type $HOME\\.ssh\\id_ed25519.pub<\/code><\/li>\n<\/ul>\n\n
Optimizing access to keys for everyday use<\/h2>\n\n
~\/.ssh\/config<\/code>. This file allows you to simplify and customize SSH connections, especially useful when managing multiple servers or specific configurations. <\/p>\n\n
What is
~\/.ssh\/config<\/code>?<\/strong><\/h3>\n\n
~\/.ssh\/config<\/code> file allows you to define specific settings for SSH hosts, making it easier to manage connections. With it, you can assign nicknames to servers, specify ports, default users and other connection options. <\/p>\n\n
Benefits of using the SSH configuration file<\/strong><\/h3>\n\n
\n
How to configure the file
~\/.ssh\/config<\/code><\/strong><\/h3>\n\n
\n
touch ~\/.ssh\/config<\/pre>\n\n
\n
chmod 600 ~\/.ssh\/config<\/pre>\n\n
\n
Host meu-servidor\n HostName 192.168.1.100\n User usuario\n Port 2222\n IdentitiesOnly=yes\n IdentityFile ~\/.ssh\/id_ed25519<\/pre>\n\n
\n
Host<\/code>: Nickname for the server.<\/li>\n\n\n\n
HostName<\/code>: IP address or domain of the server.<\/li>\n\n\n\n
User<\/code>: User name for the connection.<\/li>\n\n\n\n
Port<\/code>SSH port used by the server.<\/li>\n\n\n\n
IdentitiesOnly<\/code>=yes: exclusively uses the key specified in
IdentityFile<\/code><\/li>\n\n\n\n
IdentityFile<\/code>Path to the corresponding private key.<\/li>\n<\/ul>\n\n
Using the configuration file<\/strong><\/h3>\n\n
ssh meu-servidor<\/pre>\n\n
meu-servidor<\/code>.<\/p>\n\n
Practical example<\/strong><\/h3>\n\n
~\/.ssh\/config<\/code> could be configured as follows: <\/p>\n\n
Host servidor-web\n HostName web.bureau-it.com\n User admin\n Port 22\n IdentitiesOnly=yes\n IdentityFile ~\/.ssh\/id_ed25519_web\n\nHost servidor-banco\n HostName db.bureau-it.com\n User dbadmin\n Port 2222\n IdentitiesOnly=yes\n IdentityFile ~\/.ssh\/id_ed25519_banco<\/pre>\n\n
ssh servidor-web<\/pre>\n\n
ssh servidor-banco<\/pre>\n\n
\n
~\/.ssh\/config<\/code> with restricted permissions to prevent unauthorized access.<\/li>\n\n\n\n
~\/.ssh\/config<\/code> file, you simplify the management of SSH connections and improve efficiency when accessing multiple servers.<\/p>\n\n