VOOZH about

URL: https://thenewstack.io/linux-hide-your-shell-passwords-with-sshpass/

⇱ Linux: Hide Your Shell Passwords with sshpass - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-02-03 04:00:14
Linux: Hide Your Shell Passwords with sshpass
Operations

Linux: Hide Your Shell Passwords with sshpass

Hard-coding is never the answer. Instead, use sshpass when you need to put a password in your Bash script.
Feb 3rd, 2024 4:00am by Jack Wallen
👁 Featued image for: Linux: Hide Your Shell Passwords with sshpass

At some point in your interactions with Linux, you will write a shell script, Bash or otherwise. It could be as simple as a single line or as complex as any program you’ve ever written. Either way, they help to make Linux the most flexible and powerful operating system on the planet.

The deeper you dive into shell scripting in Linux, you might run into a situation where you need to include a password in a script. When that happens, you certainly don’t want to hard-code that password.

Or, if you wind up getting prompted for the password, you can’t automate the script. Anyone who gains access to your machine could view that script and then have access to whatever account that password is associated with. Also, cron jobs for that script will fail.

For example, you might create a backup script that uses rsync over a network and requires a user password for security purposes. Say, for example, you have the /data directory which houses specific information for which you need to have a regular backup. You’ve set it up such that it has all the necessary permissions and the last thing to do is create a backup that will save the contents to a remote machine.

Such a script might look something like this:

!/bin/bash
rsync -av /data USER@SERVER:/home/USER/databackup

Where USER is the remote username and SERVER is the IP address or domain of the remote server. When you run the script, you’ll be prompted for a password.

How do you get around such a situation?

With a Little Help from sshpass…

The application sshpass was created specifically for the purpose of password automation. This non-interactive tool makes it possible to automate shell scripts, even if they require a password.

Let me show you how it works..

Installing sshpass

To use sshpass, you’ll need a running Linux distribution. I’m going to demonstrate with Ubuntu Server 22.04 but the app can be installed on Fedora-based distributions as well. You’ll also need a user with sudo privileges.

To install sshpass on a Ubuntu-based distribution, open a terminal window and issue the command:

sudo apt-get install sshpass -y

For Fedora-based distributions, that command would be:

sudo dnf install sshpass -y

That’s it for the installation.

Using sshpass

We’re going to stick with our backup script idea. The first thing we must do is create an encrypted file that will hold our password. Create the file with the command:

nano ~/.password

Name that file whatever you like, but I would suggest making it hidden by using a period at the beginning of the filename.

In that file add the password for the account used in the shell script and save it with the Ctrl-x keyboard shortcut.

Encrypt the file with:

gpg -c ~/.password

You’ll be prompted to type and verify a password for the encryption.

The above command will create a new file, named .password.gpg that has the encrypted version of the password. You can then delete the ~/.password file.

Creating the Shell Script

Remember, we’re sticking with our simple backup script. First, I’m going to demonstrate how to simply pass the password with the sshpass command (so you can see how it works). For example, an rsync backup command that requires user authentication would look like this:

sshpass -p "PASSWORD" rsync -av /data USER@SERVER:/home/USER/databackup

Where PASSWORD is the remote user password, USER is the remote username, and SERVER is the IP address or domain of the remote server. The sshpass app will pass the password to the rync command and everything should work as expected.

Of course, you don’t want to hard-code that password, right? To avoid that, you have to get a bit creative with the script, and here’s what it would look like:

#!/bin/bash
gpg -d -q ~/.password.gpg | sshpass rsync -av /data USER@SERVER:/home/USER/databackups

Where USER is the remote username and SERVER is the IP address or domain of the remote server.

What we’ve done here is first decrypt the .password.gpg file and send the output of that to sshpass which then is used by rsync to connect to the remote server for the backup.

It’s a bit tricky but it works.

With the help of sshpass, you can create shell scripts that can work with an encrypted password, passing it to sshpass within the script and never having to hard-code a password or interact with the script.

By doing this you add a layer of security to the system while also making it possible to create automated scripts to do just about anything you need.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
Feature image by Eak K. from Pixabay.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.