0

I am trying to copy the ssh private key from one user to another user in single shot.

sudo su && cp /home/sk/keys/master.pem /home/sg-user/keys/

This command works well, when i run them one by one. But, it shows error as,

cp: cannot create regular file ‘/home/sg-user/keys/master.pem’: Permission denied

When i ran the command above, it switches to root prompt as,

root@sk-box:/home/sk# 

I tried,

sudo su && cp /home/sk/keys/master.pem /home/sg-user/keys/ && exit

to see, if the command exits root terminal after completing the task. But exit command doesnt have any effect and the prompt doesn't come out of root session....

Note: sudo su has been configured to not prompt for password using /etc/sudoers file

1

You should try

sudo cp /home/sk/keys/master.pem /home/sg-user/keys/
2

When commands are run they split into a child processes, so when the 'su' command runs it's going to run in a separate process to the cp command and not actually within the root terminal.

The command will work if you run cp with sudo, but it's not going to run inside the root terminal, you'd have to run the commands separately within su to accomplish that.

sudo su && sudo cp /home/sk/keys/master.pem /home/sg-user/keys/

The 'sudo so &&' is redundant though if you're only trying to accomplish the copy. This should be all that is needed:

sudo cp /home/sk/keys/master.pem /home/sg-user/keys/
1
sudo su && cp /home/sk/keys/master.pem /home/sg-user/keys/

This runs the command sudo su. sudo su runs an interactive shell as root, it is basically equivalent to sudo -s or sudo bash (if your shell is bash). Once this command has finished, if it returned a success status, the cp command is executed as the original user.

As far as the shell is concerned, sudo is an ordinary command, not special shell syntax. So, like with any other command, && is a command separator, and the command on its left is executed first, then the command on its right (if the first command was successful).

 sudo su && cp /home/sk/keys/master.pem /home/sg-user/keys/ && exit

This variant does the same, except that it exits the parent shell once sudo su returns and the file has been copied (assuming those operations were successful). The su command runs an interactive shell, which runs the commands that you pass it as input, not the commands that the parent shell is going to run later.

If you want to run two commands as root, running the second one only if the first one succeeded, you can invoke sudo twice:

sudo command1 && sudo command2

or you can make sudo invoke a shell and use the && operator in that nested shell:

sudo sh -c 'command1 && command2'

In your case, you're only running a single command, so it's just

sudo cp /home/sk/keys/master.pem /home/sg-user/keys/

sudo su is basically always useless, because both sudo and su are used to change users. Since sudo already runs the specified command as root, su is redundant. This just runs a shell as root, which you can do with sudo su.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.