Difference between revisions of "Tutorial6: File Transfer / Sending Email Messages"
(→INVESTIGATION 1: USING SECURE COPY) |
(→INVESTIGATION 1: USING SECURE COPY) |
||
Line 148: | Line 148: | ||
</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>Change back to your <b>home</b> directory and confirm that you are located in your <i>home</i> directory.<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>Issue the following: <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 to create a directory called <b>local</b> in your current directory: <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 to change to the local directory: <span style="color:blue;font-family:courier;font-weight:bold;">cd local</span><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 | + | <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>Change back to your <b>home</b> directory and confirm that you are located in your <i>home</i> directory.<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>Issue the following: <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 to create a directory called <b>local</b> in your current directory: <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 to change to the local directory: <span style="color:blue;font-family:courier;font-weight:bold;">cd local</span><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 then click on the <b>File</b> menu and select <b>save as</b> (save as the filename <b>other.txt</b>) and then exit the Notepad 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> to confirm the contents of your current directory.<br><br>The <b>SSH package</b> on your <i>home computer</i> and on the <i>Matrix Linux server</i> contain a <b>suite</b> (i.e. <b>collection</b>) of secure utilities <br>including <b>ssh</b>, <b>scp</b> and <b>sftp</b>.<br><br></li></ol> |
Revision as of 10:13, 23 January 2021
Contents
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.
Secure Copy (scp)
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)
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.
Operation | sftp Command (Local Server) | sftp Command (Remote Server) |
---|---|---|
Display current working directory | !pwd | pwd |
Display directory contents | !ls | ls |
Create Directory | !mkdir | mkdir |
Change directory location | lcd | cd |
Upload file to remote server | put | - |
Download file to local server | get | - |
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)
Sending a Simple Email Message:
- Type: mail username@hostname and press ENTER
- Enter subject line and press ENTER
- Type the body of the message and then when finished, press ctrl+d to send message
- Type: mail username@hostname and press ENTER
Sending an Email Message with a File Attachment:
- Type: mail username@hostname -a filepathname and press ENTER
- Enter subject line and press ENTER
- Type the body of the message and then when finished, press ctrl+d to send message
- Type: mail username@hostname -a filepathname and press ENTER
Alternative Method of Sending an Email Message with a File Attachment:
- Type: mail -s "your subject line" username@hostname < filenamepathname
- 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.
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.
- Determine which operating system that your computer is using.
- 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: |
---|---|---|
|
|
|
- When you have issue the above ssh command, enter your password to login.
- Issue the following Linux commands to create the following directory:
mkdir ~/remote - Change to the ~/remote directory and confirm that you have changed to that directory.
- Use a text editor to create a text file called myfile.txt
- Enter the following two lines displayed below in your editing session:
This is my file
It is a small file - Save editing changes to your myfile.txt file and exit your text editor.
- Change back to your home directory and confirm that you are located in your home directory.
- 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. Issue the following: bash /home/murray.saul/scripts/week6-check-1 - 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. - Exit your Matrix ssh session, but remain in the OS terminal on your home computer.
- The mkdir command works with MS Windows/UNIX/Linux/MacOSx computers.
Issue the following command to create a directory called local in your current directory: mkdir local - The cd command works with MS Windows/UNIX/Linux/MacOSx computers.
Issue the following command to change to the local directory: cd local - 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
. - Enter a few lines of text, and then click on the File menu and select save as (save as the filename other.txt) and then exit the Notepad text editor.
- If your OS is MS Windows issue the dir to confirm the contents of your current directory.
The SSH package on your home computer and on the Matrix Linux server contain a suite (i.e. collection) of secure utilities
including ssh, scp and sftp.
- Issue the following Linux command to copy the myfile.txt from your local server to your remote server (i.e. yoursenecaid is YOUR Seneca ID):
scp myfile.txt yoursenecaid@matrix.senecacollege.ca:destination
NOTE: You may be required to enter yes to have the public key shared. - When prompted, enter your Matrix password.
- Issue the same Linux command that you issued in step #13.
What do you notice? - Issue the following Linux command:
scp myfile.txt yoursenecaid@matrix.senecacollege.ca:
Where do you think this file was copied to on your remote server? - Issue the ls command for the home directory.
What do you see?
What does this tell you about how the previously-issued command works with just a colon? - Issue the following Linux command (leaving-out the colon character at the end):
scp myfile.txt yoursenecaid@matrix.senecacollege.ca
What happened? (hint: look for a file named: yoursenecaid@matrix.senecacollege.ca). - Make certain that your current directory is your home directory.
- 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-2 - If you encounter errors, make corrections and then re-run the checking script until you receive a congratulations message.
- 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.
Perform the Following Steps:
- 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:
- Make certain that you are logged into your Matrix account and are located in your home directory.
- Issue the following Linux command:
mail yoursenecaid@myseneca.ca - When prompted, enter the subject line: Test Message
and press ENTER - In the email message BODY section, type the following text displayed below:
This is a test email message - 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? - 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. - Return to your shell and issue the following Linux command:
mail -a ~/local/thefile.txt yoursenecaid@myseneca.ca - When prompted, enter the subject line: Test Message with Attachment
and press ENTER - In the email message BODY section, type the following text displayed below:
This is a test email message with a file attachment - Press ctrl-d to send your message.
- Switch to your email and check your Seneca email for messages. Did you receive that email message?
- Check your email to see if you received your email message. If you did, what do you notice?
- Return to your shell and issue the following Linux command:
mail yoursenecaid@myseneca.ca < ~/local/thefile.txt
What happened? - 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. - Return to your shell and issue the following Linux command:
mail -s "email with attachment" yoursenecaid@myseneca.ca < ~/local/thefile.txt - Check your email to see if you received your email message. If you did, what do you notice this time?
- 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:
- 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. - Write a Linux command similar to the previous question, but rename the file on the remote Linux server to yourtext.txt
- 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). - 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.
- 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?
- 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? - 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?
- 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?
- Assuming that you are connected to that server in question #4. What is the sftp command to quit your current session?
- 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
- Create a table listing each Linux command, useful options and command purpose for the following Linux commands: scp , sftp , mail.
- Create a table listing each sftp commandand it's purpose.