Now in our first demonstration, we're going to run through looking at remote
copy operations using scp and rsync. Now to begin this,
we are working on the Ubuntu system.
This is going to be our remote server. And we're going to create ourselves a
user account to be able to use, so sudo and useradd ‑m to create the home
directory, and we're going to create a user called user1.
We need to set a password.
Remember, we had set the SSH daemon to allow password‑based
authentication, so we've got sudo and passwd to set the
password for the user account, user1. We were prompted twice for the password.
I'm just setting a relatively simple password.
Obviously, you can set whatever you need within your own environment,
but that's the user account now up and running with.
If I move back onto my Alma Linux system,
I can then use scp, our secure copy program, so scp, secure
copy. I've got the file1 that I was editing earlier, and I can
copy it through to user1@, and then the IP address of my Ubuntu
system is 192.168.33.13.
Now if you use the same Vagrant file that I've supplied when setting up,
you'll have that same IP address. It does need to be an IP address that's
on a private network for ease of communication in this way. If we put a
colon at the end, that's then just specifying that that is the server,
and then we can specify the path.
If I leave the path blank,
it's going to be copied across to user1's home directory.
The first time we connect, we have to accept the
public key of the Ubuntu system.
Now I need to authenticate as user1, so this is the
password authentication that we had enabled,
so add in the password that we set, and the file's being copied across.
Now whilst that is good,
it's probably not the same as we want to copy across heaps of files, and there
is a tool that is really good at that, and that is where rsync comes in. Now I
did install it earlier, but you didn't. Just make sure you do your sudo and dnf
install rsync. I'm going to create myself a directory, so I'm just going to go
mkdir, and let's call it docs. I can then go through and use the find command.
Let's move this to the top of the screen, so Ctrl+L. Find from the
/usr/share/doc directory where the ‑name and then in quotes "*.html". If I just
look at that,
the output will be printed to the screen, so the default option is actually
‑print. I can change the default option to ‑exec command. We want to then
go through and execute copy. I want to copy these files that we find. We
have a placeholder for our file name.
I want to copy the found file through to the docs directory. I can
reference it just as docs because it's in my current directory. And
then there's a little bit of a funny thing here, we have to escape
with the backslash, the semicolon.
The semicolon is our end‑of‑line operator.
We want to make sure that it's not picked up by the Bash
shell, but passed through to find.
Now if I go through and list the docs directory, we'll be able to see
I've got those HTML files, and I'm going to use the rsync command, and
we're going to copy with our archive mode.
That means we automatically recurse as well for both so we can see what's going
on, ‑ave for external program, and we're going to use ssh as our external
program, so we can sync with the remote system. And then I can go through and
add in the docs directory that's on my local system, and then that's going
through. And if I use escape and dot several times, you can see that I'm
scrolling through the arguments. But I've got back to user1@192.168.33.13. So
just escape and dot allows you to cycle through your arguments. Hit the Enter
key, authenticate, and then you can see that we copy the contents of the
directory.
If I go through and execute again, sure enough,
it's annoyingly prompting me for my password, but I can use
key‑based authentication to overcome this.
There are no changes locally,
so there's nothing to synchronize. If I go through though and add a
new file using the touch command into my docs directory, so touch
docs and file1, now as I go through,
it's just file1 that needs to be sent across. So rsync is a really useful tool to keep systems up to date.