Jump to content

[CentOS] How to execute a command that requires a password at every startup?

Go to solution Solved by MysticalGnome,

You don't have to use a password, you could generate a ssh key so you can mount it from /etc/fstab without needing to enter your password.

ssh-keygen -t rsa -b 2048

Now you should have a .ssh directory in your home directory with a file named id_rsa.pub, to get it on the other server execute:

ssh-copy-id -i ~/.ssh/id_rsa.pub user@IP_address

Add the mount line to /etc/fstab, depending on which version of CentOS (6 and older) would be something like this

sshfs#user@IP_address:/path/to/remote/mount/directory /local/mounted/folder fuse -o  _netdev,allow_other,nonempty,ServerAliveInterval=15,ServerAliveCountMax=3 0 0

Or CentOS 7

user@IP_address:/path/to/remote/mount/directory /local/mounted/folder fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=youruser,IdentityFile=/home/youruser/.ssh/id_rsa,allow_other,reconnect 0 0

Hope this helps a bit.

Hello,

I need some help from our Linux gurus :(

We have a CentOS-based FTP server (vsftpd) in a Virtual Machine, I need to create a permanent way to make a certain command run at each system boot.

I wanted to put it inside of the /etc/rc.d/rc.local but it won't work because the command is a remote mount that requires a password every time it's executed (aka once until the next reboot).

The command is as following:

sshfs -C -o allow_other,nonempty,ServerAliveInterval=15,ServerAliveCountMax=3 user@IP_address:/path/to/remote/mount/directory /local/mounted/folder

It works great but obviously it wants me to authorize with a password right after the command is executed so it can authorize with the remote server.

I don't want to be forced to re-run the command at each VM reboot so I'm looking for a permanent solution, can anyone point me to it?

The best way to do that would probably be to put the password inside the command so it doesn't require a password right after running it, but I'm not sure if it's even possible and if so - how to do it?

I'd be very grateful to someone willing to help me.

Thanks in advance!
 

CPU: AMD Ryzen 7 5800X3D GPU: AMD Radeon RX 6900 XT 16GB GDDR6 Motherboard: MSI PRESTIGE X570 CREATION
AIO: Corsair H150i Pro RAM: Corsair Dominator Platinum RGB 32GB 3600MHz DDR4 Case: Lian Li PC-O11 Dynamic PSU: Corsair RM850x White

Link to comment
Share on other sites

Link to post
Share on other sites

Is there a way to specify the password with that command?

You could drop the password in a file and reference it that way.

Current Network Layout:

Current Build Log/PC:

Prior Build Log/PC:

Link to comment
Share on other sites

Link to post
Share on other sites

I think you would need sshpass (can't remember the command structure) but to install it, it should be ;

yum install sshpass

 

Edited by wkdpaul

If you need help with your forum account, please use the Forum Support form !

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Lurick said:

Is there a way to specify the password with that command?

You could drop the password in a file and reference it that way.

So you're suggesting to just add: username=user,password=pass to the command? Also, would the "username=user" option be necessary if the command has an "user@IP_address" in it already?

I suppose I could also try putting credentials into a file as you suggested, I found something that might be useful to help me do that:
GwQgVwI.png

What do you think? :)

Thank you for your help

2 minutes ago, wkdpaul said:

I think you would need sshpass (can't remember the command structure) but to install it, it should be ;


yum install sshpass

Thanks! It looks like it might be something that should work, I'll give it a shot if I don't figure out a simpler solution ^_^

CPU: AMD Ryzen 7 5800X3D GPU: AMD Radeon RX 6900 XT 16GB GDDR6 Motherboard: MSI PRESTIGE X570 CREATION
AIO: Corsair H150i Pro RAM: Corsair Dominator Platinum RGB 32GB 3600MHz DDR4 Case: Lian Li PC-O11 Dynamic PSU: Corsair RM850x White

Link to comment
Share on other sites

Link to post
Share on other sites

From stackexchange (that's where I found sshpass first but had to look it up since I couldn't remember how to use it)

 

echo "MyPassword" > passwordfile
chmod 600 passwordfile
sshfs user@host mountpoint -o ssh_command='sshpass -f passwordfile'

 

That should work

If you need help with your forum account, please use the Forum Support form !

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Morgan MLGman said:

So you're suggesting to just add: username=user,password=pass to the command? Also, would the "username=user" option be necessary if the command has an "user@IP_address" in it already?

I suppose I could also try putting credentials into a file as you suggested, I found something that might be useful to help me do that:


What do you think?

Yah, that should work well :)

Current Network Layout:

Current Build Log/PC:

Prior Build Log/PC:

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, wkdpaul said:

From stackexchange (that's where I found sshpass first but had to look it up since I couldn't remember how to use it)


echo "MyPassword" > passwordfile
chmod 600 passwordfile
sshfs user@host mountpoint -o ssh_command='sshpass -f passwordfile'

That should work

 

3 minutes ago, Lurick said:

Yah, that should work well :)

Thank you once again guys! I'll try it tomorrow when I come back to work :)

CPU: AMD Ryzen 7 5800X3D GPU: AMD Radeon RX 6900 XT 16GB GDDR6 Motherboard: MSI PRESTIGE X570 CREATION
AIO: Corsair H150i Pro RAM: Corsair Dominator Platinum RGB 32GB 3600MHz DDR4 Case: Lian Li PC-O11 Dynamic PSU: Corsair RM850x White

Link to comment
Share on other sites

Link to post
Share on other sites

expect scripts might come in handy also...

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
Share on other sites

Link to post
Share on other sites

You don't have to use a password, you could generate a ssh key so you can mount it from /etc/fstab without needing to enter your password.

ssh-keygen -t rsa -b 2048

Now you should have a .ssh directory in your home directory with a file named id_rsa.pub, to get it on the other server execute:

ssh-copy-id -i ~/.ssh/id_rsa.pub user@IP_address

Add the mount line to /etc/fstab, depending on which version of CentOS (6 and older) would be something like this

sshfs#user@IP_address:/path/to/remote/mount/directory /local/mounted/folder fuse -o  _netdev,allow_other,nonempty,ServerAliveInterval=15,ServerAliveCountMax=3 0 0

Or CentOS 7

user@IP_address:/path/to/remote/mount/directory /local/mounted/folder fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=youruser,IdentityFile=/home/youruser/.ssh/id_rsa,allow_other,reconnect 0 0

Hope this helps a bit.

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, MysticalGnome said:

You don't have to use a password, you could generate a ssh key so you can mount it from /etc/fstab without needing to enter your password.


ssh-keygen -t rsa -b 2048

Now you should have a .ssh directory in your home directory with a file named id_rsa.pub, to get it on the other server execute:


ssh-copy-id -i ~/.ssh/id_rsa.pub user@IP_address

Add the mount line to /etc/fstab, depending on which version of CentOS (6 and older) would be something like this


sshfs#user@IP_address:/path/to/remote/mount/directory /local/mounted/folder fuse -o  _netdev,allow_other,nonempty,ServerAliveInterval=15,ServerAliveCountMax=3 0 0

Or CentOS 7


user@IP_address:/path/to/remote/mount/directory /local/mounted/folder fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=youruser,IdentityFile=/home/youruser/.ssh/id_rsa,allow_other,reconnect 0 0

Hope this helps a bit.

I did exactly what you wrote, but instead of adding the mount line to /etc/fstab I added it to the /etc/rc.d/rc.local file and it works like a charm now, my problem is solved :)

Thank you all very much for your assistance, marking the thread as answered!

CPU: AMD Ryzen 7 5800X3D GPU: AMD Radeon RX 6900 XT 16GB GDDR6 Motherboard: MSI PRESTIGE X570 CREATION
AIO: Corsair H150i Pro RAM: Corsair Dominator Platinum RGB 32GB 3600MHz DDR4 Case: Lian Li PC-O11 Dynamic PSU: Corsair RM850x White

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×