bash - Changing user to root when connected to a linux server and copying files -


my script coded in way doesn't allow connect server directly root. code copies files server computer , works don't have access many files because root can access them. how can connect server user , copy files switching root?

code want change:

sshpass -p "password" scp -q -r username@74.11.11.11:some_directory copy_it/here/ 

in other words, want able remotely copy files accessible root on remote server, don't wish access remote server via ssh/scp directly root.

is possible through ssh , not sshpass?

if understand question correctly, want able remotely copy files accessible root on remote machine, don't wish (or can't) access remote machine via ssh/scp directly root. , separate question whether done without sshpass.

(please understand solutions suggest below have various security implications , should weigh benefits versus potential consequences before deploying them. can't know specific usage scenario tell if these idea or not.)

when ssh/scp user, don't have access files accessible root, can't copy of them. need instead "switch root" once connected in order copy files.

"switching root" command accomplished prefixing sudo, approach remotely execute commands copy files via sudo /tmp on remote machine, changes owner connected user, , remotely copy them /tmp:

ssh username@74.11.11.11 "sudo cp -r some_directory /tmp" ssh username@74.11.11.11 "sudo chown -r username:username /tmp/some_directory" scp -q -r username@74.11.11.11:/tmp/some_directory copy_it/here/ ssh username@74.11.11.11 "rm -r /tmp/some_directory" 

however, sudo prompts user's password, you'll "sudo: no tty present , no askpass program specified" error if try this. need edit /etc/sudoers on remote machine authorize user use sudo needed commands without password. add these lines:

username all=nopasswd: /bin/cp username all=nopasswd: /bin/chown 

(or, if you're cool user being able execute command via sudo without being prompted password, instead use:)

username all=nopasswd: 

now above commands work , you'll able copy files.

as avoiding using sshpass, instead use public/private key pair, in private key on local machine unlocks public key on remote machine in order authenticate user, rather password.

to set up, on local machine, type ssh-keygen. accept default file (/home/username/.ssh/id_rsa). use empty passphrase. append file /home/username/.ssh/id_rsa.pub on local machine /home/username/.ssh/authorized_keys on remote machine:

cat /home/username/.ssh/id_rsa.pub | ssh username@74.11.11.11 \ "mkdir -m 0700 -p .ssh && cat - >> .ssh/authorized_keys && \ chmod 0600 .ssh/authorized_keys" 

once you've done this, you'll able use ssh or scp local machine without password authorization.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -