Tutorial6: File Transfer / Sending Email Messages
Contents
TRANSFERRING FILES / USING THE MAIL COMMAND
Main Objectives of this Practice Tutorial
- List common utilities combined with the ssh suite of utilities
- 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 actually a suite of tools to allow the user to not only issue commands securely between Unix / Linux servers, but also securely copy and transfer files among Unix/Linux servers via the commands scp and sftp.
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 user@host:destination-pathname
scp local.file user@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 framework of rules on how to convert data that is transferred between computer servers (both identical and different).
When you login via the sftp command, you are in the sftp prompt (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 issues the sftp command.
Below is a table that provides sftp commands to perform basic transfer and file 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)
Steps when sending email (Method 1):
- Type: mail recipient@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
NOTE: You can use the mail command with the -a option to specify a file.
Example:
mail recipient@hostname -a filename
- Type: mail recipient@hostname and press ENTER
Steps when sending email (Method 2):
- Use the -s option in the command to specify the subject line of the command and use stdin redirection to send a text file as the body of the message.
NOTE: The advantage of using this method is that you can send an email message without being prompted for the subject line or body of text message.
Example:
mail -s "your subject line" recipient@hostname < filename
- Use the -s option in the command to specify the subject line of the command and use stdin redirection to send a text file as the body of the message.
INVESTIGATION 1: USING SECURE COPY
In this section, you will learn how to use the scp command.
ATTENTION: Since the only server account you have is your Matrix server,
we will simulate using the scp command to copy from your Matrix server to
another instance of your Matrix server.
To make this work, we will need to edit a "start-up" file to place a special series of commands.
You will learn more about start-up files later in this course.
Perform the Following Steps:
- Login your matrix account and issue a command to confirm you are located in your home directory.
- Issue the following Linux commands to create the following directories:
mkdir ~/from
mkdir ~/destination - Change to the ~/from directory.
- Use a text editor to create a text file called myfile.txt
- Enter the following two lines displayed below:
This is my file
It is a small file - Save editing changes 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 to make certain that you created the correct directories
and that you created the myfile.txt file (with correct file contents) in the ~/from directory
in order to practice using the scp Linux command. - Issue the following Linux command to run a checking script: bash /home/murray.saul/scripts/week6-check-1
- If you encounter errors and re-run the checking script until you receive a congratulations message, and proceed to the next step.
NOTE: As mentioned we will simulate using the scp on another instance of our Matrix server.
To make this work, we will need to edit a "start-up" file to place a special series of commands - Use a text editor to edit the following start-up file: ~/.bashrc
- Enter the following text (shown below) at the TOP of this file:
$- == *i* || return - Issue the following Linux command to confirm that the ~/destination directory is empty:
ls ~/destination - Although you are in your Matrix account, issue the following Linux command (i.e. yoursenecaid is YOUR Seneca ID):
scp myfile.txt yoursenecaid@matrix.senecacollege.ca:/destination
NOTE: You may be required to type y to have the public key shared. - When prompted, enter your Matrix password.
- Issue the same Linux command that you issued in step #12.
What did 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? - Issue the following Linux command (leaving-out the colon character at the end):
scp myfile.txt yoursenecaid@matrix.senecacollege.ca
What happened? - 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 and 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 TRANSMISSION CONTROL PROTOCOL
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.
NOTE: Since the only server account you have is your Matrix server,
we will simulate using the sftp command to transfer/copy from your Matrix server to
another instance of your Matrix server. You are NOT required to edit your .bashrc file
to make this work since you already performed that operation in the previous investigation.
Perform the Following Steps:
- Make certain that you are logged into your Matrix account and are located in your home directory.
- x
- In the next investigation, you will ...
INVESTIGATION 3: USING THE MAIL COMMAND
In this section, you will learn how to ...
Perform the Following Steps:
- x
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 that were mentioned in the online assignment #1 and command purpose for the following Linux commands: scp , sftp , mail.