Open a terminal on your local machine (client machine, where you will connect from).
Generate an SSH key pair using the ssh-keygen command:
bash
复制代码
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-t rsa: Specifies the type of key to create. RSA is the default.
-b 4096: Specifies the number of bits in the key (4096 bits for better security).
-C "your_email@example.com": A comment to identify the key.
Press Enter to save the key in the default location (~/.ssh/id_rsa), or specify a different location.
Set a passphrase when prompted (optional but recommended for extra security).
Use ssh-copy-id to transfer your public key to the remote server:
ssh-copy-id username@remote_server_ip
Replace username with the username on the remote server and remote_server_ip with its IP address or hostname.
This command copies the contents of ~/.ssh/id_rsa.pub to the ~/.ssh/authorized_keys file on the remote server.
Alternatively, you can manually copy the public key:
Display your public key:
cat ~/.ssh/id_rsa.pub
Connect to the remote server:
ssh username@remote_server_ip
On the remote server, create the ~/.ssh directory if it doesn't exist:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
Paste the public key into the authorized_keys file:
echo "your_public_key" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys