How to start ssh-agent on Ubuntu

On Ubuntu, we can use this command

eval $(ssh-agent -s)

It will output something like

Agent pid 2499xxx

After this, we can use ssh-add

ssh-add <path to the private key>

Explain “eval $(ssh-agent -s)”

The command eval $(ssh-agent -s) is a convenient way to start the SSH agent and set up the necessary environment variables in the current shell session. Here’s a breakdown of what each part of the command does:

  1. ssh-agent -s: This command starts the SSH agent in the background and outputs shell commands to set up the environment variables that point to the newly created SSH agent. The -s option instructs ssh-agent to output commands suitable for use in a shell script.
  2. $(...): This is called command substitution. It allows the output of a command (ssh-agent -s) to be substituted in place as part of another command. Whatever is inside $(...) is executed, and its output is treated as if it were written directly into the command line.
  3. eval: This is a shell built-in command that evaluates the arguments as a shell command. It basically causes the shell to re-parse and execute the command line generated by the command substitution $(ssh-agent -s).

So, when you run eval $(ssh-agent -s), the ssh-agent -s command starts the SSH agent and outputs shell commands to set up the environment variables, and then eval evaluates and executes those commands in the current shell session. This effectively starts the SSH agent and sets up the environment variables so that subsequent SSH-related commands know how to communicate with the SSH agent.