Transcript SSH

Linux OS(Part І: Basics)
Computer Workshop
Session 7
Sharif University of Technology
Ali Ahari
1
4/8/2016
SSH
 Secure Shell or SSH is a network protocol that allows
data to be exchanged using a secure channel between
two networked devices.
 Used primarily on Linux and Unix based systems to
access shell accounts.
 SSH was designed as a replacement for Telnet and
other insecure remote shells.
2
Ali Ahari
4/8/2016
Shell
 A program that takes the commands you type and
translates them into instructions to the operating
system.
 It is like Command Prompt in windows.
 A shell is just one interface to Linux.
 There are many possible interfaces--like the X Window
System, which lets you run commands by using the
mouse and keyboard.
3
Ali Ahari
4/8/2016
Shell or Kernel?
 The shell manages the technical details of the
operating system kernel interface, which is to the
lowest-level, or 'inner-most' component of an
operating system.
 The program is called a "shell" because it hides the
details of the underlying operating system behind the
shell's interface
4
Ali Ahari
4/8/2016
Famous Shells
 Bourne:was one of the major shells used in early versions of the Unix
operating system.
 C: Its syntax is modeled after the C programming language. It has
many interactive commands.
 Z: can be thought of as an extended Bourne shell with a large number
of improvements.
 BASH(Bourne-again shell): It is the default shell on most systems built
on top of the Linux kernel
 KORN: It is backwards-compatible with the Bourne shell and includes
many features of the C shell as well.
 TC(TENEX C Shell): It is essentially the C shell with programmable
command line completion, command-line editing, and a few other
features.
5
Ali Ahari
4/8/2016
Get Started 1
 Using Tab and Arrow keys.
 Everything is a file.
 files are case sensitive.
 bookstobuy.txt
 BooksToBuy.txt
 BoOkStObUy.txt
 Maximum filename length is 256 char.
 Files are identified by their file names.
 Directory is a file, tooa special kind of file that
contains information about other files.
6
Ali Ahari
4/8/2016
Wildcards
 Imagine that you have the following files:
 libby1.jpg
 libby2.jpg
 libby3.jpg
…
 libby12.jpg
 libby1.txt
 You want to delete these files using the rm
command,what will you do?
7
Ali Ahari
4/8/2016
Wildcards
 There are three wildcards:
 *: matches any character zero or more times.
 ?: matches a single character.
 [ ]: match either a set of single characters ([12],
for instance) or a range of characters separated by a
hyphen (such as [1-3]).
8
Ali Ahari
4/8/2016
Escaping special characters
 $ rm Why\ don\ 't\ I\ name\ files\ with\ \*\?.txt
 $ rm "Why don't I name files with *?.txt"
9
Ali Ahari
Character
Advice
/
Never use. Cannot be escaped.
\
Must be escaped. Avoid.
-
Never use at beginning of file or directory name.
[]
Must be escaped. Avoid.
{}
Must be escaped. Avoid.
*
Must be escaped. Avoid.
?
Must be escaped. Avoid.
'
Must be escaped. Avoid.
"
Must be escaped. Avoid.
4/8/2016
Examples for using wildcards
 Examples for using *:
10
Ali Ahari
Command
Matches
rm libby1*
libby10.jpg through libby12.jpg, as well as libby1.txt
rm libby*.jpg
libby1.jpg through libby12.jpg, but not libby1.txt
rm *txt
libby1.txt, but not libby1.jpg through libby12.jpg
rm libby*
libby1.jpg through libby12.jpg, and libby1.txt
rm *
All files in the directory
4/8/2016
Examples for using wildcards
 Examples for using ?:
Command
Matches
rm libby1?.jpg libby10.jpg through libby12.jpg, but not libby1.txt
11
rm libby?.jpg
libby1.jpg through libby9.jpg, but not libby10.jpg
rm libby?.*
libby1.jpg though libby9.jpg, as well as libby1.txt
Ali Ahari
4/8/2016
Examples for using wildcards
 Examples for using [ ]:
12
Command
Matches
rm libby1[12].jpg
libby11.jpg and libby12.jpg, but not libby10.jpg
rm libby1[0-2].jpg
libby10.jpg through libby12.jpg, but not libby1.jpg
rm libby[6-8].jpg
libby6.jpg through libby8.jpg, but nothing else
Ali Ahari
4/8/2016
Common shell commands:
 File & directory
commands:
 cd,ls,cp,mv,rm,mkdir,pwd,ln
 touch,cat, vim, head, tail
 Searching for files:
 find, locate, updatedb
 whereis, which
 Permission-related:
 chmod, chgrp, chown
13
Ali Ahari
 Security-related:
 su, passwd, logout
 System-resources related:
 ps, kill, free, df, du
 Other commands:
 echo
 who, whoami,finger, write,
talk, mesg
 gcc
4/8/2016
Commands
 cd: change directory
 cd [with no arguments] ---> change to user home directory
 cd / ---> root directory
 cd (back)
 ls: list directory contents
-a : all, -F, -X
 --color
 -l : long listing format
 -n : numeric uid/gid (this way, students can find others' STUDENT IDs)

 cp: copy, mv: move (can be used to rename files, too)
-r : recursive
 -v : verbose
 -I (warn you about overwriting)
 -R (for copying directories)

14
Ali Ahari
4/8/2016
Commands
15
Ali Ahari
4/8/2016
Commands
 chgrp: change the group owning files.
 See /etc/group to find the list of groups
 chown: change the owner of a file.
 See /etc/passwd to find the list of owner
 chown owner:group
16
Ali Ahari
4/8/2016
Permission Letters and Their Meanings
The root user can always do anything to any file or directory
File Attribute
17
Abbrivation
Meaning for File
Meaning for Directory
Readable
r
Can view.
Can list with ls.
Writable
w
Can edit.
Can delete, rename, or add
files.
Executable
x
Can run as program.
Can access to read files and
subdirectories or to run
files.
Ali Ahari
4/8/2016
Change Permissions on Files
 chmod:
 Alphabetic: chmod [ugo][+-=][rwx]
 Example: chmod go+w ali.txt
 Numeric: chmod [0-7][0-7][0-7]
 Example: chmod 664 ali.txt
Owner
Group
World
r; w; x
r; w; x
r; w; x
Numeric
4; 2; 1
representation
4; 2; 1
4; 2; 1
Permissions
18
Ali Ahari
4/8/2016
Commands
 rm: remove , rmdir: remove empty directory
 -d : directory
 -f : force
 -r : recursive
 mkdir: make directory
 -p (make n-level directory and subdirectory)
 pwd: print working directory
 ln: make links between files
 -s : make symbolic links instead of hard links
19
Ali Ahari
4/8/2016
Link
Hard link
 Pointers to programs and files, but
NOT directories
 If the original program or file is
renamed, moved, or deleted, the
hard link is NOT broken
 Hard links cannot span disk drives,
so you CANNOT have a hard link on
/dev/hdb that refers to a program
or file on /dev/hda
 To create a hard link called
myhardlink.txt that points to a file
called myfile.txt, use this: ln
myfile.txt myhardlink.txt
20
Ali Ahari
Soft link
 Pointers to programs, files, or
directories located elsewhere (just
like Windows shortcuts)
 If the original program, file, or
directory is renamed, moved, or
deleted, the soft link is broken.
 If you type ls -F you can see which
files are soft links because they
end with @
 To create a soft link called
myfilelink.txt that points to a file
called myfile.txt, use this: ln -s
myfile.txt myfilelink.txt
4/8/2016
Commands
 touch: update file timestamp


touch can create new files
-t : change a file to any desired time
 cat: concatenate and view
 less: view text files a screen at a time
 /: when you are in less mode you can find the special pattern which you want
 V : edit files when you view
 head: view first 10 lines of a file
 tail: view the last 10 lines of a file
 -f : shows the last 10 lines of a file as the file changes
21
Ali Ahari
4/8/2016
Commands
 Vim







22
Different modes of operation (Command Mode, Insert Mode)
How to create a file
How to save a file (using :w)
How to exit (using :q)
How to save a file and exit (using :wq)
How to exit without saving (using :q!)
How to search for texts (using / operator)
Ali Ahari
4/8/2016
Commands
 locate
 -i : case-insensitive search
 find
Sample usages:


# find . -name "*.txt" ----> finds all files with txt extension in current (.) directory

# find / ! -user ahary----> finds all files in the root directory whose owner is not ahary

# find . -size +10M
----> find all files in the current directory whose size are beyond 1MB

 - whereis: locate the paths for a command's executable, source files,
and man pages
 -b : locate binary files
 -m : locate man pages
 -s : locate source files

23
which: find out which version of a command will run
Ali Ahari
4/8/2016
Commands
 ps: report process status
 Common usages:


ps aux ------> view all currently running processes
ps auxf ------> view a process tree
 df, du
 -h : print file sizes in human-readable format (using K,M,G,etc for
KB,MB,GB,etc)
 who: print who is currently logged in
 -a : all (detailed)
 whoami , su
24
Ali Ahari
4/8/2016
Now create your homepage!
1.
2.
3.
4.
5.
Create a “public_html” directory
Grant the right permission to it
Create a html file like below.
Upload html file to it.
Now you have a homepage!
Congratulation!
25
Ali Ahari
4/8/2016
Common techniques
 The meaninig of tilde (~): home directory
 Running several commands using semicolon
 ; : for do some commands sequentially
 && : run commands only if the perevious ones succeed
 || : run a command only if the perevious one fails
 Running commands in the background using &
 Using backslash (\) for commands that span more
than one line
26
Ali Ahari
4/8/2016
Common techniques
 $(): as though you had typed that output in directly
 | : it takes the output from the first and uses it as
input for the second.
 > : redirects the output of a command to a file.
 >> : append a command’s output to a file.
 < : use a file as input for a command
27
Ali Ahari
4/8/2016
Help me!
 --help
 man : manual
 -k : find the command by its funcitionality.
 -f : shows the syntax for a command
 info: a little hard for novices to understand
 whatis: same as man –f.
 apropos: same as man –k.
28
Ali Ahari
4/8/2016
Assignment
 Each student should find the use of a new Linux
command and report in farsi at most one page and
submit it electrinically in PDF format.
29
Ali Ahari
4/8/2016
Any Question?
30
Ali Ahari
4/8/2016