Sending file through ssh2, scp - linux

I need to send a copy from server1 to server2 using ssh2. Both are using ubuntu, and I'm trying sending it with the scp command:
scp test.txt userName#server2_direction:/folder_destination
But I'm getting an error:
test.txt: No such file or directory
I tried putting the complete url: /users/asd/my_user/folder/test.txt, but it doesn't work either.
Any hints?

You are in the wrong directory on the source computer. Check if there is test.txt in the current directory.
$ ls -l test.txt

Related

How to remotely SCP latest file in one server to another?

I have 3 Linux machines, namely client, server1 and server2.
I am trying to achieve something like this. I would like to copy the latest file in a particular directory of server1 to server2. I will not be doing this by logging into server1 directly, but I always log on to client machine first. Let me list down the step by step approach which is happening now:
Log on to client machine using ssh
SSH into server1
Go to directory /home/user1 in server1 using the command ls /home/user1 -Art | tail -n 1
SCP the latest file to /home/user2 directory of server2
This manual operation is happening just fine. I have automated this using a one line script like below:
ssh user1#server1 scp -o StrictHostKeyChecking=no /home/user1/test.txt user2#server2:/home/user2
But as you can see, I am uploading the file /home/user1/test.txt. How can I modify this script to always upload the latest file in the directory /home/user1?
If zsh is available on server1, you could use its advanced globbing features to find the most recent file to copy:
ssh user1#server1 \
"zsh -c 'scp -o StrictHostKeyChecking=no /home/user1/*(om[1]) user2#server2:/home/user2'"
The quoting is important in case your remote shell on server1 is not zsh.
You can use SSH to list the last file and after user scp to copy the file as follow:
FILE=$(ssh user1#server1 "ls -tp $REMOTE_DIR |grep -v / | grep -m1 \"\""); scp user1#server1:$FILE user2#server2:/home/user2
Before launch these command you need to set the remote directory where to search for the last modified file as:
REMOTE_DIR=/home/user1

Copy folder frrom local machine to a server in linux

I am trying to copy a filled folder from my local machine to AWS server.
So, I used the following command, but was not working:
scp -r IPADTEST.pem oafolder ec2-user#__________.compute.amazonaws.com:testfolder
The error was:
ec2-user#_________.compute.amazonaws.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
lost connection
I am sure the IPADTEST.pem is working okay, because I can SSH from the same location:
$ ssh -i IPADTEST.pem ec2-user#_____________.compute.amazonaws.com
Also, I can copy a file (not folder), for example I can copy index.html:
sudo scp -i IPADTEST.pem index.html ec2-user#______________.compute.amazonaws.com:testfolder/
You absolutely need "-i .pem".
Q: Have you tried scp -r -i IPADTEST.pem oafolder ec2-user#__________.compute.amazonaws.com:testfolder?

How to properly upload a local file to a server using mobaXterm?

I'm trying to upload a file from my local desktop to a server and I'm using this command:
scp myFile.txt cooluser#192.168.10.102:/opt/nicescada/web
following the structure: scp filename user#ip:/remotePath.
But I get "Permission Denied". I tried using sudo , but I get the same message. I'm being able to download from the server to my local machine, so I assume I have all permissions needed.
What can be wrong in that line of code?
In case your /desired/path on your destination machine has write access only for root, and if you have an account on your destination machine with sudo privileges (super user privileges by prefixing a sudo to your command), you could also do it the following way:
Option 1 based on scp:
copy the file to a location on your destination machine where you have write access like /tmp:
scp file user#destinationMachine:/tmp
Login to your destination machine with:
ssh user#destinationMachine
Move the file to your /desired/path with:
sudo mv /tmp/file /desired/path
In case you have a passwordless sudo setup you could also combine step 2. and 3. to
ssh user#destination sudo mv /tmp/file /desired/path
Option 2 based on rsync
Another maybe even simpler option would be to use rsync:
rsync -e "ssh -tt" --rsync-path="sudo rsync" file user#destinationMachine:/desired/path
with -e "ssh -tt" added to run sudo without having a tty.
Try and specify the full destination path:
scp myFile.txt cooluser#192.168.10.102:/opt/nicescada/web/myFile.txt
Of course, double-check cooluser has the right to write (not just read) in that folder: 755, not 644 for the web parent folder.

SCP not working permission denied even with SSH key given

I am trying to get scp to work and transfer a file from a remote server to my local. I tried looking around and this post helped the most but it still is not working here is the current output.
<HOSTNAME>:chef4-rep
<USERNAME>$ sudo scp -i ./.chef/<NAME>.pem <USERNAME>#<IP>:/home/postgres/post_0604_dump/db0604_schema_and_data.sql ~/
<USERNAME>#<IP>: Permission denied (publickey).
The issue turned out not to be with my command but that I was trying to copy a file in another users directory and it wouldn't work. I ended up SSH'ing in and using sudo to copy the file to my home directory and then used scp with no issues.
Kindly use below command to get it done.
root#localhost# scp -r "source_file/directory" "Destination address- remote-IP/Hostname:/location"
And if you are using passwordless ssh then make sure you are using correct user whose public keys are shared with remote server.
Thanks
I had the same issue with scp and got Permission denied (publickey):
This worked:
ssh -i "mykey.pem" ubuntu#??.??.??.???
But this didn't: scp -i "mykey.pem" test.php ubuntu#??.??.??.???:
I solved it by removing the quotes off my key file:
scp -i mykey.pem test.php ubuntu#??.??.??.???:

How can I copy file from local server to remote with creating directories which absent via SSH?

I can copy file via SSH by using SCP like this:
cd /root/dir1/dir2/
scp filename root#192.168.0.19:$PWD/
But if on remote server some directories are absent, in example remote server has only /root/ and havn't dir1 and dir2, then I can't do it and I get an error.
How can I do this - to copy file with creating directories which absent via SSH, and how to make it the easiest way?
The easiest way mean that I can get current path only by $PWD, i.e. script must be light moveable without any changes.
This command will do it:
rsync -ahHv --rsync-path="mkdir -p $PWD && rsync" filename -e "ssh -v" root#192.168.0.19:"$PWD/"
I can make the same directories on the remote servers and copy file to it via SSH by using SCP like this:
cd /root/dir1/dir2/
ssh -n root#192.168.0.19 "mkdir -p '$PWD'"
scp -p filename root#192.168.0.19:$PWD/

Resources