start ssh forwarding at bootup

The following command works fine in interactive mode :
#ssh -fNg -L 3306:localhost:3306 sciadm@mysqlserver

I have want to put in in daemon mode such that it can run everything when server boot up.

I have tried both method but does work. Any idea ?

1. put the following command in /etc/rc.d/rc.local - it will ask for password everything but in fact it connect through ssh key in real time.
       ssh -fNg -L 3306:localhost:3306 sciadm@mysqlserver
2. put the following command in /etc/rc.d/rc.local - it can start as background but it can't do the forward. In fact the command doesn't work in interactive mode as well
       ./autossh -M 3306 -fNg -L 3306:localhost:3306 sciadm@mysqlserver

Any advise that I can test further is appreciate.  Tks
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andreasSystem AdminCommented:
The bootup scrip runs as root, in realtime you might use another user account to run that ssh command.

as the passwordless logins are per user you need to set it up for root aswell.
Steven VonaCommented:
ansreas is correct, you will need to setup ssh keys for the root user to allow password-less logins.  It is against best security practices to allow remote root logins, I would be careful.

What example are you trying to do?  If you need to forward port 3306 to another server you are probably better off using iptables or something else that is designed for that function.
AXISHKAuthor Commented:
Can I setup the script to run as a specifc user, rather than the root .... due to security issue ..

Steven VonaCommented:
You can run a command as a different user like so:

runuser -l  UserName -c 'command'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AXISHKAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.