SSH and SCP: Howto, Tips & Tricks: RSS Feeds
SSH and SCP: Howto, Tips & Tricks: RSS Feeds
http://www.linuxtutorialblog.com/post/ssh-and-scp...
RSS Feeds
SSH
SSH is some kind of an abbreviation of Secure SHell. It is a protocol that allows secure connections between computers. In this tutorial, we'll be dealing with the ssh command on Linux, the OpenSSH version. Most Linux distributions feature the OpenSSH client today, but if you want to be sure, have a look at the SSH manpage on your system. You can do this by typing: [rechosen@localhost ~]$ man ssh Note: this should be done in a terminal. This tutorial assumes that you have some basic terminal knowledge, like knowing how to start a terminal session on your system and being familiar with the basic commands and syntaxes. If it displays something like this NAME ssh - OpenSSH SSH client (remote login program) then you can be quite sure you're running the OpenSSH version. For more background information about SSH, see http://en.wikipedia.org/wiki/SSH.
1 de 21 26/02/11 23:36
http://www.linuxtutorialblog.com/post/ssh-and-scp...
Specifying a username
It's actually quite simple to specify a different username. You might even already be familiar with it. See the following example: [rechosen@localhost ~]$ ssh yourusername@yourserver The above will make ssh try to connect with the username "yourusername" instead of (in my case) rechosen. This syntax is also used by a lot of other protocols, so it'll always come in handy to know it. By the way, you will still be
2 de 21
26/02/11 23:36
http://www.linuxtutorialblog.com/post/ssh-and-scp...
asked for a password. For security reasons, it is not even possible to directly specify the password in the syntax. You will always be asked interactively, unless you start configuring the server in an advanced way (which is exactly why that topic is out of this tutorials scope: this tutorial documents how to use the clients, not how to configure the server).
Specifying a port
There are many reasons to move the ssh service to an other port. One of them is avoiding brute-force login attempts. Certain hackers try to get access to ssh servers by trying a lot of common usernames with common passwords (think of a user "john" with password "doe"). Although it is very unlikely that these hackers will ever get access to the system, there is an other aspect of the brute-force attacks that you'll generally want to avoid: the system and connection load. The brute-force attacks usually are done with dozens or even thousands of tries a second, and this unnecessarily slows down the server and takes some bandwidth which could've been used a lot better. By changing the port to a non-default one, the scripts of the hackers will just be refused and most of the bandwidth will be saved. As the ssh command can't just guess the port, we will have to specify it if it's not the default 22 one. You can do that this way: [rechosen@localhost ~]$ ssh -p yourport yourusername@yourserver Of course, you will have to replace "yourport" with the port number. These is an important difference between ssh and scp on this point. I'll explain it further on.
3 de 21
26/02/11 23:36
http://www.linuxtutorialblog.com/post/ssh-and-scp...
What happened? Bash (the program behind your shell) tried to interpret the command you wanted to give ssh. This fails because there are exclamation marks in the command, which bash will interpret as special characters that should initiate a bash function. But we don't want this, we just want bash to give the command to ssh! Well, there's a very simple way to tell bash not to worry about the contents of the command but just pass it on to ssh already: wrapping it in single quotes. Have a look at this: [rechosen@localhost ~]$ ssh yourusername@yourserver 'wall "Hey, I just found out something great! Have a look at www.examplenewslink.com!"' The single quotes prevent bash from trying to interpret the command, so ssh receives it unmodified and can send it to the server as it should. Don't forget that the single quotes should be around the whole command, not anywhere else.
SCP
The scp command allows you to copy files over ssh connections. This is pretty useful if you want to transport files between computers, for example to backup something. The scp command uses the ssh command and they are very much alike. However, there are some important differences. The scp command can be used in three* ways: to copy from a (remote) server to your computer, to copy from your computer to a (remote) server, and to copy from a (remote) server to another (remote) server. In the third case, the data is transferred directly between the servers; your own computer will only tell the servers what to do. These options are very useful for a lot of things that require files to be transferred, so let's have a look at the syntax of this command: [rechosen@localhost ~]$ scp examplefile yourusername@yourserver:/home/yourusername/ Looks quite familiar, right? But there are differences. The command above will transfer the file "examplefile" to the directory "/home/yourusername/" at the server "yourserver", trying to get ssh acces with the username "yourusername". That's quite a lot information, but scp really needs it all. Well, almost all of it. You could leave out the "yourusername@" in front of "yourserver", but only if you want to login on the server with your current username on your own computer. Let's have a closer look at the end of the command. There's a colon over there, with a directory after it. Just like Linux's normal cp command, scp will need to know both the source file(s) and the target directory (or file). For remote hosts, the file(s)/directory are given to the scp command is this way. You can also copy a file (or multiple files) from the (remote) server to your own computer. Let's have a look at an example of that: [rechosen@localhost ~]$ scp yourusername@yourserver:/home /yourusername/examplefile . Note: The dot at the end means the current local directory. This is a handy trick
4 de 21 26/02/11 23:36
http://www.linuxtutorialblog.com/post/ssh-and-scp...
that can be used about everywhere in Linux. Besides a single dot, you can also type a double dot ( .. ), which is the parent directory of the current directory. This will copy the file "/home/yourusername/examplefile" to the current directory on your own computer, provided that the username and password are correct and that the file actually exists. You probably already guessed that the following command copies a file from a (remote) server to another (remote) server: [rechosen@localhost ~]$ scp yourusername@yourserver:/home /yourusername/examplefile yourusername2@yourserver2:/home /yourusername2/ Please note that, to make the above command work, the servers must be able to reach each other, as the data will be transferred directly between them. If the servers somehow can't reach each other (for example, if port 22 is not open on one of the sides) you won't be able to copy anything. In that case, copy the files to your own computer first, then to the other host. Or make the servers able to reach each other (for example by opening the port). Well, those are the main uses of scp. We'll now go a bit more in-depth about the differences between ssh and scp. *: Actually you can also use it just like the normal cp command, withhout any ssh connections in it, but that's quite useless. It requires you to type an extra 's' =).
http://www.linuxtutorialblog.com/post/ssh-and-scp...
6 de 21
26/02/11 23:36
http://www.linuxtutorialblog.com/post/ssh-and-scp...
The bandwidth is specified in Kbit/sec. What does this mean? Eight bits is one byte. If you want to copy no faster than 10 Kbyte/sec, set the limit to 80. If you want to copy no faster than 80 Kbyte/sec, set the limit to 640. Get it? You should set the limit to eight times the maximum Kbyte/sec you want it to be. I'd recommend to set the -l option with all scp'ing you do on a connection that other people need to use, too. A big amount of copying can virtually block a whole 10 Mbit network if you're using hubs.
Final Words
Well, that was it! I hope you learned a lot. Of course, you can always have a quick look at this tutorial again if you forgot something. Please tell other people who might be interested about this tutorial, you'll help this blog to grow if you do =). Thank you for reading and have a lot of fun with your new knowledge! If you enjoyed this article please consider sharing it!
vilfred February 23, 2007 at 21:00 Topic: thanks! thanks for howto =) Reply
Hasan Mahmud Riyadf March 16, 2007 at 08:33 Topic: Linux Scp Command Hello, yeh it is a well appriciated work. Thanks to share knowledgr with us. Long live GNU long live linux association Reply
n2j3 March 28, 2007 at 23:56 Topic: scp very helpful. didn't know you could do that . thanks :D
7 de 21
26/02/11 23:36