changing user for command


I want to log in with SSH as "webuser" but I want to do the following:

     cp -pr /home/web/ /home/web/

as the user "hank" password "mypassword"

Can this be done?  If so, how?

LVL 16
Who is Participating?
liddlerConnect With a Mentor Commented:
In your sudoers file, use
webadmin ALL=NOPASSWD: cp -pr /home/web/ /home/web/

Then password is not required
Either use sudo, to give user webuser hank's priviledges or use eXpect ( to write a script that passes the password.

sudo is probably better, with the NOPASSWD option, man sudo for details
To log on to a server as a different user

ssh -l username server


ssh username@server

Im not 100% on what you are trying to achieve are you coping files from one server to another? Or locally copying files once logged in on the remote computer?
If its a local copy the syntax is correct if you are copying from a remote comp to local you need to use

scp -rp user@remote:/path/to/files user@local:/path/to/files

hankknightAuthor Commented:

What I am actually trying to accomplish is to get a web script to copy local files to a different local directory.  Because of this there must not be a password prompt.

Looks like sudo should do what I need.

but I can't figure out how to enter the password for "hank"

          sudo -u webadmin -p %U mypassword cp -pr /home/web/ /home/web/

does not work.

How can I proporly pass the username and password values?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.