DEV Community

Cover image for Copying files amongst Linux Servers
Waji
Waji

Posted on • Updated on

Copying files amongst Linux Servers

Introduction

We are aware that the cp command works for copying a file or a directory in Linux systems. However what if we want to copy something from one Linux system to another connected in the same network?

SCP

scp or (secure copy) command helps us copy files between Linux systems in a secure way as it uses SSH to get and send data


Demo

I will be using 2 Linux systems(CentOS7) in the same network (192.168.1.0/24) inside a VMWare workstation

So the basic syntax would look like:

scp [source file] <username>@<destination IP>:[destination dir]
Enter fullscreen mode Exit fullscreen mode

Before starting the demo, I will be creating 2 users and add them into the same group on the receiving end (I will call this Linux-2 from now)

groupadd scp_group
useradd s1
useradd s2
usermod -G scp_group s1
usermod -G scp_group s2
Enter fullscreen mode Exit fullscreen mode

Editing the ownership and permissions for the new /testdirectory

mkdir /test

chown .scp_group /test
chmod 755 /test
ls -ld /test
drwxrwxr-x 2 root scp_group 6 Feb  9 09:47 /test
Enter fullscreen mode Exit fullscreen mode

This is done so that we can actually copy files into this directory from Linux-1 without getting any permission errors

From Linux-1, I will use the scp command to copy a demo file

scp /scp/A s1@192.168.1.129:/test
s1@192.168.1.129's password: 
A                                                                                                              100%    0     0.0KB/s   00:00 
Enter fullscreen mode Exit fullscreen mode

notice that it asks for the password. We can just press 'enter' if we didn't set any

Doing the same for another file

scp /scp/B s2@192.168.1.129:/test
s2@192.168.1.129's password: 
B                                                                                                              100%    0     0.0KB/s   00:00    
Enter fullscreen mode Exit fullscreen mode

If we check the /test directory on Linux-2,

ll /test
total 0
-rw-r--r-- 1 s1 s1 0 Feb  9 09:54 A
-rw-r--r-- 1 s2 s2 0 Feb  9 09:58 B
Enter fullscreen mode Exit fullscreen mode

We can also 'pull' a specific file from Linux-1 within Linux-2,

scp 192.168.1.128:/scp/C /test
root@192.168.1.128's password: 
C                                                                                                    100%    0     0.0KB/s   00:00
Enter fullscreen mode Exit fullscreen mode

If we check now

ll /test
total 0
-rw-r--r-- 1 s1   s1   0 Feb  9 09:54 A
-rw-r--r-- 1 s2   s2   0 Feb  9 09:58 B
-rw-r--r-- 1 root root 0 Feb  9 10:02 C
Enter fullscreen mode Exit fullscreen mode

👉 Important note that when I copied from Linux-1 I used 's1' username and 's2' username for A and B files respectively. That made the ownership to be for their respective file in Linux-2. Whereas when 'pulling' a file from Linux-2, it just makes the ownership for the file C to be 'root' as I worked as the root user in both servers

Top comments (0)