Ubuntu

How to use ssh-agent on Ubuntu

ssh-agent is configured to execute on Ubuntu automatically. If it is not started at the login automatically, it can be started manually by the `eval` command. The password of the SSH private key can be created manually by the user handle by ssh-agent. It is called an authentication agent. The ssh-agent uses the SSH_AUTH_SOCK environment variable for authentication. If this variable is set properly, then the ssh-agent will work properly. The uses of ssh-agent on Ubuntu for different purposes have been explained in this tutorial.

Prerequisites:

Before starting the steps of this tutorial, the following steps will be required to complete.

Enable the SSH service on Ubuntu if it is not enabled before.

Generate the SSH Key pairs to execute the commands in the remote server. Run the following command to create the public key and the private key. The private key will be stored in the remote server, and the public keys will be stored in the client securely.

$ ssh-keygen -t rsa

Run the following command to open the sshd_config file using nano editor to add some necessary configurations.

$ sudo nano /etc/ssh/sshd_config

Add the following lines in the file to enable the root login and password-based authentication.

PasswordAuthentication yes

PermitRootLogin yes

Run the following command to restart the SSH service.

$ sudo service ssh restart

Syntax:

Two different syntaxes of the ssh-agent have given below. The meaning of the options used with this command is described in the next part of this tutorial.

ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash] [-t life] [command [arg ...]]

ssh-agent [-c | -s] -k

Different options of ssh-agent:

the ssh-agent command has many options. The uses of the options are given below.

Option Purpose
-a bind_address It is used to bind the UNIX domain socket to the given file path forcefully instead of using the default socket.
-c It is used to generate the C-shell commands on stdout. The shell is automatically detected by default.
-d It is used to enable the debug mode.
-E fingerprint_hash It is used to define the algorithm used to generate the SSH key fingerprints. md5 and sha256 are the valid values.
-k It is used to kill the currently running agent.
-s It is used to generate the Bourne shell (/bin/sh) commands on stdout forcefully. The shell is automatically detected by default.
-t life It is used to define the maximum number of seconds used to keep the agent’s identity. The value of this option defines in seconds, but it can be defined by different suffixes, such as m for minutes, h for hours, d for days, and w for weeks. If this option is not used, the agent will keep the keys in the memory as long as it runs. This value will be overridden when the ssh-add command will be executed.

The use of the -t option has been shown here. Run the following command to keep the identity of the agents for 60 seconds.

$ ssh-agent -t 60

The following output will be appeared after executing the above command.

Using ssh-agent command for non-interactive authentication:

Run the following command from the server machine to start the ssh-agent for non-interactive authentication.

$ eval $(ssh-agent)

The following output will be appeared after executing the above command. ssh-agent is running now. Here, the username of the server machine is ‘fahmida.’

Use ssh-add to add the private key passphrase to ssh-agent:

the ssh-add command will require providing the passphrase for the ssh private keys. Now, log in to the client machine and run the ssh-add command from the terminal.

$ ssh-add

After adding the private key to the ssh-agent, different types of SSH commands, such as scp, sftp, ssh, etc., can be executed from the client machine.

Copy file from client to server:

The scp command to copy the file from the client to the server and ssh command to connect with the server for checking the file is copied or not shown in this part of the tutorial. Create a text file named testfile.txt with the following content.

testfile.txt

It is a testing file created in the client machine.

Run the following command to check the content of the testfile.txt file before executing the `scp` command.

$ cat testfile.txt

The following output will be appeared after executing the above command.

Run the following command to copy the testfile.txt file from the client machine to the Documents folder of the server machine.

$ scp testfile.txt fahmida@fahmida-VirtualBox:~/Documents/

The following output will be appeared after executing the above command if the file is copied successfully to the server location. Here, the username of the server machine is ‘fahmida,’ and the username of the client machine is ‘Yasmin.

Run the following command to establish a connection with the server using ssh command.

$ ssh fahmida@fahmida-VirtualBox

Run the following command to go to the Documents folder of the server machine to check the text file has been copied here or not.

$ cd Documents

Run the following command to check the list of the files and folders of the Documents folder.

$ ls
[

The following output shows that the text file exists in the Documents folder of the server machine.

Now, run the following command to check the content of the testfile.txt file of the server machine.

$ cat testfile.txt

The following output shows that the content of testfile.txt of the server machine is the same as the content of the textfile.txt of the client machine.

Remove all cached ssh-agent private keys:

Run the following ssh-add command with the -D option to remove all private keys of the cached ssh-agent.

$ ssh-add -D

The following output will be appeared after executing the above command.

Conclusion:

Using ssh-agent is one way to connect with the remote machine from the client machine to do different tasks. Any file can be copied from the client machine to the server machine by running an SSH agent shown in this tutorial to help the reader know the use of this command.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.