Home SSH How to Pass Password to SSH Command in Linux

How to Pass Password to SSH Command in Linux

The only way a user can accomplish a complete Linux operating system experience is if this user has had a partial or full glimpse of both sides of the Linux environment. Linux operating system exists both as a desktop environment and as a server environment.

[ You might also like: How to Pass Password to SCP Command in Linux ]

For the desktop environment, users have the advantage of using GUI icons and applications to fulfill their operating system objectives. As for the server environment, an OS user needs some level of authentic familiarity with the Linux terminal commands to achieve an OS goal.

[ You might also like: How to Restrict SSH Access to Certain Users in Linux ]

Most users stick to the desktop environment and others to the server environment. However, a multidimensional user will want to experience both sides of these environments. In this case, a Linux desktop user will need to flexibly access a Linux server environment and a Linux server user might also want to access other Linux server environments, routers, and firewalls.

Connecting to Linux Server Using SSH

There is no better-known tool for accessing Linux routers, firewalls, and servers than through the SSH cryptographic network protocol. The traditional approach of using the SSH protocol is summarized in by the following syntax:

$ ssh your_server_username@server_domain_name_or_server_ip_address

For example, I could access one of my servers in the following manner:

$ ssh [email protected]
Connect to Linux Using SSH
Connect to Linux Using SSH

As per the above screen capture, the first access attempt to the Linux server was initiated by the command:

$ ssh [email protected]

The execution of the above SSH command then later led to the prompt:

[email protected]'s password: 

It is at this point that I keyed in the user-related (ubuntu) password and was successfully able to gain access to the Linux server.

For a novice Linux user or learner, this server-access approach is exciting and worth the pursuit. However, as you become an elite Linux user, you do not want to repeat yourself with a two-liner Linux command when you could only use one.

This article is here to fix this two-liner server access issue and provide an effective one-liner SSH access command for all your future Linux servers, routers, and firewalls access.

Install SSHPass in Linux – A SSH Password Provider

sshpass command-line tool will do the job for us. It facilitates a simplified approach to non-interactive ssh sign-in and supports one-liner ssh password input.

Firstly, you need to install the sshpass tool on your Linux operating system. Before the installation, ensure that you either have Sudo privileges or you are a Sudoer user of the Linux system.

$ sudo apt-get install sshpass     [On Debian, Ubuntu and Mint]
$ sudo yum install sshpass         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a sys-apps/sshpass  [On Gentoo Linux]
$ sudo pacman -S sshpass           [On Arch Linux]
$ sudo zypper install sshpass      [On OpenSUSE]    

How to Add Password to SSH Command in Linux

With the successful installation of SSHPASS, the one-liner SSH command syntax for accessing a remote Linux server, router, or firewall will look like the following:

$ sshpass -p  "Your_Server_Password" ssh Your_Server_Username@Server_IP/Domain_Name

In my case, I would implement SSHPASS usage in the following manner:

$ sshpass -p "My_Server_Password" ssh [email protected]
Add Password to SSH Command
Add Password to SSH Command

As expected, I have successfully accessed my Linux server with a one-liner command.

Combining the use of SSHPASS and SSH to access a remote server, router, or firewall gets rid of unnecessary two-liner commands that lead to an additional password entry prompt. It makes your remote access to other Linux environments faster and efficient.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.