Linux: Hide Your Shell Passwords with sshpass (2024)

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:

1

sudo apt-get install sshpass -y


For Fedora-based distributions, that command would be:

1

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:

1

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:

1

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:

1

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:

1

2

#!/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
Linux: Hide Your Shell Passwords with sshpass (2024)
Top Articles
How to Be a Billionaire Free Summary by Martin S. Fridson
24 products people waste too much money on that you should stop buying immediately
NOAA: National Oceanic & Atmospheric Administration hiring NOAA Commissioned Officer: Inter-Service Transfer in Spokane Valley, WA | LinkedIn
Ups Stores Near
Lexi Vonn
Euro (EUR), aktuální kurzy měn
Craftsman M230 Lawn Mower Oil Change
When is streaming illegal? What you need to know about pirated content
Southland Goldendoodles
4156303136
Syracuse Jr High Home Page
Blog:Vyond-styled rants -- List of nicknames (blog edition) (TouhouWonder version)
Sand Castle Parents Guide
Craigslist Malone New York
272482061
Buy PoE 2 Chaos Orbs - Cheap Orbs For Sale | Epiccarry
Cpt 90677 Reimbursem*nt 2023
Aaa Saugus Ma Appointment
Juicy Deal D-Art
Shiftselect Carolinas
Plaza Bonita Sycuan Bus Schedule
Airtable Concatenate
Dmv In Anoka
Star Wars Armada Wikia
Things to do in Pearl City: Honolulu, HI Travel Guide by 10Best
Federal Express Drop Off Center Near Me
Ucm Black Board
Acuity Eye Group - La Quinta Photos
Blackstone Launchpad Ucf
Dreammarriage.com Login
Gwu Apps
To Give A Guarantee Promise Figgerits
Bismarck Mandan Mugshots
Woodman's Carpentersville Gas Price
Planet Fitness Santa Clarita Photos
Blackstone Launchpad Ucf
Discover Wisconsin Season 16
Sams Gas Price Sanford Fl
Owa Hilton Email
Pink Runtz Strain, The Ultimate Guide
How To Customise Mii QR Codes in Tomodachi Life?
Honkai Star Rail Aha Stuffed Toy
About Us
This Doctor Was Vilified After Contracting Ebola. Now He Sees History Repeating Itself With Coronavirus
Union Supply Direct Wisconsin
Erespassrider Ual
Abigail Cordova Murder
French Linen krijtverf van Annie Sloan
Besoldungstabellen | Niedersächsisches Landesamt für Bezüge und Versorgung (NLBV)
7 Sites to Identify the Owner of a Phone Number
라이키 유출
Wayward Carbuncle Location
Latest Posts
Article information

Author: Duncan Muller

Last Updated:

Views: 6189

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Duncan Muller

Birthday: 1997-01-13

Address: Apt. 505 914 Phillip Crossroad, O'Konborough, NV 62411

Phone: +8555305800947

Job: Construction Agent

Hobby: Shopping, Table tennis, Snowboarding, Rafting, Motor sports, Homebrewing, Taxidermy

Introduction: My name is Duncan Muller, I am a enchanting, good, gentle, modern, tasty, nice, elegant person who loves writing and wants to share my knowledge and understanding with you.