Difference between revisions of "Tutorial6: File Transfer / Sending Email Messages"

From CDOT Wiki
Jump to: navigation, search
(INVESTIGATION 1: USING SECURE COPY)
(INVESTIGATION 1: USING SECURE COPY)
Line 168: Line 168:
 
   </tr>
 
   </tr>
 
</table><br><br>
 
</table><br><br>
<ol><li value="3">When you have issue the above ssh command, enter your password to login.<br><br></li><li>Issue the following Linux commands to create the following directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir ~/remote</span><br><br></li><li>Change to the <b>~/remote</b> directory and confirm that you have changed to that directory.<br><br></li><li>Use a text editor to create a text file called <b>myfile.txt</b><br><br></li><li>Enter the following two lines displayed below in your editing session:<br><span style="font-family:courier;font-weight:bold;">This is my file<br>It is a small file</span><br><br></li><li><b>Save</b> editing changes to your ''myfile.txt'' file and exit your text editor.<br><br></li><li>Let's run a <b>shell script</b> to check that you created the correct directories<br>and that you created the <b>myfile.txt</b> file (with correct file contents) in the <b>~/from</b> directory<br>before using the ''scp'' Linux command.<br><br></li></li>Enter the following command: <span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts/week6-check-1</span><br><br></li><li>If you encounter errors, make corrections and then re-run the checking script until you receive<br>a congratulations message, and proceed to the next step.<br><br><b>NOTE:</b> We will now learn to transfer files between your <b>(local) home computer</b> and your <b>(remote) Matrix Linux server</b>.<br><br></li><li>'''Exit your Matrix ssh session''', but '''remain in the OS terminal on your <u>home</u> computer'''.<br><br></li><li>The <b>mkdir</b> command works with <i>MS Windows/UNIX/Linux/MacOSx computers</i>.<br>Issue the following command on your <u>local</u> computer to create a directory called <b>local</b>: <span style="color:blue;font-family:courier;font-weight:bold;">mkdir local</span><br><br></li><li>The <b>cd</b> command works with <i>MS Windows/UNIX/Linux/MacOSx computers</i>.<br>Issue the following command on your <u>local</u> computer to change to the <b>local</b> directory: <span style="color:blue;font-family:courier;font-weight:bold;">cd local</span><br><br></li><li>If you are using MS Windows on your local computer, issue the <span style="color:blue;font-weight:bold;font-family:courier;">dir</span> command to confirm you are<br>in the ''local'' directory; otherwise, use the <span style="color:blue;font-weight:bold;font-family:courier;">ls -ld</span> command.<br><br></li> <li>If you are in MS Windows, open the <span style="color:blue;font-family:courier;font-weight:bold;">NotePad</span> application to create a text file.<br><br><b>NOTE:</b> if you are using a Graphical Linux or MacOSx OS, then use a text editor like <b>vi</b><br>.</li><li>Enter a few lines of text, and if using '''Notepad''', then click on the <b>File</b> menu and select <b>save as</b><br>(save as the filename <b>other.txt</b> in your <b>local</b> directory) and then <b>exit</b> the <i>Notepad</i> text editor.<br><br>Otherwise, if using another text editor, save your editing session and exit the text editor.<br><br></li><li>If your OS is MS Windows issue the <span style="color:blue;font-family:courier;font-weight:bold;">dir</span> Windows command to view the contents of your current directory<br>(otherwise, issue the <span style="color:blue;font-family:courier;font-weight:bold;">ls</span> command for other operating systems).<br><br>We will now use the scp command to copy the <u>local</u> file called '''other.txt''' to your home directory on your <u>remote</u> Matrix Linux server.<br><br></li><li>Issue the following Linux command to copy the <b>other.txt</b> file from your local machine to your remote Matrix server<br>(replace yoursenecaid is <u>YOUR</u> Seneca ID and '''ADD A COLON : TO THE END OF THE COMMAND'''):<br><span style="color:blue;font-weight:bold;font-family:courier;">scp other.txt yoursenecaid@matrix.senecacollege.ca:</span><br><br></li><li>When prompted, enter your Matrix '''password'''.<br><br><b>TIP:</b> You can issue the ssh command, followed by a command that will be run on your remote computer,<br>but displayed on your local computer <b><u>without</u></b> having to establish a continuous connection to your remote Matrix server.<br><br></li><li>Issue the following command (using your matrix username): <span style="color:blue;font-weight:bold;font-family:courier;">ssh yoursenecaid@matrix.senecacollege.ca ls -l</span><br><br></li><li>When prompted, enter your password and press ENTER.<br><br>Do you see detailed information regarding the file called <b>output.txt</b>?<br>That command was run remotely on your Matrix server as confirmation that you securely copied that file<br>to the home directory of the Matrix server.<br><br></li><li>Use the '''Notepad''' application (or vi for other OS types) to create a text file called '''mytextfile.txt''' and save in the '''local''' directory of your computer.<br><br></li>
+
<ol><li value="3">When you have issue the above ssh command, enter your password to login.<br><br></li><li>Issue the following Linux commands to create the following directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir ~/remote</span><br><br></li><li>Change to the <b>~/remote</b> directory and confirm that you have changed to that directory.<br><br></li><li>Use a text editor to create a text file called <b>myfile.txt</b><br><br></li><li>Enter the following two lines displayed below in your editing session:<br><span style="font-family:courier;font-weight:bold;">This is my file<br>It is a small file</span><br><br></li><li><b>Save</b> editing changes to your ''myfile.txt'' file and exit your text editor.<br><br></li><li>Let's run a <b>shell script</b> to check that you created the correct directories<br>and that you created the <b>myfile.txt</b> file (with correct file contents) in the <b>~/from</b> directory<br>before using the ''scp'' Linux command.<br><br></li></li>Enter the following command: <span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts/week6-check-1</span><br><br></li><li>If you encounter errors, make corrections and then re-run the checking script until you receive<br>a congratulations message, and proceed to the next step.<br><br><b>NOTE:</b> We will now learn to transfer files between your <b>(local) home computer</b> and your <b>(remote) Matrix Linux server</b>.<br><br></li><li>'''Exit your Matrix ssh session''', but '''remain in the OS terminal on your <u>home</u> computer'''.<br><br></li><li>The <b>mkdir</b> command works with <i>MS Windows/UNIX/Linux/MacOSx computers</i>.<br>Issue the following command on your <u>local</u> computer to create a directory called <b>local</b>: <span style="color:blue;font-family:courier;font-weight:bold;">mkdir local</span><br><br></li><li>The <b>cd</b> command works with <i>MS Windows/UNIX/Linux/MacOSx computers</i>.<br>Issue the following command on your <u>local</u> computer to change to the <b>local</b> directory: <span style="color:blue;font-family:courier;font-weight:bold;">cd local</span><br><br></li><li>If you are using MS Windows on your local computer, issue the <span style="color:blue;font-weight:bold;font-family:courier;">dir</span> command to confirm you are<br>in the ''local'' directory; otherwise, use the <span style="color:blue;font-weight:bold;font-family:courier;">ls -ld</span> command.<br><br></li> <li>If you are in MS Windows, open the <span style="color:blue;font-family:courier;font-weight:bold;">NotePad</span> application to create a text file.<br><br><b>NOTE:</b> if you are using a Graphical Linux or MacOSx OS, then use a text editor like <b>vi</b><br>.</li><li>Enter a few lines of text, and if using '''Notepad''', then click on the <b>File</b> menu and select <b>save as</b><br>(save as the filename <b>other.txt</b> in your <b>local</b> directory) and then <b>exit</b> the <i>Notepad</i> text editor.<br><br>Otherwise, if using another text editor, save your editing session and exit the text editor.<br><br></li><li>If your OS is MS Windows issue the <span style="color:blue;font-family:courier;font-weight:bold;">dir</span> Windows command to view the contents of your current directory<br>(otherwise, issue the <span style="color:blue;font-family:courier;font-weight:bold;">ls</span> command for other operating systems).<br><br>We will now use the scp command to copy the <u>local</u> file called '''other.txt''' to your home directory on your <u>remote</u> Matrix Linux server.<br><br></li><li>Issue the following Linux command to copy the <b>other.txt</b> file from your local machine to your remote Matrix server<br>(replace yoursenecaid is <u>YOUR</u> Seneca ID and '''ADD A COLON : TO THE END OF THE COMMAND'''):<br><span style="color:blue;font-weight:bold;font-family:courier;">scp other.txt yoursenecaid@matrix.senecacollege.ca:</span><br><br></li><li>When prompted, enter your Matrix '''password'''.<br><br><b>TIP:</b> You can issue the ssh command, followed by a command that will be run on your remote computer,<br>but displayed on your local computer <b><u>without</u></b> having to establish a continuous connection to your remote Matrix server.<br><br></li><li>Issue the following command (using your matrix username): <span style="color:blue;font-weight:bold;font-family:courier;">ssh yoursenecaid@matrix.senecacollege.ca ls -l other.txt</span><br><br></li><li>When prompted, enter your password and press ENTER.<br><br>Do you see detailed information regarding the file called <b>output.txt</b>?<br>That command was run remotely on your Matrix server as confirmation that you securely copied that file<br>to the home directory of the Matrix server.<br><br></li><li>Use the '''Notepad''' application (or vi for other OS types) to create a text file called '''mytextfile.txt''' and save in the '''local''' directory of your computer.<br><br></li>
 
<li>Issue the following Linux command to copy the <b>mytextfile.txt</b> file from your local machine to your remote Matrix server<br>(replace yoursenecaid is <u>YOUR</u> Seneca ID and LEAVE <u>OUT</u> THE : at the end of the command!):<br><span style="color:blue;font-weight:bold;font-family:courier;">scp mytextfile.txt yoursenecaid@matrix.senecacollege.ca</span><br><br></li><li>When prompted, enter your Matrix '''password'''.<br><br></li><li>Issue the following command (using your matrix username): <span style="color:blue;font-weight:bold;font-family:courier;">ssh yoursenecaid@matrix.senecacollege.ca ls -l</span><br><br></li><li>When prompted, enter your password and press ENTER.<br><br>Do you see the file? That is because the COLON was NOT added to the end of the command!<br>Therefore, you MUST remember to include the COLON : at the end of the hostname, or it will NOT properly remotely copy the file!<br><br></li><li>Issue the following command to copy that same file to the ~/remote directory in Matrix:<br><span style="color:blue;font-weight:bold;font-family:courier;">scp other.txt yoursenecaid@matrix.senecacollege.ca:remote</span><br><br></li><li>Issue the following command to confirm that it was remotely copied to your ~/remote directory in Matrix:<br><span style="color:blue;font-weight:bold;font-family:courier;">ssh yoursenecaid@matrix.senecacollege.ca ls -l /home/yoursenecaid/remote</span><br><br>Do you see the output for the detailed file listing of <b>output.txt</b>?<br>What does  this indicate?<br><br><li>Issue the following command to copy that same file to the ~/remote directory in Matrix as a different filename:<br><span style="color:blue;font-weight:bold;font-family:courier;">scp other.txt yoursenecaid@matrix.senecacollege.ca:remote/different.txt</span><br><br></li><li>Issue the following command to confirm that the file was remotely copied to your ~/remote directory in Matrix with a different filename:<br><span style="color:blue;font-weight:bold;font-family:courier;">ssh yoursenecaid@matrix.senecacollege.ca ls -l /home/yoursenecaid/remote/different.txt</span><br><br>What does  this indicate?<br><br>Let's issue a checking script remotely to see that you properly copied that file from your<br>local computer to your remote Linux server to both your <b>home</b> directory and <b>~/remote</b> directory.<br><br></li><li>Issue the following:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts/week6-check-2</span><br><br>If you encounter errors, re-run the scp commands to correct and re-run the above command until you receive a congratulations message.<br><br></li></ol>
 
<li>Issue the following Linux command to copy the <b>mytextfile.txt</b> file from your local machine to your remote Matrix server<br>(replace yoursenecaid is <u>YOUR</u> Seneca ID and LEAVE <u>OUT</u> THE : at the end of the command!):<br><span style="color:blue;font-weight:bold;font-family:courier;">scp mytextfile.txt yoursenecaid@matrix.senecacollege.ca</span><br><br></li><li>When prompted, enter your Matrix '''password'''.<br><br></li><li>Issue the following command (using your matrix username): <span style="color:blue;font-weight:bold;font-family:courier;">ssh yoursenecaid@matrix.senecacollege.ca ls -l</span><br><br></li><li>When prompted, enter your password and press ENTER.<br><br>Do you see the file? That is because the COLON was NOT added to the end of the command!<br>Therefore, you MUST remember to include the COLON : at the end of the hostname, or it will NOT properly remotely copy the file!<br><br></li><li>Issue the following command to copy that same file to the ~/remote directory in Matrix:<br><span style="color:blue;font-weight:bold;font-family:courier;">scp other.txt yoursenecaid@matrix.senecacollege.ca:remote</span><br><br></li><li>Issue the following command to confirm that it was remotely copied to your ~/remote directory in Matrix:<br><span style="color:blue;font-weight:bold;font-family:courier;">ssh yoursenecaid@matrix.senecacollege.ca ls -l /home/yoursenecaid/remote</span><br><br>Do you see the output for the detailed file listing of <b>output.txt</b>?<br>What does  this indicate?<br><br><li>Issue the following command to copy that same file to the ~/remote directory in Matrix as a different filename:<br><span style="color:blue;font-weight:bold;font-family:courier;">scp other.txt yoursenecaid@matrix.senecacollege.ca:remote/different.txt</span><br><br></li><li>Issue the following command to confirm that the file was remotely copied to your ~/remote directory in Matrix with a different filename:<br><span style="color:blue;font-weight:bold;font-family:courier;">ssh yoursenecaid@matrix.senecacollege.ca ls -l /home/yoursenecaid/remote/different.txt</span><br><br>What does  this indicate?<br><br>Let's issue a checking script remotely to see that you properly copied that file from your<br>local computer to your remote Linux server to both your <b>home</b> directory and <b>~/remote</b> directory.<br><br></li><li>Issue the following:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts/week6-check-2</span><br><br>If you encounter errors, re-run the scp commands to correct and re-run the above command until you receive a congratulations message.<br><br></li></ol>
  

Revision as of 09:07, 26 January 2021

TRANSFERRING FILES BETWEEN COMPUTERS


Main Objectives of this Practice Tutorial

  • List common utilities contained in the ssh application framework
  • Securely copy files between Unix/Linux servers using the scp command
  • Securely transfer copies of files between Unix/Linux servers using the sftp command
  • Use the mail command to send email messages to your Seneca email account
  • Use the mail command to send email with file attachments to your Seneca email account


Tutorial Reference Material

Course Notes
Definitions / Commands
YouTube Videos
Course Notes: Definitions: File Transfer Commands: Instructional Videos:

KEY CONCEPTS

The ssh Linux command is a suite of tools to allow the user to issue commands
securely between Unix / Linux servers, as well as copy and transfer files among Unix/Linux servers.

In this tutorial, you will learn several different methods to securely transfer files from your Matrix Linux account
to other computers using Linux commands including scp, sftp and mail.


Issuing Commands on Remote Unix/Linux Servers

You can use the ssh command to issue Unix/Linux commands on a remote server
without logging in to a remote session.

Example:

ssh username@matrix.senecacollege.ca ls

You will be prompted for your Matrix account password, then the contents of your home directory
in your remote Matrix account will be displayed, although you will remain on your local computer.

Secure Copy (scp)

The scp Unix/Linux command is used to securely copy files between Unix/Linux servers.

Secure copy protocol (scp) is a means of securely transferring computer files between a local host and a remote host or between two remote hosts. It is based on the Secure Shell (SSH) protocol. "SCP" commonly refers to both the Secure Copy Protocol and the program itself.

Reference: https://en.wikipedia.org/wiki/Secure_copy


The usage for the scp command is similar to the cp command with the addition of host names, for example:


scp local.file username@host:destination-pathname
scp local.file username@host:
scp local.file host:

The most common mistake that students make is forgetting to add
the colon character ":" after the remote hostname.

The user name in the command can be omitted if it's the same as on the local host.

Multiple file and recursive directory copy (i.e. option -R) is supported.

Secure File Transmission Control Protocol (sftp)

The sftp Unix/Linux command is used to securely transfer (copy) files between Unix/Linux servers.

sftp is a file transfer program, similar to ftp, which performs all operations over an encrypted ssh transport. It may also use many features of ssh, such as public key authentication and compression.

Reference: http://man7.org/linux/man-pages/man1/sftp.1.html

FTP is an acronym for File Transfer Protocol which provides a set of rules on how to convert data that is transferred between computer servers (both identical and different operating systems).

The usage for the sftp command is displayed below:

sftp username@hostname

When you login via the sftp command, the sftp prompt appears. The ftp prompt is like a Bash shell prompt, but with a limited number of commands. When issuing sftp commands, it is important to understand that the local server relates to the server where you first issued the sftp command.

Below is a table that provide basic sftp commands to perform file transfer and management commands.
Some of those commands loosely resemble Unix/Linux commands.
Commands for the local computer has a lowercase l (not a number) for the command.


Operationsftp Command
(Local Server)
sftp Command
(Remote Server)
Display current working directorylpwdpwd
Display directory contentsllsls
Create Directorylmkdirmkdir
Change directory locationlcdcd
Upload file to remote serverput-
Download file to local serverget-
Graphical SFTP application.



Graphical SFTP Applications

Although it is important to learn how to use command-line sftp and is considered coverage
for quizzes, midterm and final exam, there are graphical sftp applications.

If you installed the graphical Secure Shell application in TUTORIAL 1 INVESTIGATION 1,
you can run a graphical application from your computer desktop.

Sending Emails with File Attachment (mail)

mail is a command-line email client for Unix and Unix-like operating systems.

Reference: https://en.wikipedia.org/wiki/Mail_(Unix)

The mail command allows users to send and receive email messages. (Image licensed under cc)

Sending a Simple Email Message:

  1. Type: mail username@hostname and press ENTER
  2. Enter subject line and press ENTER
  3. Type the body of the message and then when finished, press ctrl+d to send message

Sending an Email Message with a File Attachment:

  1. Type: mail username@hostname -a filepathname and press ENTER
  2. Enter subject line and press ENTER
  3. Type the body of the message and then when finished, press ctrl+d to send message

Alternative Method of Sending an Email Message with a File Attachment:

  1. Type: mail -s "your subject line" username@hostname < filenamepathname
  2. Press ENTER to send

    NOTE: You would have to use this method since you have used stdin redirection
    to attach the file’s so you can’t input the subject line from the terminal!

INVESTIGATION 1: USING SECURE COPY


In this section, you will learn how to use the scp command to securely copy files between your computer
and your Matrix Linux server. You will also learn how to issue the ssh command to run a command on your
remote Matrix server while remaining on your local computer.


Perform the Following Steps:

NOTE: You are REQUIRED to open a terminal in yours OS (i.e. NOT a graphical SSH application) for this tutorial.

  1. Determine which operating system that your computer is using.

  2. Try connecting to your Matrix account using the instructions in the table below based on your current operating system.

Newer Version of Windows 10: MacOSX: Linux:
  • From the start menu, type cmd and launch program
  • In the command terminal, enter the following command:
    ssh senecausername@matrix.senecacollege.ca
  • Click Launchpad icon, type terminal
    and press ENTER
  • In the terminal, enter the following command:
    ssh senecausername@matrix.senecacollege.ca
  • From the menu, choose:
    Applications > System Tools > Terminal
  • In the terminal, enter the following command:
    ssh senecausername@matrix.senecacollege.ca


  1. When you have issue the above ssh command, enter your password to login.

  2. Issue the following Linux commands to create the following directory:
    mkdir ~/remote

  3. Change to the ~/remote directory and confirm that you have changed to that directory.

  4. Use a text editor to create a text file called myfile.txt

  5. Enter the following two lines displayed below in your editing session:
    This is my file
    It is a small file


  6. Save editing changes to your myfile.txt file and exit your text editor.

  7. Let's run a shell script to check that you created the correct directories
    and that you created the myfile.txt file (with correct file contents) in the ~/from directory
    before using the scp Linux command.

  8. Enter the following command: bash /home/murray.saul/scripts/week6-check-1

  9. If you encounter errors, make corrections and then re-run the checking script until you receive
    a congratulations message, and proceed to the next step.

    NOTE: We will now learn to transfer files between your (local) home computer and your (remote) Matrix Linux server.

  10. Exit your Matrix ssh session, but remain in the OS terminal on your home computer.

  11. The mkdir command works with MS Windows/UNIX/Linux/MacOSx computers.
    Issue the following command on your local computer to create a directory called local: mkdir local

  12. The cd command works with MS Windows/UNIX/Linux/MacOSx computers.
    Issue the following command on your local computer to change to the local directory: cd local

  13. If you are using MS Windows on your local computer, issue the dir command to confirm you are
    in the local directory; otherwise, use the ls -ld command.

  14. If you are in MS Windows, open the NotePad application to create a text file.

    NOTE: if you are using a Graphical Linux or MacOSx OS, then use a text editor like vi
    .
  15. Enter a few lines of text, and if using Notepad, then click on the File menu and select save as
    (save as the filename other.txt in your local directory) and then exit the Notepad text editor.

    Otherwise, if using another text editor, save your editing session and exit the text editor.

  16. If your OS is MS Windows issue the dir Windows command to view the contents of your current directory
    (otherwise, issue the ls command for other operating systems).

    We will now use the scp command to copy the local file called other.txt to your home directory on your remote Matrix Linux server.

  17. Issue the following Linux command to copy the other.txt file from your local machine to your remote Matrix server
    (replace yoursenecaid is YOUR Seneca ID and ADD A COLON : TO THE END OF THE COMMAND):
    scp other.txt yoursenecaid@matrix.senecacollege.ca:

  18. When prompted, enter your Matrix password.

    TIP: You can issue the ssh command, followed by a command that will be run on your remote computer,
    but displayed on your local computer without having to establish a continuous connection to your remote Matrix server.

  19. Issue the following command (using your matrix username): ssh yoursenecaid@matrix.senecacollege.ca ls -l other.txt

  20. When prompted, enter your password and press ENTER.

    Do you see detailed information regarding the file called output.txt?
    That command was run remotely on your Matrix server as confirmation that you securely copied that file
    to the home directory of the Matrix server.

  21. Use the Notepad application (or vi for other OS types) to create a text file called mytextfile.txt and save in the local directory of your computer.

  22. Issue the following Linux command to copy the mytextfile.txt file from your local machine to your remote Matrix server
    (replace yoursenecaid is YOUR Seneca ID and LEAVE OUT THE : at the end of the command!):
    scp mytextfile.txt yoursenecaid@matrix.senecacollege.ca

  23. When prompted, enter your Matrix password.

  24. Issue the following command (using your matrix username): ssh yoursenecaid@matrix.senecacollege.ca ls -l

  25. When prompted, enter your password and press ENTER.

    Do you see the file? That is because the COLON was NOT added to the end of the command!
    Therefore, you MUST remember to include the COLON : at the end of the hostname, or it will NOT properly remotely copy the file!

  26. Issue the following command to copy that same file to the ~/remote directory in Matrix:
    scp other.txt yoursenecaid@matrix.senecacollege.ca:remote

  27. Issue the following command to confirm that it was remotely copied to your ~/remote directory in Matrix:
    ssh yoursenecaid@matrix.senecacollege.ca ls -l /home/yoursenecaid/remote

    Do you see the output for the detailed file listing of output.txt?
    What does this indicate?

  28. Issue the following command to copy that same file to the ~/remote directory in Matrix as a different filename:
    scp other.txt yoursenecaid@matrix.senecacollege.ca:remote/different.txt

  29. Issue the following command to confirm that the file was remotely copied to your ~/remote directory in Matrix with a different filename:
    ssh yoursenecaid@matrix.senecacollege.ca ls -l /home/yoursenecaid/remote/different.txt

    What does this indicate?

    Let's issue a checking script remotely to see that you properly copied that file from your
    local computer to your remote Linux server to both your home directory and ~/remote directory.

  30. Issue the following:
    bash /home/murray.saul/scripts/week6-check-2

    If you encounter errors, re-run the scp commands to correct and re-run the above command until you receive a congratulations message.

NOTE: It would be possible to use the scp command to copy files from the Matrix server to your Windows computer,
but this would require obtaining your computer's IP address and your Windows username, so we will not demonstrate this in our tutorial.
In the next investigation, you will use the sftp Linux command to transfer (i.e. copy) files between servers.


INVESTIGATION 2: USING SECURE FTP

In this section, you will learn how to use the sftp command to transfer files between Unix/Linux servers and
perform some basic directory navigation/management operations.

Using the sftp Command (Command Line)

The SSH package on your home computer and on the Matrix Linux server contain a suite
(i.e. collection) of secure utilities including ssh and sftp.
Let's look at using the sftp command on your local machine.


Perform the Following Steps:

  1. Make certain that you are in your terminal that you opened on your local computer (i.e. NOT in your Matrix account).

  2. Make certain that you are located in the local directory in your home computer.

  3. If you are in MS Windows, open the NotePad application to create a text file.

    NOTE: if you are using a Graphical Linux or MacOSx OS, then use a text editor like vi
    .
  4. Enter a few lines of text, and then click on the File menu and select save as
    (save as the filename thefile.txt in your local directory) and then exit the Notepad text editor.

  5. If your OS is MS Windows issue the dir Windows command to view the contents of your current directory
    (otherwise, issue the ls command for other operating systems).

  6. Issue the following command to move to the parent directory: cd ..

  7. If your OS is MS Windows issue the dir Windows command to view the contents of your current directory
    (otherwise, issue the ls command for other operating systems).

  8. Issue the following command to start an sftp session (note: yoursenecaid is YOUR Seneca ID):
    sftp yoursenecaid@matrix.senecacollege.ca

    NOTE: You may be required to enter yes to have the public key shared.

    Common sftp commands to manage the transfer
    of files between computers.
  9. You should be in the sftp command prompt where you are expected to issue sftp commands.
    Please take a moment to common sftp commands on the right-side table.

  10. Issue the following sftp command: pwd

    What is the pathname? Which server does this represent: local or remote?

  11. Issue the following sftp command: lpwd

    What is the pathname? Which server does this represent: local or remote?

  12. Issue the following sftp command to create a directory on your remote server: mkdir remote2

  13. Issue the following sftp command to confirm that the remote2 directory has been created
    in your remote server's home directory:
    ls

  14. Issue the following sftp command to change to the remote2 directory on your remote server:
    cd remote2

  15. Issue the pwd sftp command to confirm that you have changed to the remote2 directory on your remote server.

  16. Issue the following sftp command to change to the local directory on your local server:
    lcd local

  17. Issue the pwd sftp command to confirm that you have changed to the local directory on your local server.

  18. Issue the following sftp command to transfer the file called thefile.txt to the ~/remote2 directory on your remote server:
    put thefile.txt

  19. Issue the ls sftp command to confirmed that you transferred/copied the file: thefile.txt

    Let's create another directory on your local server called local2 so we can learn to download a file from your remote directory.

  20. Issue the following sftp command (without an argument) to change to the parent directory:
    lcd ..

  21. Issue the lpwd sftp command to confirm that your current working directory on your local server is your home directory.

  22. Issue the following sftp command to create the following directory on your local server:
    lmkdir local2

  23. Issue the following sftp command to change to the local2 directory on your local server:
    lcd local2

  24. Issue the lpwd sftp command to confirm you have changed to the local2 directory on your local server.

    Let's learn to download a file from your remote server to your local server.

  25. Issue the following sftp command to transfer your thefile.txt file from the remote2 directory
    on your remote server to your local computer:
    get thefile.txt

  26. Issue the lls sftp command to confirm that you transferred/copied the file: thefile.txt on your local server.

  27. Issue the following sftp command to exit the sftp utlilty: exit

  28. Issue the ssh command to login to your Matrix server account.

  29. Issue the following Linux command to run a checking script to ensure you created the CORRECT directories,
    created and copied the correct files:
    bash /home/murray.saul/scripts/week6-check-3

  30. If you encounter errors, make corrections and then re-run the checking script until you receive a congratulations message.

Although it is important to learn how to use command-line sftp and is considered coverage for quizzes, midterm and final exam, there are graphical sftp applications. The next section will how you can use the graphical SFTP Secure Client to transfer files between your local computer and your remote Matrix Linux server.


Using Secure Shell sftp Application in MS Windows (Graphical)

If you installed the graphical Secure Shell application in TUTORIAL 1 INVESTIGATION 1,
you can run a graphical application from your computer desktop.

Let's run the Secure Shell SFTP application to transfer files between computers.
This section assumes that your computer is a Windows machine. If so, you can skip this section.


Perform the Following Steps:

  1. Open the NotePad application to create a text file.

    NOTE: if you are using a Graphical Linux or MacOSx OS, then use a text editor like vi
    .
  2. Enter a few lines of text, and then click on the File menu and select save as
    (save as the filename yourfile.txt in your local directory) and then exit the Notepad text editor.

    Double Click on SFTP to launch graphical application.
    Using Quick Connect to login to your Matrix Linux server.
  3. On your Windows desktop, point and double-click on the
    SSH Secure file Transfer application icon.

  4. The main Secure Shell SFTP application window will occur.

  5. Click on the Quick Connect button.

    NOTE: The Connect dialog box allows the user to specify the server name
    and your account name to allow you to connect to the server.

  6. Click on the textbox labelled Hostname and type the text: matrix.senecacollege.ca

  7. Click on the textbox labelled User Name and type your Seneca username
    (i.e. same as your Seneca userid).

  8. After entering the hostname and username, click the Connect button.

  9. Enter your Seneca password when prompted.

  10. When you correctly connect to your Matrix account, you should see folders in your remote server
    (i.e. Matrix server).

    Click and drag file(s) to transfer between computers.
  11. There are two sub-windows to represent your local computer on the left
    and the remote (Matrix) server on the right.

  12. In the left (local computer) window, navigate to your local directory.

  13. In the right (remote Matrix server) window, navigate to the remote2 directory.

  14. In the left window, click and drag the yourfile.txt file to the right window.

  15. Confirm that the yourfile.txt file in the right window to confirm that the file was transferred.

    NOTE: You can select multiple files by using SHIFT-CLICK or CTRL-CLICK methods
    and then click and drag file selections between computers.

  16. Take a few moments to see other buttons for both windows to refresh the view, delete file(s), etc.

  17. Click the Disconnect button and exit the Secure SFTP application.


In the next investigation, you will learn an alternative way to transfer a file to another computer server
by sending an e-mail message with an attached file.



INVESTIGATION 3: USING THE MAIL COMMAND TO SEND FILE ATTACHMENTS

Your Matrix server is also an email server that can allow you to send and receive text-based email messages.

In this section, you will learn how to transfer a file from your Matrix server to another computer system
by sending an email message with a file attachment.


Perform the Following Steps:

  1. Make certain that you are logged into your Matrix account and are located in your home directory.

  2. Issue the following Linux command:
    mail yoursenecaid@myseneca.ca

  3. When prompted, enter the subject line: Test Message
    and press ENTER

  4. In the email message BODY section, type the following text displayed below:
    This is a test email message

  5. Press ctrl-d to send your email message.

    Did any output display after you sent this email message? If so, what you do think it stands for?

  6. Launch a web-browser, login into your Seneca email and check for new email messages.
    Did you receive the email message that you sent from your Matrix server?

    If you did NOT receive an e-mail message, check the JUNK or CLUTTER folders.

  7. Return to your shell and issue the following Linux command:
    mail -a ~/local/thefile.txt yoursenecaid@myseneca.ca

  8. When prompted, enter the subject line: Test Message with Attachment
    and press ENTER

  9. In the email message BODY section, type the following text displayed below:
    This is a test email message with a file attachment

  10. Press ctrl-d to send your message.

  11. Switch to your email and check your Seneca email for messages. Did you receive that email message?

  12. Check your email to see if you received your email message. If you did, what do you notice?

  13. Return to your shell and issue the following Linux command:
    mail yoursenecaid@myseneca.ca < ~/local/thefile.txt

    What happened?

  14. Check your email to see if you received your email message. If you did, what do you notice?

    You should have noticed that there was NO customized subject line, since you redirected stdin from the file,
    thus there was no ability to send a subject line.

    You can use the -s option, followed by text (in quotes) to specify a subject line.

  15. Return to your shell and issue the following Linux command:
    mail -s "email with attachment" yoursenecaid@myseneca.ca < ~/local/thefile.txt

  16. Check your email to see if you received your email message. If you did, what do you notice this time?

  17. After completing this INVESTIGATION, perform the LINUX PRACTICE QUESTIONS at the end of the tutorial.

LINUX PRACTICE QUESTIONS

The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.

Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:

https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week6_practice.docx

Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).


Review Questions:

  1. Write a Linux command to copy a file in the current directory called mytext.txt from your Matrix account to your account called user1
    on the Linux server domain name called tech.myserver.com to that user’s home directory.
  2. Write a Linux command similar to the previous question, but rename the file on the remote Linux server to yourtext.txt
  3. Write a Linux command to copy a file called ~/project/linux.txt to the remote server called linux.techie.org
    (your username for this remote server is the same username for your local server).

  4. Write a Linux command to connect to the username saulm for the server domain name tux.senecac.on.ca to transfer files between Linux servers.
  5. Assuming that you are connected to that server in question #4. What is the sftp command to display your current working directory on your local server?
  6. Assuming that you are connected to that server in question #4. What is the sftp command to view files in your local server?
    What is the sftp command to view files in your remote server?
  7. Assuming that you are connected to that server in question #4. What is the sftp command to download the file answers.txt from the current directory of your remote server?
  8. Assuming that you are connected to that server in question #4. What is the sftp command to upload the file questions.txt from your local server to the ~/documents/tests directory on your remote server?
  9. Assuming that you are connected to that server in question #4. What is the sftp command to quit your current session?

  10. Write a Linux command to send the attached file message.txt to the email address murray.saul@senecacollege.ca with the subject line: Important Message
  11. Create a table listing each Linux command, useful options and command purpose for the following Linux commands: scp , sftp , mail.
  12. Create a table listing each sftp commandand it's purpose.