Transferring files with the scp Command (Linux Crash Course Series)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[MUSIC] Hello again everyone and welcome back to Learn Linux TV. In today's video, what we're going to do is look into how you could go about transferring files from one server to another or point A to point B, using the SCP command which stands for secure copy. And SCP is a great solution for one-off file transfers. For example, if you want to copy a file or directory from your local workstation to a remote server or even vice versa, the SCP command can help you do exactly that. In fact, I'll show you some hands-on examples in this video that will teach you everything you need to know in order to start using it. For those of you that are more seasoned than others when it comes to your Linux skills, you might at least be aware of the fact that the rsync command exists, which is another command that does essentially the same thing. So you might be wondering when you should use rsync versus scp. Well, my basic rule of thumb is if you're intending on performing a one-off file transfer, then scp is probably the way to go. But for anything more advanced or complicated than that, you should probably take a look at rsync. Now I will be covering rsync in a future video in this series. So if you don't know how to use rsync, well, make sure you subscribe. And that way you'll be alerted as soon as I have that video out. But today we're going to go over scp. And before we go over that, I need to take a moment to mention the sponsor for this particular video, Akamai Connected Cloud. If you're looking for a cloud provider that's affordable, flexible, and reliable, then look no further than Akamai Connected Cloud. With Akamai's cloud platform, you can split up Linux servers quickly, and the platform contains all the features you'll need to deploy full featured solutions. And using the marketplace, you could easily deploy applications, such as Next Cloud, Rocket Chat, Mastodon, WordPress, Pihole, Plex, Jenkins, and many more. In fact, there's over 100 applications available in the marketplace. If you want to set up a custom Linux instance, you could do that too. Other popular Linux distributions are available on the platform, including, but not limited to, Debian, CentOS, Fedora, Ubuntu, and many others. In fact, even Arch Linux is available. So check out Akamai Connected Cloud with the URL that you see on the screen right now, which will do two things. First, it'll help support this channel, which I'll really appreciate. And it'll also get you $100 in starter credit to check out the platform. And thank you yet again to Akamai for sponsoring this video. Thank you so much to Akamai yet again for sponsoring today's video. I really appreciate it. And with all of that out of the way, let's dive into SCP. [SOUND] Now, normally what I would have you guys do is check and make sure whether you have the command that we're going over available on your Linux terminal. But chances are you probably already have this installed. In fact, what we could do is type command-v and then SCP just like that. And that'll help us confirm that SCP is available on our system. And in my case, it is. I see output right there, which confirms that SCP is present. Now, you shouldn't have to install anything to get SCP. I have yet to see a single Linux distribution that doesn't include it by default. Now, if you happen to be running a distribution of Linux that doesn't have this pre-installed, then let me know in the comments because I'm curious. I really don't think that any of you will have to install SCP. If you do, it's probably just a package that you'll have to install with your package manager, same as anything else. But anyway, we have SCP here on my system, so let's continue. Now, the first thing to know about SCP is that it uses SSH. That's the delivery mechanism through which files are transferred. That also means a prerequisite is having access to another server. I mean, you can technically use SCP locally from one local location to another, but that's probably a waste because in that case, you could, well, use SCP instead of SCP. But what we want to do is have access to a server, any server, and I'm going to use my development server as a test example here. We just want to make sure that we can SSH into the target server because that's a requirement. So what I'll do is type SSH and then the hostname for the server that I plan on using. Instead of the hostname, you could type the IP address. I've gone over all of this in my SSH client video, which I'll leave a card for it right about here if you're at all confused. But essentially what I'm doing is using SSH to log into a server named dev that exists on my network. We just need access to another Linux server. If I press enter, I can confirm that I'm indeed able to connect to it. In fact, as we can see right here, that server is currently running Debian 12. So in case you're curious about the server that I'm connecting to, well, here's some information for you. Anyway, what I've done is I've confirmed that I can indeed access that server. So I'll exit out and now I'm back to my local footage PC. And the next thing that we'll need for our first example is a file that we could transfer to another server. It doesn't matter what file we use, it could be any file. You could even create a file if you wanted to. Now here I have a custom SSHD config file. And what I plan on doing is copying that over to the other server. Let's just say, for example, I've been working with configuring that file and I want to move it over to the target server, a development server, and install it so I could test this custom config file. That's a good use for the SCP command, so let's do it. And here's where we're going to see the SCP command in action. So to use it, we'll type SCP. And then we'll type the name of the file that we want to transfer to the target. Now just use tab auto completion here to fill out the name for me. That's the config file right there that I've been working with. And what I want to do is copy it to dev, that's the name of the server. And where I want to put it on that server is in my home directory, which is at slash home slash j on that server. And what we're seeing right here is the very first example. We type SCP, the name of the file, the destination server name or IP address colon, and then the path at the target where we want that file to be copied to. So what I'll do is press enter and let's see what happens. And take a look at that, it worked. We have the file name listed right there for confirmation. It shows me that 100% of that file was copied to the server and it copied it at 3.9 megabytes a second. And since that file is very small, then it happened in, well, a fraction of that. Now if I log into that other server, And then I list the storage, we could see the file right there. It was successfully copied over. Now at this point, I could install this config file if I'm actually testing this custom config file and then test it on the server. But I'm not going to go through all of that. This is just a hypothetical example. The point here is that I was able to transfer this file from my local workstation to the remote server. And with that, I'll log out. And now I'm back to my local computer. Oh, sorry. I was just consulting my own book, actually. You know, that's something that I do from time to time because unlike what many of you might think, I don't memorize everything that I teach you guys. Sometimes I actually look at my own book if I want to remember how to do something that I don't do every day. But since I'm on the subject of this book, check it out. Mastering Ubuntu Server, 4th edition, written by yours truly. It's available right now. And I think you're going to love it. So if you're looking for an Ubuntu book to teach you everything that you need to know when it comes to managing real Ubuntu servers, check it out. So definitely check out the book. I would really appreciate it. Anyway, let's get back to the video. Now, again, I have the file right there that I copied to the target. So what I'm going to do right now to reset this example is remove that file. It's stored on my remote server, so I no longer need it here anyway. So I'll just delete it. And now it's gone. And for the next example, what I want to do is show you that the command works in reverse as well. In fact, this is the command that we used right here. So if I wanted to copy the SSHD config custom file from the remote server back to my local server, here's what we could do. We're going to make the target server and target path the very first thing here. And I'll also add the name of the file as well. I called it SSHD config custom. That's the file that I transferred to the other server. And what I want to do is copy it locally to slash home slash J here on my local workstation. So what I've done is I've just flipped it around. So I'll press enter. And if I list the storage, we could see that the file has returned. I was able to grab the file from the remote server and save it locally. Now you just saw me copy a file from a remote server to my local workstation. And that worked well because I already knew the file name. But what if I forgot the file name? Now one thing I could do is SSH into that server and then list the storage. And there we see the file name. So now I know what it is if I did forget it, but that's the longer way to do it. So what I'm going to do is give you a tip right now that's going to make this a lot easier. And this isn't specific to SCP. It's just a really cool trick to know when it comes to SSH. And this is something that you guys might know already, but if you didn't, well, you're going to love this one. So what I did was I used SSH to connect to the remote server. Now what you may or may not know is that you can also execute a one off command against the server. So if I add a command right here after the SSH command, watch what happens. It lists the storage and then it drops me back to my local computer. And that's a faster way to find out what's on the remote end. I didn't have to specify the home directory here because it defaulted to that. We see here that the file name is sshd_config_custom. And it's not just the ls command that you can use with SSH in this way. I mean, any other command that you can use on a target server is fair game. So essentially the trick is this. If you want to execute a one off command against a remote server, you don't have to establish an SSH connection and maintain that connection just to run that command. You could give the SSH command itself another command to run after the server name, just like I did here. And it's going to execute that command and take you back to your shell. So there's a freebie if you guys didn't already know about that. And if you want to learn even more awesome Linux tricks, just like this one, then I'll leave a card for a video right here that'll give you some Linux terminal tricks. You could watch that video and find out some really awesome things you could do on the Linux terminal. And I'll also leave a card for another video that gives you even more tricks that you could do with Linux right here. And you could check out that video and learn even more about Linux and some of the awesome things that you could do with it. Anyway, the point here was to find out the file name. And if I forgot the file name, then SSH and then the host name, followed by the ls command is a great way to find out what the name of the file is. So at this point, I know to give the scp command to transfer the file locally. Now for the next example, what I'm going to do is show you how you can simplify the scp command. So before I showed you that you could run scp, the name of a file that you want to transfer, the server's host name or IP address, followed by a colon, and then the path where you want that file to be copied to. And there's nothing wrong with this command. It works just fine. In fact, I copied it over again. We could see that the command works. But the thing is, we could simplify this a little bit. So what I'm going to do right now is show you how you could do exactly that. The first thing I'll do is take off the path. For the target directory, I had slash home slash J, my home directory on the other server, but I didn't need to include that because if the target is your user's home directory on the other server, you can leave off the home directory because the home directory is assumed. So if that's all you want to do is copy a file to your home directory on the other server, just leave the path out. You don't have to type it because that's the default anyway. Now, another thing I did here was leave off the username. Now what I should have done is specify the username on the other end. But since the username on the other end is my username, then I don't actually have to specify the username at all. Because if you don't specify a username with the SCP command, it's going to assume that the user on the remote end is the same as your username on the local machine. So that's another thing that I don't have to include here unless the username just so happens to be different on the other end. For example, you could have a username called admin on the other end. Since that's different than J in my case, I do have to declare that. If the user J doesn't exist on the other end and I leave that off, then it's going to assume my username again is J in my case and well, the command won't work. Now, going back to my point where you can leave out the home directory if that's where the file is going, if the target directory on the other end is a sub directory of your home directory, you also don't have to specify the home directory for that either. So let's say, for example, that I had a directory called documents on the remote end, just like this, and it was saved aside by home directory. Then in that case, I don't have to type the home directory for this either. I just type the colon and then the name of the directory that's right underneath my home directory and the command will still work. Now this particular command will not work because I don't have a directory named documents on the remote server. So that command is not going to do anything at all. But I just wanted you to be aware of the fact that if you did want to copy a file into a sub directory of your home directory, you can leave out the home directory because again, that is assumed. Now with each of the examples that I gave you guys so far, I used a file as an example, I transferred a file from my local computer here, my footage PC to my remote server, but the SCP command changes a little bit. If you are copying a directory to the remote server, if I list my storage here, I have a backups directory and let's say for example, I wanted to copy that entire directory over to the target server. So what's not going to work is typing SCP backups. That's the name of the directory, the name of the server in my case, and then colon that's not going to work. And here's why, because it's a directory. It even tells me right here that backups is not a regular file. So it prevented me from copying it to the remote server. Thankfully, the solution to this problem is very easy. All we have to do is add the option dash R to SCP, just like this, which stands for recursive. That means it's going to copy a directory and everything underneath it to the remote server. So essentially, if you are intending on copying a directory to the remote server, you do need to specify the fact that it is a directory and use the recursive option just like I'm doing here. And if I press enter for this here, let's see what happens. Now off camera, I created some temporary files just to use as an example. As you can see here, each of these files are zero bytes, so they contain absolutely nothing, but it's just an example. There were three files underneath the backups directory. So inside the directory that I copied, we have a date directory. Inside that date directory, we have those three files just like you see here. So let's take a look at the remote server and just see exactly what ended up happening. So back on the server here, we have the backups directory, the directory that I copied over. Inside there, I have, you guessed it, the same date folder. Inside that date folder, I have the same three files. So using the scp-r command, I was able to recursively copy a directory to the target. And I did that with this command right here. Now, another option that I want to give you guys is the -p option. And what I'll do is add it to my existing -r option. So that becomes -pr. That's how we can combine options in Linux if you didn't already know. Now what this is going to do is retain the modification date of the files that it copies over. By default, it doesn't do that. But if you wanted to maintain that information, the -p option is exactly how you do that. That stands for preserve. So I'll press enter. It did the exact same thing. However, if I check the date and then quickly log onto that server and then list the contents of the directory that I copied over, and I'll add the -l option here to make sure that I see the date of the files inside this folder. There we have that information. Now, if I scroll up real quick. The date when I executed the command right here was May 30th at 3.34. 19 seconds. And well, here we have a completely different time. That was over a half an hour ago. So what you're seeing here on the screen is the dates and the times for each of the files when they were created. That information was maintained because I used the -p option when I copied these files over to the target. Now, for the next example, what I'm going to do is show you the -p option. Now, this option is not going to be one that you'll use all that often, but it is going to be one that you'll be glad to know when the time comes. You will run into this if you haven't already. And specifically, what this allows you to do is type a different port number. By default, SSH listens on port 22. And since SCP uses SSH, it's going to also default to port 22. Now, as I taught you in previous videos, you can alter the port that SSH listens on. So, for example, if you changed it to something like 2222, and by the way, never use that port. It's just an example. A lot of people use 2222. But anyway, the point is if SSH is listening on a different port on the remote server, the -p option will allow you to specify that port and then the rest of the command will play out exactly the same way. So if I wanted to copy that particular file over to that server, then well, there you go. But the takeaway here is that if you run into another server that has SSH listening on a different nonstandard port, the -p option will allow you to specify that port. And well, there's our video. In this video, I showed you how to use the SCP command, which can come in quite handy when you want to transfer a file or even the contents of a directory from one location to another. Now, did you like this video? If this video has helped you out, then please consider clicking the like button to let YouTube know that it helped you out. And that'll help spread Linux learning to more people. And I would really appreciate that. Anyway, thank you so much for checking out this video. I appreciate it. And I'll see you in the next video.
Info
Channel: Learn Linux TV
Views: 24,348
Rating: undefined out of 5
Keywords: Linux, gnu/linux, LearnLinuxTV, Learn Linux TV, LearnLinux.TV, Learn Linux, Linux Training, Linux Tutorials, Linux Commands, scp, scp command, secure copy, file transfer, linux server, devops, devops training, devops tutorial, linux tutorial, linux tutorial for beginners, Devops Training, Linux Training Course, Linux Explained, transfer files on linux with this one easy trick, ssh, linux essential commands, linux essentials, linux boot camp
Id: Aa7tKMmeFZI
Channel Id: undefined
Length: 20min 2sec (1202 seconds)
Published: Tue Jun 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.