1
Remote Access and SSH
Pay attenti
- n
to text i n green! These are correcti
- ns
done af ter the l ecture!
Remote Access and SSH a t t e n t i i g r e e n ! P a y t o t e x t - - PowerPoint PPT Presentation
Remote Access and SSH a t t e n t i i g r e e n ! P a y t o t e x t o n n T h e s e c o r r e c t i d o n e a f t h e l e c t u r e ! t e r a r e o n s 1 Remote Access Modern computing requires the use of a variety of resources located on
1
Pay attenti
to text i n green! These are correcti
done af ter the l ecture!
2
3
4
5
6
any command, it appears on the computer as a process, with attributes and identifiers. If a process runs amok, you need to do something about
running at the same time. This is referred as multitasking.
process). The process has the same permissions as its owner does.
it as a sandbox where the code of your particular program/task is executed.
7
system.
Ctrl+Esc or Ctrl+Alt+Del and mostly used to kill application that is hanging.
the Firefox Browser.
files\firefox\firefox.exe".
define the way command should behave. Each command has it’s own predefined list of parameters which make sense specifically for this
man <command_name> command. For example man ls will give you information about ls command.
8
input from the keyboard and sends its output to the screen.
keyboard. While a program is running in the foreground and is time-consuming, no other commands can be run (start any other processes) because the prompt would not be available until the program finishes processing and comes out.
background process requires any keyboard input, it waits but you couldn’t see this.
at the end of the command.
whereas /usr/bin/firefox & will run Firefox Browser in background.
9
10
11
12
13
Parent process ID (ppid). Each user process in the system has a parent process. Most of the commands that you run have the shell as their parent. Check the ps -f example where this command listed both the process ID and the parent process ID.
SIGCHLD signal. Then the parent can do some other task or restart a new child as needed. However, sometimes the parent process is killed before its child is killed. In this case, the "parent of all processes," the init process, becomes the new PPID (parent process ID). In some cases, these processes are called orphan processes.
have completed execution but still find an entry in the process table.
root and services requests from other processes. A daemon has no controlling terminal. If you do a "ps -ef" and look at the tty field, all daemons will have a ? for the tty.
it is capable of working with. For example, a printer daemon waiting for print commands or SSH daemon you are actually interacting with to connect to this machine through SSH.
background.
14
15
account's home directory. A home directory is a directory set aside for your user to store files and create directories. It is the location in the filesystem where you have full dominion.
filesystem, you can use the pwd command. This command displays the directory that we are currently in.
example is what the value would be if you were logged into the server with an account called user1. This directory is within a directory called /home, which is itself within the top-level directory, which is called "root" but represented by a single slash "/".
16
17
18
19
20
filename_to. filename_from/filename_to could be either folder or file name.
filename_to. filename_from/filename_to could be either folder or file name.
fetch multiple files/folder at a time, e.g. cp /home/user1/dir1/*.txt /home/ser1/dir2 will copy all files with .txt extension from dir1 to dir2.
21
copy, change, delete, and execute the files stored on a machine's file system. The way those rights are handled and modified varies from OS to OS.
copy of it.
a directory, and even to delete the file or directory.
directory execute permission means that user is allowed to enter the directory, and access files and directories inside.
file rights is separate from the ability to change the contents of a file, while in Unix the ability to change permissions on the file is included in the "write" permission.
22
the listing of rights on files in Unix. Each file has a 10-character string associated with it that describes its nature and rights completely. We can view this string using the ls -l command: ls -l myfile.txt
directory, the first character would be a "d".
to the rights of the owner of the file. The next three give the rights for members of the group that
execute (x) permission on the file, while members of the group (called "grp") are treated the same as anyone else: they can read or execute the file, but not write to it. This means that anyone can view, copy or run the files in the directory, but cannot change it or delete its contents.
10:45 book1.pdf. In this case, book1.pdf is a plain file whose owner ("sally") and group ("grp") has read and write permission, while everyone else has only read permission.
23
command has the form chmod [ugo][+-][rwx] <filename>
command applies to both owner and owner’s group).
removes the specified modes from the specified classes
can use multiple options at a time, e.g wx means that your command will grant/revoke both write and execute permissions).
current machine, chmod g-wx file1 will revoke write and execute permissions form owner’s group.
24
zeros, how can they be different?"
Yes - all files are the same, but they are interpreted differently to you by the computer.
concerns whether they are considered to be text or binary. Text files (in English) are usually coded in ASCII (the American Standard Code for Information Interchange) - a seven bit scheme for representing letters as binary numbers.
IBM mainframes) are considered to be "binary" files. Such files may be programs - sequences of instructions that the computer can run directly - or they may require
viewed well using a simple text editor because it contains non-ASCII character codes.
25
expression in their name
given expression in the output of some_other_command and list all lines where expression occurs. For example ls -l | grep “test” will list all lines where string test occur (it can be either in file name or in
26
a shell is another name for command-line interface. It is named a shell because it is a layer around the operating system kernel. The aspect we want to emphasize here is that each shell defines a set of commands and syntax (programming language) it exposes to user.
Shell), csh (the C Shell), tcsh (the Trendy C Shell, or something like that...), and ksh (the Korn Shell) are but a few. We will use bash. bash is widely considered to be technically superior to most other shells, but for what most users do, it doesn't matter.
session), so every command you run is interpreted by the rules defined for this specific shell and not any other. To see what shell are you using type echo $0.
27
shell in a way that depends on which shell you use. Environment variables are one such area. Every shell has certain variables that are used by the shell in its operations.
in which the shell will look for commands that are not built-in.
$PATH. To set variable value simply use <variablename>=”<variablevalue>”, e.g. PATH=$PATH:/home/user1/custom will append string “:/home/user1/custom” to your PATH variable.
are self-explanatory.
28
29
then saves the file in both the native format for the spreadsheet program, and also as a comma separated value file. He then runs a program that converts the comma separated file into an HTML table, and then uses SCP to send the file to a web server where his students can view it. The instructor might try to automate this process to some extent using the following script:
libreoffice --calc /home/me/M300/grades.xls read cd "/home/me/M300" txt2html grades.txt scp me@www.myserver.wsu.edu grades.txt
program until the instructor indicates that he is ready to continue by hitting any key (this gives him time to enter the grades and save the files). The third switches to the directory where the files are. The next command runs the program that does the conversion (which might be a script itself), and the last runs ftp for him. Finally, the last line uploads file to www.myserver.wsu.edu. Thus, to carry out the entire process the instructor only needs to type the name of the script (or double-click it in the file manager), and hit any key after the grades are saved.
Most scripts are much more powerful. They store and retrieve values, and make decisions based on conditions on the computer.
30
executable by typing chmod +x myscript. Many people like to make their bash- shell scripts end with .sh, e.g. myscript.sh, but this is not mandatory.
system that the commands in the script should be interpreted using the bash shell.
/home/user1/scripts/myscript.sh. Note that to run script which is in the current folder type ./myscript (add ./ in the beginning).
nano myscrpt (type #!/bin/bash and some commands you want to run and save file and close nano) chmod +x myscript ./myscript
31
work with.
M300 directory. That way if we are also teaching Math 171, we can use the same script for both courses by entering different variable values.
libreoffice --calc /home/me/$COURSE/grades.xls read cd "/home/me/$COURSE" txt2html grades.txt scp me@www.myserver.wsu.edu grades.txt
COURSE=M300 export COURSE ./upload_grades.sh
COURSE=M171 export COURSE ./upload_grades.sh
to the script (using export <variablename> commnad) which you are going to run (note: still no $ sign). The last line runs your script. While script is executed wherever $COURSE is used in your script it is substituted with the actual value you defined in the first line (note: when we refer to a variable in a script $ is necessary)
32
directories, the values of variables, and other factors relating to the machine they run on. In particular, it is important to be able to choose which of several actions to take in response to input or
if [ $VAR == "yes" ]; then echo "The answer was yes." else echo "The answer was no." fi
“The answer is no.”
if_[_$VAR_==_"yes"_];
between else and fi.
Correction: $VAR should be wrapped with double quotation mark, i.e. if [ "$VAR" == "yes" ]; then sam e here: i f _[_"$V A R "_= = _"yes"_];
33
underscore here denotes a whitespace): for_VAR_in_"Linux"_"Redhat"_"Unix";_do echo_We_like_$VAR done
comprises the strings "Linux", "Redhat", and "Unix". This block of code executes three times. The first time, the variable VAR contains the string "Linux", the second time it contains "Redhat", and,
We like Linux We like Redhat We like Unix
34
35
36
information in them cannot be understood by anyone intercepting it.
that supports SFTP on the remote machine. While there are many client packages, in the simplest setting the interface is a command line.
list the contents of the remote directory > ls change the remote directory > cd newdirectory change the local directory > lcd mydirectory put a file on the remote machine > put myfile remotefile get a file from the remote machine > get remotefile close the session >exit
37
CoPy). We used in example scrip to upload grades to remote machine.
and sftp, but the appearance to the user is significantly different. In particular, the scp command works very like the cp (copy) command of Unix.
named mycomputer.math.wsu.edu to the current directory, one could type a command of the form scp myname@mycomputer.math.wsu.edu:remote.txt local.txt.
remote computer named mycomputer.math.wsu.edu of the form scp local.txt myname@mycomputer.math.wsu.edu:remote.txt.
38
39
connect to the server via SSH.
and run ssh your_username@adams.math.wsu.edu to connect to the server via SSH.
the port is also the same (since SFTP runs as a subsystem of SSH). Your
https://en.wikipedia.org/wiki/CentOS for details.
These exercises are not graded! That's for your practice!
40
1) Start ssh session 2) Run command to display your current working directory. What directory is that? 3) Go two levels up in directories hierarchy. List contents of the current directory in the way that you can see permissions for each
4) Go to your home directory with one command. 5) Create the following folders structure inside your home directory:
~/dir1 ~/dir1/dir2 ~/dir1/dir3 ~/dir1/dir3/dir4
6) Run command to show nested folders structure to make sure you did everything correctly. 7) Go to ~/dir1/dir3 and create three empty files there: balck_dog.txt, white_dog.txt, red_cat.txt 8) What user permissions do these newly created files have? Is any user on this machine can modify it? Since we all are working
9) Open editor for red_cat.txt and add text saying something, e.g. “It’s pretty strange!”. Close editor 10) Display the contents of red_cat.txt 11) Copy all the files containing “dog” in it’s name to ~/dir1/dir2 12) Move red_cat.txt to ~/dir1. 13) Remove ~/dir1/dir3. 14) Go to your home directory to show and run command to show nested folders structure. 15) Remove dir1.
use "l s
di r1"
41
1) Create a directory ice2 and go to that directory. 2) Create an empty file named mycommand. 3) mycommand is going to be a script and it’s supposed to do the following: if the value of environment variable called NEED_A_FILE_HERE equals “yes”, then it creates a file in current directory with name file_for_{username} and notifies user about that (where {username} is name of the current user); otherwise, it deletes file_for_{username} if any exits in current folder and notifies user about that. So, you need to edit mycommand and add corresponding commands. 4) Try to run mycommand (type ./mycommand). Why do you get an error message? Fix it and try again. 5) Set NEED_A_FILE_HERE to “yes” and make it visible to your script. Try to run script the
6) Go to your home folder and run your script again. Check if a new file was created. 7) Now set NEED_A_FILE_HERE to “no” and try to run it again. Check if file was deleted. 8) How can you make your command accessible from any location by just typing mycommand without specifying a path to the script (like you did it in home folder). Make it possible.
42
1) Create a directory ice3 and go to that directory. 2) Create a script with name dosomething.sh with following commands: echo Doing something... for VAR in {0..1200}; do echo $((VAR*3)) secnonds elasped. sleep 3 done This script imitates some long-running task which reports some progress periodically. It will take
3) Run the script as a foreground process. How can you interrupt it? Wait for some time and do it. 4) Run the script as a background process. Where does output of your script go? How can you interrupt background process? Wait for some time and do it. 5) Now run the script as a background process and redirect its output to a file called some_output.txt. Wait for 15 seconds and check what’s in that file. Wait for 15 more seconds and do it again. Has anything changed? Kill the background process you’ve created.
43
1) Your SSH should be still open and you are connected to a remote machine. 2) Create a directory ice4 and go to that directory. Create an empty file called remote.txt. Close SSH session. 3) Start SFTP client. 4) Copy remote.txt to your local machine with name local.txt. 5) Using any graphical editor (e.g. Notepad on Windows) add the following text to local.txt: “This file was modified on local machine.”. 6) Copy local.txt to remote machine with name remote.txt (so you will overwrite the existing one). 7) Close SFTP client. 8) Open SSH session and and see what’s inside remote.txt. 9) Close SSH session.
44