ftp:connect_with_sftp
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
ftp:connect_with_sftp [2016/10/18 14:26] – peter | ftp:connect_with_sftp [2019/11/29 14:03] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== FTP - Connect with SFTP ====== | ||
- | TODO | ||
- | |||
- | By default, SFTP uses the SSH protocol to authenticate and establish a secure connection. | ||
- | |||
- | Although passwords are easy to use and set up by default, we recommend you create SSH keys and transfer your public key to any system that you need to access. This is much more secure and can save you time in the long run. | ||
- | |||
- | Please see this guide to set up SSH keys in order to access your server if you have not done so already. | ||
- | |||
- | If you can connect to the machine using SSH, then you have completed all of the necessary requirements necessary to use SFTP to manage files. | ||
- | |||
- | Test SSH access with the following command: | ||
- | |||
- | <code bash> | ||
- | ssh username@remote_hostname_or_IP | ||
- | </ | ||
- | |||
- | If that works, exit back out by typing: | ||
- | |||
- | < | ||
- | exit | ||
- | </ | ||
- | |||
- | We can establish an SSH connection and then open up an SFTP session using that connection by issuing the following command: | ||
- | |||
- | < | ||
- | sftp username@remote_hostname_or_IP | ||
- | </ | ||
- | |||
- | You will connect the the remote system and your prompt will change to an SFTP prompt. | ||
- | |||
- | |||
- | ===== Getting Help in SFTP ===== | ||
- | |||
- | The most useful command to learn first is the help command. This gives you access to a summary of the SFTP help. You can call it by typing either of these in the prompt: | ||
- | |||
- | < | ||
- | help | ||
- | ? | ||
- | </ | ||
- | |||
- | This will display a list of the available commands: | ||
- | |||
- | < | ||
- | Available commands: | ||
- | bye Quit sftp | ||
- | cd path Change remote directory to ' | ||
- | chgrp grp path | ||
- | chmod mode path Change permissions of file ' | ||
- | chown own path | ||
- | df [-hi] [path] | ||
- | | ||
- | exit Quit sftp | ||
- | get [-Ppr] remote [local] | ||
- | help | ||
- | lcd path | ||
- | . . . | ||
- | </ | ||
- | |||
- | We will explore some of the commands you see in the following sections. | ||
- | |||
- | |||
- | ===== Navigating with SFTP ===== | ||
- | |||
- | We can navigate through the remote system' | ||
- | |||
- | First, let's orient ourselves by finding out which directory we are in currently on the remote system. Just like in a typical shell session, we can type the following to get the current directory: | ||
- | |||
- | pwd | ||
- | Remote working directory: / | ||
- | We can view the contents of the current directory of the remote system with another familiar command: | ||
- | |||
- | ls | ||
- | Summary.txt | ||
- | Note that the commands within the SFTP interface are not the normal shell commands and are not as feature-rich, | ||
- | |||
- | ls -la | ||
- | drwxr-xr-x | ||
- | drwxr-xr-x | ||
- | -rw------- | ||
- | -rw-r--r-- | ||
- | -rw-r--r-- | ||
- | drwx------ | ||
- | -rw-r--r-- | ||
- | . . . | ||
- | To get to another directory, we can issue this command: | ||
- | |||
- | cd testDirectory | ||
- | We can now traverse the remote file system, but what if we need to access our local file system? We can direct commands towards the local file system by preceding them with an " | ||
- | |||
- | All of the commands discussed so far have local equivalents. We can print the local working directory: | ||
- | |||
- | lpwd | ||
- | Local working directory: / | ||
- | We can list the contents of the current directory on the local machine: | ||
- | |||
- | lls | ||
- | Desktop local.txt test.html | ||
- | Documents analysis.rtf zebra.html | ||
- | We can also change the directory we wish to interact with on the local system: | ||
- | |||
- | lcd Desktop | ||
- | Transferring Files with SFTP | ||
- | Navigating the remote and local filesystems is of limited usefulness without being able to transfer files between the two. | ||
- | |||
- | |||
- | ===== Transferring Remote Files to the Local System ===== | ||
- | |||
- | If we would like download files from our remote host, we can do so by issuing the following command: | ||
- | |||
- | get remoteFile | ||
- | Fetching / | ||
- | / | ||
- | As you can see, by default, the " | ||
- | |||
- | We can copy the remote file to a different name by specifying the name afterwards: | ||
- | |||
- | get remoteFile localFile | ||
- | The " | ||
- | |||
- | get -r someDirectory | ||
- | We can tell SFTP to maintain the appropriate permissions and access times by using the " | ||
- | |||
- | get -Pr someDirectory | ||
- | Transferring Local Files to the Remote System | ||
- | |||
- | Transferring files to the remote system is just as easily accomplished by using the appropriately named " | ||
- | |||
- | put localFile | ||
- | Uploading localFile to / | ||
- | localFile | ||
- | The same flags that work with " | ||
- | |||
- | put -r localDirectory | ||
- | Note | ||
- | There is currently a bug in the versions of OpenSSH shipped with current Ubuntu releases (at least 14.04 to 15.10) that prevents the above command from operating correctly. Upon issuing the command above to transfer content to a server using the buggy version of OpenSSH, the following error will be given: Couldn' | ||
- | |||
- | To work around this issue, create the destination directory on the remote end first by typing mkdir localDirectory. Afterwards, the above command should complete without error. | ||
- | |||
- | One familiar tool that is useful when downloading and uploading files is the " | ||
- | |||
- | df -h | ||
- | Size | ||
- | 19.9GB | ||
- | Please note, that there is no local variation of this command, but we can get around that by issuing the " | ||
- | |||
- | The " | ||
- | |||
- | ! | ||
- | df -h | ||
- | Filesystem | ||
- | / | ||
- | devfs 181Ki 181Ki 0Bi | ||
- | map -hosts | ||
- | map auto_home | ||
- | Any other local command will work as expected. To return to your SFTP session, type: | ||
- | |||
- | exit | ||
- | You should now see the SFTP prompt return. | ||
- | |||
- | Simple File Manipulations with SFTP | ||
- | SFTP allows you to perform the type of basic file maintenance that is useful when working with file hierarchies. | ||
- | |||
- | For instance, you can change the owner of a file on the remote system with: | ||
- | |||
- | chown userID file | ||
- | Notice how, unlike the system " | ||
- | |||
- | An involved work around could be accomplished with: | ||
- | |||
- | get /etc/passwd | ||
- | !less passwd | ||
- | root: | ||
- | daemon: | ||
- | bin: | ||
- | sys: | ||
- | sync: | ||
- | games: | ||
- | man: | ||
- | . . . | ||
- | Notice how instead of giving the " | ||
- | |||
- | The UID will be in the third column of the file, as delineated by colon characters. | ||
- | |||
- | Similarly, we can change the group owner of a file with: | ||
- | |||
- | chgrp groupID file | ||
- | Again, there is no easy way to get a listing of the remote system' | ||
- | |||
- | get /etc/group | ||
- | !less group | ||
- | root:x:0: | ||
- | daemon:x:1: | ||
- | bin:x:2: | ||
- | sys:x:3: | ||
- | adm:x:4: | ||
- | tty:x:5: | ||
- | disk:x:6: | ||
- | lp:x:7: | ||
- | . . . | ||
- | The third column holds the ID of the group associated with name in the first column. This is what we are looking for. | ||
- | |||
- | Thankfully, the " | ||
- | |||
- | chmod 777 publicFile | ||
- | Changing mode on / | ||
- | There is no command for manipulating local file permissions, | ||
- | |||
- | That can be done with the " | ||
- | |||
- | lumask 022 | ||
- | Local umask: 022 | ||
- | Now all regular files downloaded (as long as the " | ||
- | |||
- | SFTP allows you to create directories on both local and remote systems with " | ||
- | |||
- | The rest of the file commands target only the remote filesystem: | ||
- | |||
- | ln | ||
- | rm | ||
- | rmdir | ||
- | These commands replicate the basic behavior of the shell versions. If you need to perform these actions on the local file system, remember that you can drop into a shell by issuing this command: | ||
- | |||
- | ! | ||
- | Or execute a single command on the local system by prefacing the command with " | ||
- | |||
- | !chmod 644 somefile | ||
- | When you are finished with your SFTP session, use " | ||
- | |||
- | bye | ||
- | |||
- | ===== Conclusion ===== | ||
- | |||
- | Although SFTP is a simple tool, it is very useful for administrating servers and transferring files between them. | ||
- | |||
- | If you are used to using FTP or SCP to accomplish your transfers, SFTP is a good way to leverage the strengths of both. While it is not appropriate for every situation, it is a flexible tool to have in your repertoire. | ||
- | |||
- | |||
- | ===== References ===== | ||
- | |||
- | https:// |
ftp/connect_with_sftp.1476800798.txt.gz · Last modified: 2020/07/15 09:30 (external edit)