- SSH Command — Usage, Options, Configuration
- Contents
- SSH Command in Linux
- Other SSH Commands
- Using the Linux client
- Specifying a different user name
- Executing remote commands on the server
- SSH client configuration file
- Configuring public key authentication
- Configuring port forwarding
- SSH command line options
- A little history
- Linux / UNIX: Generate SSH Keys
- ssh-keygen command to Generate SSH Keys
- SSH Keys Are Generated, What Next?
- Optional ssh-keygen command syntax for advance users
- Conclusion
- How To Set up SSH Keys on a Linux / Unix System
- What is a public key authentication?
- How to set up SSH keys
- How do I set up public key authentication?
- 1: Create the key pair
- Optional syntax for advance users
- Creating an SSH Key Pair and Configuring Public Key Authentication on a Server
- How SSH Public Keys Work
- How Does SSH Public Key Authentication Work?
- The authorized_keys File
- Granting Someone Else Access to Your Server
- Challenge-Response
- SSH Private Key Passphrases
- Is it Safe to Share Public SSH Key?
- How Secure is SSH Key Authentication?
- Public Key Authentication on Linux And macOS
- Generate an SSH Key Pair on Linux And macOS
- Upload Your Public Key
- Using ssh-copy-id
- Using Secure Copy (scp)
- Manually Copy Your Public Key
- Connect to the Remote Server
- Public Key Authentication on Windows
- Generate a Key Pair with PuTTY
- Manually Copy the SSH Key with PuTTY
- Manually Copy the SSH Key with WinSCP
- Connect to the Remote Server with PuTTY
- Upload Your SSH Key to Linode Cloud Manager
SSH Command — Usage, Options, Configuration
Practically every Unix and Linux system includes the ssh command. This command is used to start the SSH client program that enables secure connection to the SSH server on a remote machine. The ssh command is used from logging into the remote machine, transferring files between the two machines, and for executing commands on the remote machine.
Contents
SSH Command in Linux
The ssh command provides a secure encrypted connection between two hosts over an insecure network. This connection can also be used for terminal access, file transfers, and for tunneling other applications. Graphical X11 applications can also be run securely over SSH from a remote location.
Other SSH Commands
There are other SSH commands besides the client ssh . Each has its own page.
ssh-keygen — creates a key pair for public key authentication
ssh-copy-id — configures a public key as authorized on a server
ssh-agent — agent to hold private key for single sign-on
ssh-add — tool to add a key to the agent
scp — file transfer client with RCP-like command interface
sftp — file transfer client with FTP-like command interface
Using the Linux client
Linux typically uses the OpenSSH client. The ssh command to log into a remote machine is very simple. To log in to a remote computer called sample.ssh.com, type the following command at a shell prompt:
If this is the first time you use ssh to connect to this remote machine, you will see a message like:
Type yes to continue. This will add the server to your list of known hosts (
/.ssh/known_hosts ) as seen in the following message:
Each server has a host key , and the above question related to verifying and saving the host key, so that next time you connect to the server, it can verify that it actually is the same server.
Once the server connection has been established, the user is authenticated. Typically, it asks for a password. For some servers, you may be required to type in a one-time password generated by a special hardware token.
Once authentication has been accepted, you will be at the shell prompt for the remote machine.
Specifying a different user name
It is also possible to use a different username at the remote machine by entering the command as:
The above can also be expressed with the syntax:
Executing remote commands on the server
The ssh command is often also used to remotely execute commands on the remote machine without logging in to a shell prompt. The syntax for this is:
For example, to execute the command:
on host sample.ssh.com, type the following command at a shell prompt:
After authenticating to the remote server, the contents of the remote directory will be displayed, and you will return to your local shell prompt. -x Disables X11 forwarding.
SSH client configuration file
The ssh command reads its configuration from the SSH client configuration file
/.ssh/config . For more information, see the page on SSH client configuration file .
Configuring public key authentication
To configure passwordless public key authentication , you may want to create an SSH key and set up an authorized_keys file. See the pages on ssh-keygen and ssh-copy-id for more information.
Configuring port forwarding
Command-line options can be used to set up port forwarding. Local fowarding means that a local port (at the client computer) is tunneled to an IP address and port from the server. Remote forwarding means that a remote port (at the server computer) is forwarded to a given IP address and port from the client machine. See the page on configuring port forwarding on how to configure them.
OpenSSH also supports forwarding Unix domain sockets and IP packets from a tunnel device to establish a VPN (Virtual Private Network).
SSH command line options
Some of the most important command-line options for the OpenSSH client are:
-1 Use protocol version 1 only.
-2 Use protocol version 2 only.
-4 Use IPv4 addresses only.
-6 Use IPv6 addresses only.
-A Enable forwarding of the authentication agent connection.
-a Disable forwarding of the authentication agent connection.
-C Use data compression
-c cipher_spec Selects the cipher specification for encrypting the session.
-D [bind_address:] port Dynamic application-level port forwarding. This allocates a socket to listen to port on the local side. When a connection is made to this port, the connection is forwarded over the secure channel, and the application protocol is then used to determine where to connect to from the remote machine.
-E log_file Append debug logs to log_file instead of standard error.
-F configfile Specifies a per-user configuration file. The default for the per-user configuration file is
-g Allows remote hosts to connect to local forwarded ports.
-i identity_file A file from which the identity key (private key) for public key authentication is read.
-J [user@] host [:port] Connect to the target host by first making a ssh connection to the pjump host[(/iam/jump-host) and then establishing a TCP forwarding to the ultimate destination from there.
-l login_name Specifies the user to log in as on the remote machine.
-p port Port to connect to on the remote host.
-V Display the version number.
-X Enables X11 forwarding.
A little history
SSH replaced several older commands and protocols in Unix and Linux the 1990s. The include telnet , rlogin , and rsh .
SSH runs at TCP/IP port 22. This is right between ftp and telnet, which are 20 years older. Read the story of how SSH got port 22 .
The following video summarizes how and why SSH was originally developed.
Together with our customers, our mission is to secure their digital business on on-premises, cloud, and hybrid ecosystems cost-efficiently, at scale, and without disruptions to their operations or business continuity.
Источник
Linux / UNIX: Generate SSH Keys
H ow do I generate ssh keys under Linux / UNIX / Mac OS X and *BSD operating systems for remote login?
SSH uses public-key cryptography to authenticate the remote computer and allow the remote computer to authenticate the user, if required. You can create ssh keys as follows on any Linux or UNIX-like operating systems including Mac OS X.[donotprint]
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | 5m |
[/donotprint]
ssh-keygen command to Generate SSH Keys
The ssh-keygen command generates, manages and converts authentication keys for ssh client and server usage. Type the following command to generate ssh keys (open terminal and type the command):
$ ssh-keygen
Generate SSH keys looks as follows:
The above command creates
/.ssh/ directory. So if your user name is vivek, than all files are stored in /home/vivek/.ssh/ or $HOME/.ssh/ directory as follows:
- $HOME/.ssh/id_rsa – Your private key. Do not share this file with anyone. Keep it private
- $HOME/.ssh/id_rsa.pub – Your public key.
Please note that the passphrase must be different from your current password and do not share keys or passphrase with anyone. Also, make sure you have correct and secure permissions on $HOME/.ssh/ directory:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
SSH Keys Are Generated, What Next?
You need to copy $HOME/.ssh/id_rsa.pub file to remote server so that you can login using keys instead of the password. Use any one of the following command to copy key to remote server called vpn22.nixcraft.net.in for vivek user:
ssh-copy-id vivek@vpn22.nixcraft.net.in
On some *nix system such as OS X ssh-copy-id command may not be installed, use the following commands (when prompted provide the password for remote user account called vivek) to install/append the public key on remote host:
ssh vivek@vpn22.nixcraft.net.in «umask 077; mkdir .ssh»
cat $HOME/.ssh/id_rsa.pub | ssh vivek@vpn22.nixcraft.net.in «cat >> .ssh/authorized_keys»
To login simply type:
ssh vivek@vpn22.nixcraft.net.in
The following command will help to remember passphrase
exec ssh-agent $SHELL
ssh-add
ssh vivek@vpn22.nixcraft.net.in
Optional ssh-keygen command syntax for advance users
The following syntax specifies the 4096 of bits in the RSA key to creation (default 2048):
ssh-keygen -t rsa -b 4096 -f
/.ssh/aws.key -C «My AWs cloud key»
Where,
- -t rsa : Specifies the type of key to create. The possible values are “rsa1” for protocol version 1 and “dsa”, “ecdsa”, “ed25519”, or “rsa” for protocol version 2.
- -b 4096 : Specifies the number of bits in the key to create.
- -f
/.ssh/aws.key : Specifies the filename of the key file.
Now install the
/.ssh/aws.key, run:
ssh-copy-id -i
/.ssh/aws.key user@aws-server-ip
Test it with the ssh command:
ssh -i
Conclusion
You learned how to create and generate ssh keys using the ssh-keygen command.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How To Set up SSH Keys on a Linux / Unix System
I recently read that SSH keys provide a secure way of logging into a Linux and Unix-based server. How do I set up SSH keys on a Linux or Unix based systems? In SSH for Linux/Unix, how do I set up public key authentication?
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | OpenSSH client and server |
Est. reading time | 8 mintues |
This page explains a public key and shows you how to set up SSH keys on a Linux or Unix-like server. I am assuming that you are using Linux or Unix-like server and client with the following software:
- OpenSSH SSHD server
- OpenSSH ssh client and friends on Linux (Ubuntu, Debian,
BSD, RHEL, CentOS, MacOS/OSX, AIX, HP-UX and co).
What is a public key authentication?
OpenSSH server supports various authentication schema. The two most popular are as follows:
- Passwords based authentication
- Public key based authentication. It is an alternative security method to using passwords. This method is recommended on a VPS, cloud, dedicated or even home based server.
How to set up SSH keys
Steps to setup secure ssh keys:
- Create the ssh key pair using ssh-keygen command.
- Copy and install the public ssh key using ssh-copy-id command on a Linux or Unix server.
- Add yourself to sudo or wheel group admin account.
- Disable the password login for root account.
- Test your password less ssh keys login using ssh user@server-name command.
Let us see all steps in details.
How do I set up public key authentication?
You must generate both a public and a private key pair. For example:
Fig.01: Our sample setup
In public key based method you can log into remote hosts and server, and transfer files to them, without using your account passwords. Feel free to replace server1.cyberciti.biz and client1.cyberciti.biz names with your actual setup. Enough talk, let’s set up public key authentication. Open the Terminal and type following commands if .ssh directory does not exists:
1: Create the key pair
On the computer (such as client1.cyberciti.biz), generate a key pair for the protocol.
You need to set the Key Pair location and name. I recommend you use the default location if you do not yet have another key there, for example: $HOME/.ssh/id_rsa. You will be prompted to supply a passphrase (password) for your private key. I suggest that you setup a passphrase when prompted. You should see two new files in $HOME/.ssh/ directory:
- $HOME/.ssh/id_rsa – contains your private key.
- $HOME/.ssh/id_rsa.pub – contain your public key.
Optional syntax for advance users
The following syntax specifies the 4096 of bits in the RSA key to creation (default 2048):
$ ssh-keygen -t rsa -b 4096 -f
/.ssh/vps-cloud.web-server.key -C «My web-server key»
Where,
- -t rsa : Specifies the type of key to create. The possible values are “rsa1” for protocol version 1 and “dsa”, “ecdsa”, “ed25519”, or “rsa” for protocol version 2.
- -b 4096 : Specifies the number of bits in the key to create
- -f
Источник
Creating an SSH Key Pair and Configuring Public Key Authentication on a Server
Password authentication is the default method most SSH (Secure Shell) clients use to authenticate with remote servers, but it suffers from potential security vulnerabilities like brute-force login attempts. An alternative to password authentication is using public key authentication with SSH, in which you generate and store on your computer a pair of cryptographic keys and then configure your server to recognize and accept your keys. Using key-based authentication offers a range of benefits:
Key-based login is not a major target for brute-force hacking attacks.
If a server that uses SSH keys is compromised by a hacker, no authorization credentials are at risk of being exposed.
Because a password isn’t required at login, you can log into servers from within scripts or automation tools that you need to run unattended. For example, you can set up periodic updates for your servers with a configuration management tool like Ansible, and you can run those updates without having to be physically present.
This guide explains how the SSH key login scheme works, how to generate an SSH key, and how to use those keys with your Linode.
How SSH Public Keys Work
SSH keys are generated in pairs and stored in plain-text files. The key pair (or keypair) consists of two parts:
A private key, usually named id_rsa . The private key is stored on your local computer and should be kept secure, with permissions set so that no other users on your computer can read the file.
A public key, usually named id_rsa.pub . The public key is placed on the server you intend to log in to. You can freely share your public key with others. If someone else adds your public key to their server, you will be able to log in to that server.
When a site or service asks for your SSH key, they are referring to your SSH public key ( id_rsa.pub ). For instance, services like GitHub and Gitlab allow you to place your SSH public key on their servers to streamline the process of pushing code changes to remote repositories.
How Does SSH Public Key Authentication Work?
In the previous section, we saw that we have a public key and a private key. We understand that they play an important role in enabling secure access. But how? The best way to understand them is to understand that the following components in this authentication system are mathematically related to each other:
- Public key
- Private key
- Authentication algorithm
If you use your Public key to encrypt something, then only your private key can decrypt it. Similarly, once you encrypt something using your private key, it can only be decrypted by your public key. And to enable secure access between servers/machines, we share our public key with the other machine to enable secure access.
But to carry this encryption and decryption, there is an algorithm that runs in the background and keeps SSH secure. Here’s how it works:
- Signed communication: Any message that goes out is signed using your private keys.
- Verification of communication: Your server has a public key from the sender stored. A signed message is verified by using this public key to decrypt the message.
When we sign a message, we allow others to decrypt the message as well. But when the receiver decrypts this message, they can safely and securely validate that the communication is in fact from you. To match these keys and validate, we use an algorithm like Diffie-Hellman.
The authorized_keys File
In order for your Linode to recognize and accept your key pair, you must upload your public key to your server. More specifically, you must upload your public key to the home directory of the user you would like to log in as. If you would like to log in to more than one user on the server using your key pair, you must add your public key to each of those users.
To set up SSH key authentication for one of your server’s users, add your public key to a new line inside the user’s authorized_keys file. This file is stored inside a directory named .ssh/ under the user’s home folder. A user’s authorized_keys file can store more than one public key, and each public key is listed on its own line. If your file contains more than one public key, then the owner of each key listed can log in as that user.
Granting Someone Else Access to Your Server
To give someone else access to your server’s user, simply add their public key on a new line in your authorized_keys file, just as you would add your own. To revoke access for that person, remove that same line and save the changes.
Challenge-Response
When logging in to a server using SSH, if that servers has a public key on file, the server creates a challenge. This challenge is crafted in such a way that only the holder of the private SSH key can decipher it.
This challenge-response action happens without any user interaction. If the person attempting to log in has the corresponding private key, then they can safely log in. If not, the login either fails or falls back to a password-based authentication scheme.
SSH Private Key Passphrases
You can optionally provide an additional level of security for your SSH private key by encrypting it locally with a passphrase at the time of creation. When you attempt to log in using an encrypted SSH key, you are prompted to enter its passphrase. This is not to be confused with a password, as this passphrase only decrypts the key file locally. A passphrase is not transferred over the Internet as a password might be.
If you’d like to set up your logins so that they require no user input, then creating a passphrase might not be desirable. Nevertheless, using a passphrase to protect your private key is strongly recommended.
Is it Safe to Share Public SSH Key?
Yes, it is safe to share your public SSH key with others. Public keys usually stored as id_rsa.pub are used to log into other servers. If anyone else has your public SSH keys on their server and they add them, you can log into their servers.
How Secure is SSH Key Authentication?
SSH key authentication is very secure. In addition to allowing secure remote authentication, it also brings its ability to withstand brute force attacks. Typically, passwords sent over any network can be vulnerable to these brute force attacks. With SSH key authentication, signed messages are exchanged using SSH keys that are up to 4096 bits in length, which is equivalent to a 20 character password.
SSH keys are machine-generated, and not human-generated. Human bias towards certain strings and numbers has proven to increase vulnerability in secure systems as opposed to machine-generated keys.
What makes SSH even more secure is the fact that you can easily add a passphrase on top of your SSH key authentication. This is also commonly referred to as multi-factor authentication or MFA.
Public Key Authentication on Linux And macOS
Generate an SSH Key Pair on Linux And macOS
Perform the steps in this section on your local machine.
Create a new key pair.
This command will overwrite an existing RSA key pair, potentially locking you out of other systems.
If you’ve already created a key pair, skip this step. To check for existing keys, run ls
If you accidentally lock yourself out of the SSH service on your Linode, you can still use the Lish console to login to your server. After you’ve logged in via Lish, update your authorized_keys file to use your new public key. This should re-establish normal SSH access.
The -b flag instructs ssh-keygen to increase the number of bits used to generate the key pair, and is suggested for additional security.
Press Enter to use the default names id_rsa and id_rsa.pub in the /home/your_username/.ssh directory before entering your passphrase.
While creating the key pair, you are given the option to encrypt the private key with a passphrase. This means that the key pair cannot be used without entering the passphrase (unless you save that passphrase to your local machine’s keychain manager). We suggest that you use the key pair with a passphrase, but you can leave this field blank if you don’t want to use one.
Upload Your Public Key
There are a few different ways to upload your public key to your Linode from Linux and macOS client systems:
Using ssh-copy-id
ssh-copy-id is a utility available on some operating systems that can copy a SSH public key to a remote server over SSH.
To use ssh-copy-id , pass your username and the IP address of the server you would like to access:
You’ll see output like the following, and a prompt to enter your user’s password:
Verify that you can log in to the server with your key.
Using Secure Copy (scp)
Secure Copy ( scp ) is a tool that copies files from a local computer to a remote server over SSH:
Connect to your server at its IP address via SSH with the user you would like to add your key to:
/.ssh directory and authorized_keys file if they don’t already exist:
/.ssh directory and authorized_keys files appropriate file permissions:
In another terminal on your local machine, use scp to copy the contents of your SSH public key ( id_rsa.pub ) into the authorized_keys file on your server. Substitute in your own username and your server’s IP address:
Verify that you can log in to the server with your key.
Manually Copy Your Public Key
You can also manually add an SSH key to a server:
Begin by copying the contents of your public SSH key on your local computer. You can use the following command to output the contents of the file:
You should see output similar to the following:
Note that the public key begins with ssh-rsa and ends with [email protected] .
Once you have copied that text, connect to your server via SSH with the user you would like to add your key to:
/.ssh directory and authorized_keys file if they don’t already exist:
/.ssh directory and authorized_keys files appropriate file permissions:
Open the authorized_keys file with the text editor of your choice ( nano , for example). Then, paste the contents of your public key that you copied in step one on a new line at the end of the file.
Save and close the file.
If you initially logged into the server as root but edited the authorized_keys file of another user, then the .ssh/ folder and authorized_keys file of that user may be owned by root . Set that other user as the files’ owner:
Verify that you can log in to the server with your key.
Connect to the Remote Server
SSH into the server from your local machine:
If you choose to use a passphrase when creating your SSH key, you will be prompted to enter it when you attempt to log in. Depending on your desktop environment, a window may appear:
You may also see the passphrase prompt at your command line:
Enter your passphrase. You should see the connection established in the local terminal.
Public Key Authentication on Windows
The following instructions use the PuTTY software to connect over SSH, but other options are available on Windows too.
Generate a Key Pair with PuTTY
Download PuTTYgen ( puttygen.exe ) and PuTTY ( putty.exe ) from the official site.
Launch puttygen.exe . The RSA key type at the bottom of the window is selected by default for an RSA key pair but ED25519 ( EdDSA using Curve25519) is a comparable option if your remote machine’s SSH server supports DSA signatures. Do not use the SSH-1(RSA) key type unless you know what you’re doing.
Increase the RSA key size from 2048 bits 4096 and click Generate:
PuTTY uses the random input from your mouse to generate a unique key. Once key generation begins, keep moving your mouse until the progress bar is filled:
When finished, PuTTY displays the new public key. Right-click on it and select Select All, then copy the public key into a Notepad file.
Save the public key as a .txt file or some other plaintext format. This is important–a rich text format such as .rtf or .doc can add extra formatting characters and then your private key won’t work:
Enter a passphrase for the private key in the Key passphrase and Confirm passphrase text fields:
Click Save private key. Choose a filename and location in Explorer while keeping the ppk file extension. If you plan to create multiple key pairs for different servers, be sure to give them different names so that you don’t overwrite old keys with new:
Manually Copy the SSH Key with PuTTY
Launch putty.exe . Find the Connection tree in the Category window, expand SSH and select Auth. Click Browse and navigate to the private key you created above:
Scroll back to the top of the Category window and click Session. Enter the hostname or IP address of your Linode. PuTTY’s default TCP port is 22 , the IANA assigned port for SSH traffic. Change it if your server is listening on a different port. Name the session in the Saved Sessions text bar and click Save:
Click the Open button to establish a connection. You are prompted to enter a login name and password for the remote server.
Once you’re logged in to the remote server, configure it to authenticate with your SSH key pair instead of a user’s password. Create an .ssh directory in your home directory on your Linode, create a blank authorized_keys file inside, and set their access permissions:
Open the authorized_keys file with the text editor of your choice ( nano , for example). Then, paste the contents of your public key that you copied in step one on a new line at the end of the file.
Save, close the file, and exit PuTTY.
Verify that you can log in to the server with your key.
Manually Copy the SSH Key with WinSCP
Uploading a public key from Windows can also be done using WinSCP:
In the login window, enter your Linode’s public IP address as the hostname, the user you would like to add your key to, and your user’s password. Click Login to connect.
Once connected, WinSCP shows two file tree sections. The left shows files on your local computer and the right shows files on your Linode. Using the file explorer on the left, navigate to the file where you saved your public key in Windows. Select the public key file and click Upload in the toolbar above.
You are prompted to enter a path on your Linode where you want to upload the file. Upload the file to /home/your_username/.ssh/authorized_keys .
Verify that you can log in to the server with your key.
Connect to the Remote Server with PuTTY
Start PuTTY and Load your saved session. You are be prompted to enter your server user’s login name as before. However, this time you are prompted for your private SSH key’s passphrase rather than the password for your server’s user. Enter the passphrase and press Enter.
Upload Your SSH Key to Linode Cloud Manager
It is possible to provision each new Linode you create with an SSH public key automatically through the Cloud Manager.
Click on your username or profile image at the top bar of the page. Then click on SSH Keys in the dropdown menu that appears:
Источник