howto:sshkey
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| howto:sshkey [2025/07/08 23:26] – [Password authentication is disabled on the remote host] jnilsson | howto:sshkey [2026/05/12 19:31] (current) – [Note about encrypting your keys] jnilsson | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Setting up Passwordless | + | ====== Setting up SSH Public Key Authentication |
| - | ===== Generating keys ===== | + | |
| - | === ssh-keygen === | + | SSH public key authentication lets you log in to a remote Linux server without needing to type a password. Instead SSH uses a key pair: |
| - | You use the tool " | + | |
| - | < | + | * **Private key**: '' |
| - | [user@host ~]$ ssh-keygen -t ed25519 | + | * **Public key**: '' |
| + | |||
| + | When you try to connect, your private key is used to authenticate you, and if your public key is set up correctly on the server, you are allowed to log in. | ||
| + | |||
| + | You can read more about SSH public key authentication here: | ||
| + | |||
| + | * [[https:// | ||
| + | |||
| + | ===== Important notes ===== | ||
| + | |||
| + | * Never share your private key. | ||
| + | * It is safe to share your public key. | ||
| + | * Your public key file usually ends in '' | ||
| + | * Confirm that SSH key login works before password login is disabled. | ||
| + | * If you need help, contact SocIT at [[socit@uci.edu]]. | ||
| + | |||
| + | ===== Step 1: Check Whether You Already Have an SSH Key ===== | ||
| + | |||
| + | If you already have an SSH key pair, you can usually reuse it ([[# | ||
| + | |||
| + | Use the code below to see if you already have files such as '' | ||
| + | |||
| + | ==== Windows Users ==== | ||
| + | |||
| + | Open **PowerShell** or **Windows Terminal** and run:<code powershell> | ||
| + | dir $env: | ||
| + | </ | ||
| + | |||
| + | ==== macOS and Linux Users ==== | ||
| + | |||
| + | Open **Terminal** and run:< | ||
| + | ls ~/.ssh | ||
| + | </ | ||
| + | |||
| + | ===== Step 2: Generate an SSH Key ===== | ||
| + | |||
| + | If you do not already have an SSH key, create one using '' | ||
| + | |||
| + | ==== Windows Users ==== | ||
| + | |||
| + | Open **PowerShell** or **Windows Terminal**. Run:< | ||
| + | ssh-keygen -t ed25519 | ||
| + | </ | ||
| + | |||
| + | You will see prompts similar | ||
| + | |||
| + | < | ||
| Generating public/ | Generating public/ | ||
| - | Enter file in which to save the key (/home/user/ | + | Enter file in which to save the key (C: |
| - | Enter passphrase (empty for no passphrase): | + | Enter passphrase (empty for no passphrase): |
| - | Enter same passphrase: | + | Enter same passphrase: |
| </ | </ | ||
| - | You should leave the default values above by simply hitting Enter three times. | ||
| - | ===== Distributing your public key ===== | + | The default |
| - | The *.pub file needs to be appended to the ~/ | + | |
| - | * owner: the user whose home directory it resides in | + | |
| - | * permissions: 0600, writable by the owner and no one else | + | |
| - | There are different ways to distribute | + | < |
| + | #private key | ||
| + | C: | ||
| + | #public key | ||
| + | C: | ||
| + | </ | ||
| + | |||
| + | ==== macOS and Linux Users ==== | ||
| + | |||
| + | Open **Terminal**. Run:< | ||
| + | ssh-keygen -t ed25519 | ||
| + | </ | ||
| + | |||
| + | You will see prompts similar | ||
| + | |||
| + | < | ||
| + | Generating public/ | ||
| + | Enter file in which to save the key (/ | ||
| + | Enter passphrase (empty for no passphrase): | ||
| + | Enter same passphrase: | ||
| + | </ | ||
| + | |||
| + | The default key files are: | ||
| + | |||
| + | < | ||
| + | #private key | ||
| + | ~/ | ||
| + | #public key | ||
| + | ~/ | ||
| + | </ | ||
| + | |||
| + | ==== Note about encrypting your keys ==== | ||
| + | |||
| + | If you typed a password above during key generation, then your private key is encrypted and you will be prompted for a password every time you try to use the key. This is an optional security feature, but not something covered by this guide. | ||
| + | |||
| + | ===== Step 3: Copy Your Public Key to the Server ===== | ||
| + | |||
| + | Your public key must be added to this file on the remote Linux server: | ||
| + | |||
| + | < | ||
| + | ~/.ssh/authorized_keys | ||
| + | </ | ||
| + | |||
| + | Use a method below to distribute your public key to the server: | ||
| + | |||
| + | ===== Option A: Ask SocIT for Help ===== | ||
| + | |||
| + | If you would like help, contact SocIT at [[socit@uci.edu]]. Please include: | ||
| + | |||
| + | * The server name you want to connect to | ||
| + | * Your username on that server | ||
| + | * The contents of your public key file. You can paste the contents of '' | ||
| + | |||
| + | * NOTE: Never share your private key | ||
| + | |||
| + | ===== Option B: Use ssh-copy-id ===== | ||
| + | |||
| + | Use this method if password login is currently enabled | ||
| + | |||
| + | The '' | ||
| + | |||
| + | <code bash> | ||
| + | ssh-copy-id username@remotehost | ||
| + | </ | ||
| + | |||
| + | Replace: | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | You will be prompted for your password on the remote server. If the command is not found, use [[# | ||
| + | |||
| + | ===== Option C: Use Local Console Access ===== | ||
| + | |||
| + | Use this method if you can physically access the remote server console or otherwise log in locally. | ||
| - | ==== Local Console Access ==== | + | - Copy the contents |
| - | If you have access to the console | + | ssh-ed25519 |
| - | - Generate your SSH key on your computer that you will use to access the remote host using ssh-keygen as described above | + | |
| - | - Save the .pub key file either in email to yourself or on a USB drive | + | |
| - | - On the local console of the remote host, login as your user account: | + | |
| - | - Copy the contents of the new public key (either from email or a USB drive) to a temp file:< | + | |
| - | TMP_NEWKEY_FILE=/ | + | |
| - | echo "ssh-ed25519 | + | |
| </ | </ | ||
| - | | + | |
| mkdir -p ~/.ssh | mkdir -p ~/.ssh | ||
| touch ~/ | touch ~/ | ||
| Line 34: | Line 144: | ||
| chmod 600 ~/ | chmod 600 ~/ | ||
| </ | </ | ||
| - | | + | |
| - | cat $TMP_NEWKEY_FILE | + | echo " |
| - | </ | + | |
| - | - Delete the temp file:< | + | |
| - | rm $TMP_NEWKEY_FILE | + | |
| </ | </ | ||
| - | ==== Password authentication is enabled on the remote host ==== | + | ===== Step 4: Test Your SSH Login ===== |
| - | If you can log in to the remote host using a password, then you can use the command **ssh-copy-id** to set up your public key. Here is an example session showing how ssh-copy-id works: | + | |
| - | < | + | |
| - | [user@localhost ~]$ ssh-copy-id user@remotehost | + | |
| - | / | + | |
| - | / | + | |
| - | / | + | |
| - | user@remotehosts' | + | |
| - | Number of key(s) added: | + | After your public |
| - | Now try logging into the machine, with: "ssh 'user@remotehost'" | + | <code bash> |
| - | and check to make sure that only the key(s) you wanted were added. | + | ssh username@remotehost |
| + | </ | ||
| - | </ | + | If you entered a password when generating your key, then you will be prompted for this password in order to decrypt your private key. This is different from your user account password. |
| - | If ssh-copy-id does not work or is not available, then you can manually accomplish the same steps like this: | + | ===== Administrator Instructions ===== |
| - | < | + | |
| - | # Your pub key's filename may differ depending on whether it was ed25519 (id_ed25519.pub), | + | |
| - | [user@localhost ~]$ scp ~/ | + | |
| - | Enter password for user@remotehost: | + | |
| - | [user@localhost ~]$ ssh user@remotehost | + | |
| - | Enter Password | + | |
| - | [user@remotehost ~]$ cd .ssh | + | |
| - | [user@remotehost .ssh]$ cat my_id.pub >> authorized_keys | + | |
| - | [user@remotehost .ssh]$ chmod 600 authorized_keys | + | |
| - | [user@remotehost .ssh]$ rm my_id.pub | + | |
| - | </ | + | |
| - | ==== Password authentication is disabled on the remote host ==== | + | |
| - | If password authentication is already disabled, then you will have to give your public key file to someone | + | Use this section if you are an administrator adding |
| - | If you have admin access and have been asked to add someone else's public key to their user account, the following example code should guide you on the steps to take. This assumes that they have somehow given you their public key and it is stored in a file called / | ||
| <code bash> | <code bash> | ||
| - | USERNAME=example | + | USERNAME=panteater |
| - | TMP_PUBKEY_FILE="/ | + | TMP_PUBKEY_FILE="/ |
| - | USER_HOME=$(eval echo " | + | |
| - | AUTHKEY_FILE=" | + | |
| - | # Ensure | + | USER_HOME=/ |
| - | sudo mkdir -p "$USER_HOME/ | + | SSH_DIR=" |
| + | AUTHKEY_FILE=" | ||
| + | |||
| + | # Create | ||
| + | sudo mkdir -p "$SSH_DIR" | ||
| sudo touch " | sudo touch " | ||
| - | # Append | + | # Add the key only if it is not already present |
| if ! sudo grep -Fxq -f " | if ! sudo grep -Fxq -f " | ||
| cat " | cat " | ||
| fi | fi | ||
| - | # Set permissions | + | # Set correct |
| - | sudo chmod 700 "$USER_HOME/ | + | sudo chmod 700 "$SSH_DIR" |
| sudo chmod 600 " | sudo chmod 600 " | ||
| - | sudo chown -R " | + | sudo chown -R " |
| - | # Clean up temp file | + | # Remove temporary public key file |
| rm -f " | rm -f " | ||
| </ | </ | ||
| + | |||
| + | ===== Troubleshooting ===== | ||
| + | |||
| + | For more detailed troubleshooting output, use: | ||
| + | |||
| + | <code bash> | ||
| + | ssh -vv username@remotehost | ||
| + | </ | ||
| + | |||
| + | This can help identify any issues when logging in. | ||
| + | |||
| + | ===== Security Reminders ===== | ||
| + | |||
| + | * Do not share your private key. | ||
| + | * Only share your public key, the file ending in '' | ||
| + | * Use a passphrase on your private key if you want extra protection. | ||
| + | * Confirm SSH key login works before disabling password authentication. | ||
| + | * If password authentication will be disabled on a server, make sure all remote users have working SSH keys first. | ||
howto/sshkey.1752017188.txt.gz · Last modified: 2025/07/08 23:26 by jnilsson
