Using SSH Protocols to Work with Servers Part 2 - Linux Users
This is a continuation of the tutorial, for Linux users. You may see Using SSH Protocols to Work with Servers Part 1 - Windows Users for additional background info.
The usage of traditional services, like telnet and ftp, to work on or transfer content to servers should be avoided. The reason is that these services use plain text, and passwords and data can be stolen by miscreants using IP sniffing techniques. Instead, encrypted SSH protocols should be used for command line administration, and for transferring files.
We can use the command line utilities in the OpenSSH suite (ssh client for secure shell access, and sftp client for file transfers) and we can also use the GUI program, gFTP for secure file transfers.
Linux (or BSD) distributions should include all the tools you will need. They are probably even installed by default. However, if you do need to compile these programs (i.e. your distributor doesn't provide them) you can go to the official Web sites and grab the sources.
To connect to a shell account on a Unix server, open your favourite terminal (xterm, konsole, rxvt, eterm etc.) and type:
ssh -l username (ip address or hostname)
ssh -l grogan 192.168.0.2
ssh -l grogan servername.com
You are supplying the username in the command, but it will prompt for your password.
If connecting to the server for the first time with SSH, you will be prompted to accept the server's key. Type yes and hit return. It is not until you do that, that you are prompted for the password.
After you log on with your password, you will have the use of your shell on the remote system. It will be very much like working in a command console on your own machine.
This is just a Slackware box set up as a server on my local network, but it would be just the same for a remote server, except that you'd probably use the server's hostname or domain name instead of the IP address to connect. I could also use the hostname "nicetry" for that computer, as long as I have an entry in /etc/hosts for it. (There's no DNS).
After you are finished doing what you set out to do, type logout to terminate the session.
If you need to learn more about working at the command line, an excellent tutorial is Linux: Rute User's Tutorial and Exposition
For securely transferring files, the OpenSSH suite includes a very nice command line sftp client. To invoke it, type:
or, for example:
The address can be an IP address, or a hostname. It looks like an email address but it isn't, necessarily.
Before invoking the program, the easiest thing to do is to change into the directory you wish to work from. If you don't that's OK too, because you can still change the local working directory after you log on.
I cd into /home/grogan/sshhowto
I type the command to log with sftp and enter the password when prompted.
Once connected, commands like "ls" and "cd" operate on the remote server. If you wish to list or change directories on your local computer, use "lls" and "lcd" respectively. Most of those commands have a local equivalent... simply prefix them with the letter l. For example, to make a directory on the local machine type "lmkdir" instead of mkdir.
To transfer a file to the remote server, you use the put command. This program gives you full use of wildcards... it will operate on whatever files match. This concerns the "put" and "get" commands, as well as the "rm" command for deleting files. Be careful, wildcards are powerful.
For example, I want to transfer all the jpg images from sshhowto on the local computer, to sshhowto on the remote server. As you can see in the above screenshot, "put *.jpg" transfers all the jpg images in one command.
Unfortunately, this program will not transfer entire directories, so you must create directories if not already present, and transfer files into them.
While I'm connected, I also want to change to another directory on the local computer and upload a small ISO image to the remote system.
First I want to see where I am, so I issue the "pwd" and "lpwd" commands, and do directory listings on both the local machine and remote. Once the file transfer is started with the "put" command, progress is displayed, unlike some old fashioned command line ftp programs.
While I'm here, I also need to grab something from my fileserver.
I first change to the desired directory on the remote computer using the cd command, and do a directory listing to see my file.
I want to put the file in my home directory on the local computer, so I use the "lcd" command to change to /home/grogan.
I then use the get command, to download my firefox tarball.
When finished, type bye to terminate the session.
To see a more complete list of commands and options for working with the sftp program, type man sftp on your system. If you don't have the man page installed, type man sftp into Google and you will find the man page online
Secure File Transfers using the GUI, with gFTP
The gFTP program is provided by most distributions. If it isn't installed, check for a package in your distro's repository. It's probably there. Yes, it's an FTP program, but we are primarily interested in its SSH2 file transfer capabilities (sftp).
I highly recommend gFTP version 2.0.18 (or probably higher in future), because the interface to SSH has greatly improved in terms of usability. Older versions weren't as easy to configure to work with this. I'll show you what I mean, further down. At the time of writing, gFTP 2.0.18 is the current version.
While for our purposes, using SSH2 protocols, it isn't necessary to change anything in the options panel, when you first open the program, go to the FTP menu and choose Options.
Unless you need to see them, do yourself a favour and uncheck "Show Hidden Files" so you don't see all the hidden dot files (for program configuration) in your home directory. It's needless clutter and you can always change the setting if you need to have them available. I would also uncheck "preserve file permissions" because they may not always be appropriate to be publicly accessible and you may not realize it or think to change them. If you do not preserve permissions, the files should hopefully get created with sane defaults, provided the remote server has a properly configured "umask" setting.
Enter your hostname or IP address of the remote server you wish to connect to. Choose Port 22, or leave it blank and it will default to that for the chosen service. Enter your username and password in the appropriate fields, and choose SSH2 protocol from the drop list. Don't forget this step!
Click the Connect button (the icon that looks like two monitors on the far left) and by default you will be in your home directory in both the local pane on the left, and the remote pane on the right.
Browse to where you want to be on both the local and remote sides. To navigate, double click directories to enter them, or to go back up a level double click the two dots shown at the top of the directory listing. When you are all set, go to the Bookmarks menu and choose Add Bookmark.
Give it a name, and optionally check the box to save the password. This is NOT recommended if you are worried about someone tampering with your user profile. It will prompt for password on connect, if you don't enable it.
Now, if you have an older version of gFTP, it sucks to be you. Well not really, but you'll have to do a bit of configuration.
First of all, in Options on the SSH tab, you must specify the path to the sftp-server binary on the remote server. On redhat based servers, that's usually /usr/libexec/openssh but you will have to ask your system administrator if that's not the case. You can also try /usr/libexec
When you connect with SSH for the first time, you are prompted to accept the server's key. Guess what? gFTP 2.0.17 or earlier can't handle that and you must first connect with ssh at the command line and accept the key. gFTP 2.0.18 is able to prompt you to accept it.
Now, back to gFTP 2.0.18. To transfer files, simply select them and then click the appropriate arrow button to transfer them to the local or remote side. You may hold the shift or ctrl key to select multiple files.
You can transfer entire directories as well as files!
There is more info shown in the local and remote panes, if you use the horizontal scroll bar to see it. I widened the program to show the whole remote pane.
If you need to change permissions on a file or directory, simply right click on it, and choose Chmod from the menu.
See Here for a discussion of Unix permissions, but what you mainly need to know for Web hosting purposes is that directories must be "rwxr-xr-x" and files must be "rw-r--r--"
When you are finished, the Connect button at the upper left of the program also functions as the Disconnect button. Alternatively, go to the Remote menu, and choose Disconnect. Exit the program.
SSH File Transfers using KDE
Bobguy knows another way to do this, through the KDE GUI. Please see: