Introduction To Linux: Foundation Course

Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Introduction to Linux

Foundation Course
Outline

 An Operating System
 History of Linux
 Free and Open Source Software
 Linux Distributions
 Linux File System
 The Shell
 Getting Help
An Operating System
Linux Timeline
History of Linux
 Linus Torvalds wrote the original Linux
operating system as a hobbyist
 exercise in exploiting the new Intel 386 memory
architecture
 Influenced by Minix, an operating system
written by Torvald's professor
 Andy Tanenbaum as a teaching aid, which was
in turn based on UNIX
 Not derived from UNIX source code but
deliberately UNIX-compatible
Benefits of Linux
 Free thus no vendor lock-in
 Very secure, no serious virus threats
 Distributions remain current and supported by
communities of other developers.
 It can run on a wide range of hardware and will
install alongside pre-existing systems
 It supports centralized software installation from
pre-existing repositories.
 Its resource requirements are low.
Free and Open Source Software
 Linux is distributed under Open Source licences
 Popular Open Source Licences:
 GPL (GNU Public Licence) & LGPL
 MIT
 Apache
 Free as in “free speech” not “free lunch”
 Free to run,
 Freed to study & adapt,
 Free to redistribute,
 Free to improve.
Linux Distributions
 Strictly speaking, 'linux' refers only to the
operating system kernel
 Pieces of Linux OS can be downloaded from
the internet free of charge and assembled into a
working system BUT its hard work
 Most users purchase a pre-built distribution
 Vendors add value through installation and
configuration tools and support agreements for
enterprises.
Red Hat Based Distros
Debian-based Distros
Non-aligned Distros
The shell

● The 'shell' is the linux command interpreter



The shell operates in a command processing loop:
● Displays a 'prompt' and reads a command line
● Peforms various substitutions and expansions on the command line

Executes the resulting command and waits for it to finish

Loops back and prompts for another command
● Several shells have been written for UNIX and Linux
● Bourne shell (sh), Korn Shell, C Shell, Bourne Again Shell (bash)
● The core feauture set of all these shells is very similar

We will focus on bash, the most popular shell on Linux
Simple commands

● Try these simple commands: Do now!


$ hostname
snowhite Reports the name
$ date of this machine
Fri Aug 23 11:48:33 CAT 2019
$ id
uid=500(chris) gid=100(users) groups=100(users),14(uucp)
Command options

● Command options modify the behaviour of a command


– Usually, an option is a single letter prefixed by '-'

$ date -I Options are case sensitive


2019-08-16
The date in ISO format

● Some commands also have 'long' options


– Begin with '--'
– Supplement or replace the traditional single character options
$ date --iso-8601
Same as '-I'
2019-04-16
Command arguments

● Most commands accept arguments


– The command name, options, and arguments are separated by
whitespace (spaces and tabs)
– Arguments are often the names of files or directories on which to operate

● Options and arguments are often used together:


$ ls -l /home The -l option requests
total6 a 'long' listing
drwxr-xr-x 66 chris users 3328 2004-04-16 11:48 chris
drwxr-xr-x 17 dilbert users 1112 2004-02-09 11:52 dilbert
drwxr-xr-x 18 tux users 1240 2004-03-30 20:29 tux
Command history

● bash remembers the most recent commands you've entered


– stored in the file .bash_history in your home directory
– survives across logout / login, shared by all instances of bash
– size of history file is configurable, set to 500 commands in SuSE Linux
● The history command shows your command history
history shows your entire command history
history 10 shows the last ten commands
history -c clears your command history
● Previous commands can be selected and re-executed
!85 re-execute command 85
!string re-execute most recent command that began with string
!! re-execute last command
Command history (continued)

● You can also edit your command history on screen.


– The following keys are used:

 scroll back through history

 scroll forward through history


 move left along line

 move right along line


string insert text string into line
<DEL> delete character
<ENTER> execute the command
Command history (continued)

● To provide you with a command history, execute the following


commands:
$ pwd Do now!
$ hostname
$ date
$ id
$ ls /etc/hosts.allow
$ cat /etc/fstab

● Recall your command history with the history command


● Recall the date command by command number
● Recall the most recent command beginning with 'ho'

Absolute pathnames

/ (root)

home etc

at.deny
tux dilbert dumpdates
exports

code reports expenses travel

main.c July_Conf Aug_2003


edges.c paris
progress Sep_2003 rome
quality Oct_2003
Absolute pathnames (continued)

● The linux file system is organised in a tree structure



The top level directory of the tree is called the root directory and is
named '/'

A file or directory can be referenced using an absolute pathname
– Starts with a '/'
– Traces a path from the root of the tree to the file
– Uses '/' (forward slash) to separate components of the pathname
● Examples:
/etc/at.deny
/home/dilbert/travel
/home/tux/reports/quality
Relative pathnames

● Pathnames not beginning with '/' are relative to the current directory
● Examples (assuming /home/tux is the current directory):
reports
code/main.c

Every directory has a special entry named '..' which references the
parent directory
– The directory immediately above it in the tree

Use relative pathnames beginning with '..' to move outside the
current directory
● Examples (assuming /home/tux/code is the current directory):
../reports/July_Conf
../../dilbert/travel/paris
File name completion

● When entering a command, bash will perform filename completion


– Press the TAB key
– bash will complete as much of the name as is unambiguous, based on the
name completed so far and the names of the existing files
– Less typing, less chance for typing mistakes
– If there are several possible matches, [TAB] [TAB] will show them all
● Example ([T] denotes the TAB key)
$ ls -l /h[T]/t[T]/re[T]/Ju[T] ... completes to:
$ ls -l /home/tux/reports/July_Conf

Using file name completion to minimise typing, do a long directory
listing (ls -l) on these files:
Do now!
/usr/X11R6/bin/showfont
/usr/share/xscreensaver/screensaver-diagnostic.xpm
/boot/vmlinuz.config
Users and groups

● Every user has an account name (e.g. tux) along with an associated
numeric user ID (e.g. 500)
● Every user is associated with one named group which is their primary
group
– Groups allow additional flexibility in assigning access permissions

Users can also be associated with one or more secondary groups

The command id shows your user identity and group memberships

Numeric user ID and user name


Primary group ID and group name
● $ id
uid=500(tux) gid=100(users) groups=100(users),
14(uucp),16(dialout),17(audio),33(video)

Secondary groups
Standard file permissions

● Every file and directory has a set of attributes:


– An owner (by default, the person who created it)
– A group (by default, the primary group of the person who created it)
– Three sets of access permissions, relating to:
● The owner of the file
● Users who are members of the file's group
● Everyone else (“others”)
● These attributes are shown in a long directory listing:
$ ls -l etclist
-rw-r--r-- 1 tux users 65584 2004-03-16 11:30 etclist

Group
Owner
Permissions for others
Permissions for members of group
Permissions for owner
Standard file permissions (continued)

● There are three access permissions in each set


– The meanings of these permissions differ slightly depending on whether
they are applied to a regular file or a directory

Permission Meaning for a regular file Meaning for a directory


Able to see the contents of the Able to list the contents of the
r (read)
file directory
Able to change the contents of Able to create or delete files or
w (write)
the file subdirectories
Able to run the file as a Able to make the directory
x (execute)
program or a script “current” or use it in a path name

The permissions are shown as a group of nine characters,


for example:
rwxr-xr-x
Changing access permissions with chmod

● The command chmod is used to change file permissions


● How to build yourself a chmod command in five easy lessons

u + r
chmod g - w file1 file2 …

o = x

1. Type the
command 4. Select any 5. List the files whose
name! combination permissions you want
of r, w, x to change

2. Select any 3. Select one of:


combination of: + Add to existing permissions
u User (owner) of file - Remove from existing permissions
g Group = Assign absolute permissions
o Other (rest-of-world)
Or ‘a’ (same as ugo)
Changing access permissions (continued)

● Only the owner of a file (or the superuser) can change the file’s
permissions
$ chmod u+x hello.txt
$ chmod go-w display.object
$ chmod a-wx opensource openwindows
$ chmod u=rw open*
● To set different access permissions for different users, either use two
chmod commands or separate changes with a comma:
$ chmod u=rwx index
$ chmod go=r index
Or:
$ chmod u=rwx,go=r index
Question: What do you think 'chmod u=rwx go=r index' does?
Representing file permissions in octal

● Since each of the file permissions (rwxrwxrwx) is either on or off, a


file’s permissions can be represented by 3 groups of 3 binary digits
– Then each set of 3 bits can be written as an octal digit (0-7)

rwx rw- r--

111 110 100

7 6 4

● This notation can be used by chmod; e.g.


$ chmod 644 hello.txt
$ chmod 400 hello.txt
● Some early versions of chmod, and a few other commands which deal
with access permissions, only understand the octal notation
Changing ownership with chown

● The chown command can change the ownership and group of a file
chown owner.group file1 file2 ...

Example:
$ chown root.wheel foo bar
● You can change just the owner:
$ chown root foo
● or you can change just the group:
$ chown .wheel bar
$ chgrp wheel bar
● Only root can change a file's owner
– Ordinary users can change a file's group only if they are members of both
the original and the new group
Exercise: File permissions and ownerships

1. You should initially be logged in as tux for this exercise


2. Using the touch command, create a file called sample
$ touch sample
– Who owns the file sample?
– What group does the file belong to?
– What are the initial access permissions on the file?
2. Using chmod, create the following sets of access permissions, in turn,
on the file sample. After each change, verify the permissions by doing
a long listing of the file
rw-------
rw-rw-rw-
rwxrwxrwx
Setting and querying the current directory

● Every process (including the shell) has a current directory


– Where it will look for 1relative path names
– The cd command changes the current directory.
– The pwd command displays the name of the current directory
$ cd cd with no arguments takes you
$ pwd to your home directory
/home/tux
$ cd pics Descend into a subdirectory
$ pwd
/home/tux/pics
$ cd /etc Change directory using an
$ pwd absolute pathname
/etc
'-' takes you back to your
$ cd -
previous directory
/home/tux/pics
$ cd .. '..' takes you up one level
$ pwd
/home/tux
Creating and deleting directories

● The mkdir command creates new directories


– -p option creates additional directories, if required, along the path
● The rmdir command deletes directories
– Only if they are empty

$ mkdir proposals/january
mkdir: cannot create directory `proposals/january':
No such file or directory
$ mkdir -p proposals/january
$ rmdir proposals
rmdir: `proposals': Directory not empty
$ rmdir proposals/january
$ rmdir proposals
$
Listing directory contents with the ls command

● The ls command lists files in a directory


– If a directory name is given, the contents of the directory are shown
– With no arguments, the current directory is listed
● ls has many options. Here are a few:

Option Meaning
None Display filenames only, in a multi-column listing

-l Display a 'long' listing, including file type, permissions, modification time, and
size, in addition to the name
-a Display 'hidden' files (ones whose names begin with a '.')

-F After each name, append a character to indicate the file type: '/' indicates a
directory, '*' indicates an executable file, '@' indicates a symbolic link
-i Display inode numbers (discussed later)
-t Sort by time of last alteration (by default, sort is alphabetic on file name)
-u Sort by time of last access
-R Recursive: descend into any subdirectories
-d When listing a directory, list just the directory entry, not the contents
Hidden files

● Files whose names begin with a '.' are “hidden”


– They do not show up on a normal directory listing
– Most of them are configuration and startup files, for example:
– .bash_history: Where the bash shell stores its command history
– .xinitrc: Startup file for the X window system
– .bashrc: Startup file for the bash shell
– .profile: Startup file for all shells
● Use ls -a to show hidden files
Copying files with the cp command

● The cp command copies files


$ cp file1 file2
– This form makes a copy of file1 under the name file2
$ cp file1 file2 ... dir
– This form makes copies of file1 file2 ... in (existing) directory dir
● Beware: the destination files will be replaced if they already exist
● Options include:
Option Meaning
-i Interactive mode: ask for confirmation before replacing an
existing file
-u If the destination file already exists, perform the operation
only if the source file is newer than the destination file
-l Create links instead of making copies
-s Create symbolic links instead of making copies
-r, -R Copy directories recursively
Renaming files with the mv command

● The mv command renames or moves files


$ mv file1 file2
This form renames file1 as file2.
$ mv file1 file2 ... dir
This form moves file1 file2 ... into (existing) directory dir

Beware: the destination files will be replaced if they already exist

Options include:

Option Meaning
Interactive mode: ask for confirmation before replacing an
-i
existing file
-u If the destination file already exists, perform the operation
only if the source file is newer than the destination file
Deleting files with the rm command

● The rm command deletes files


$ rm file1 file2 ...
● Beware: there is no 'undelete' command!
● Options include:

Option Meaning
-i Interactive mode: ask for confirmation before deleting
Normally rm will prompt for confirmation before deleting
a file on which you do not have write permission. The -f
-f
flag suppresses this prompt and forces rm to delete the
file
-r Recursive mode: delete all subdirectories and contents
Updating files with the touch command

● The command touch updates the access and modification


timestamps on a file to the current time
– Makes it appear that the file has just been modified
– If the file does not exist it is created with zero length
$ touch file1 file 2 ...
● Options for touch include:

Option Meaning
-a Update only the access timestamp
-m Update only the modification timestamp
-r file Update the timestamps to match those of file, not the
current time
-t time Update the timestamps to the specified time, in the
format [[CC]YY]MMDDhhmm[.ss]
File and directory permissions revisited

● The ways in which file access permissions control what you can do
derive from a few simple rules
– To access the data in a file, you need read permission on the file
– To modify the data in a file, you need write permission on the file
– To list the names (only) of the files in a directory, you need read
permission on the directory
– To list the attributes of the files in a directory (e.g. to perform an ls -l), or
to use the directory in a pathname, or to make it your current directory,
you need execute permission on the directory

(You need both read and execute permission to have useful access to a
directory)

File permission examples

Operation Situation Required permissions


cp file1 file2 file2 doesn’t r permission on file1
exist w permission on file2’s directory

cp file1 file2 file2 already r permission on file1


exists w permission on file2

mv file1 file2 w permission on file1’s directory


w permission on file2’s directory

rm file1 w permission on file1’s directory


Exercise: Managing files

1. Log in as tux
2. Copy the files passwd, group, hosts and fstab from /etc to your
home directory
3. In your home directory, rename the file fstab to table
4. Create a directory called private in your home directory
5. Change the permissions on private so that only you can access it
– Hint: the permissions should be rwx------

6. Move the files passwd and group into private


7. Change directory into the private directory and list the files there
8. Log out and log back in as dilbert with password adams
– Can you list tux's home directory?
– Can you list tux's private directory?
Filename expansion using wildcards

● The shell uses several meta-characters for matching patterns in file


names
– Commonly known as wildcards

The command as
typed The shell looks
in the current
directory for
matching file alias.pl
$ ls -l *.c
names edges.c
edges.o
main.c
… and substitutes main.o
them back into the wireframe
The command as command line
executed

$ ls -l edges.c main.c
Filename expansion using wildcards (continued)

* Matches zero or more characters


? Matches exactly one character
[ ] Matches any one of the enclosed characters, e.g. [AaTt]

[x-y] Matches any character in range


e.g. [a-m] [A-Z] [0-9]
● Linux has no notion of filename “extensions”
– * matches all names, like *.* in DOS

Multiple wildcards can be used
– E.g. rm [A-Z]*.html

Wildcards can be used in multiple components of a pathname
– E.g. rm backups/*.199[7-9]/expenses??
Wildcard quiz

Given these files in the current directory What do these commands do?

410-chap1.doc intro.old rm *.old


410-chap2.doc meetings.June ls -l 410-chap?.doc
410-chap3.doc meetings.July less 410-chap[2-5].doc
410-chap4.doc meetings.Aug mv ideas* training
410-chap5.doc oldstuff
mv ideas.* training
410-CHAPS.doc opensource
display openwindows
rm *old*
display.c project6
rm *
display.h project45 cp [v-z]* training
display.object project46 cp [a-z]* training
ideas project346 ls display.?
ideas.old training rm proj*6
ideas.older venues rm proj*[a-z]6
index windows
intro x-windows
A directory
Exercise: Wildcards

● Do a long listing of all the files in /usr/bin whose names contain a


digit
– How many such files are there?

Create a subdirectory called config in your home directory

Copy all the files whose name ends in .conf from /etc to your
config directory
– Can you find a way to supress the error messages due to unreadable
files?
● Change the access permissions of all files in your config directory to
be “rw-------”
● Are there any files in your config directory whose name begins with
a vowel?

Delete any files in your config directory whose name begins with a
vowel
Manual pages

● The traditional way of providing online help is the “manual page”


– accessed via the man command
tux@earth:~> man mount
MOUNT(8) Linux Programmer's Manual MOUNT(8)

NAME
mount - mount a file system

SYNOPSIS
mount [-lhV]

mount -a [-fFnrsvw] [-t vfstype] [-O optlist]


mount [-fnrsvw] [-o options [,...]] device | dir
mount [-fnrsvw] [-t vfstype] [-o options] device dir

DESCRIPTION
All files accessible in a Unix system are arranged in one big
tree, the file hierarchy, rooted at /. These files can be
spread out over several devices. The mount command serves to
attach the file system found on some device to the big file
tree. Conversely, the umount(8) command will detach it again.
How a manual page is organised

● Each manual page is divided up into a number of sections


– Not all sections are present for all commands

Section Contents
NAME Name and short description of the command
SYNOPSIS Description of the syntax
DESCRIPTION Detailed description of the command
OPTIONS Description of all available options
COMMANDS Instructions that can be given to the program while it is running
FILES Files referenced by the command
SEE ALSO List of related commands
DIAGNOSTICS Possible error messages and what they mean
EXAMPLE Examples of usage
AUTHOR Who wrote it
BUGS or WARNINGS Known errors and problems
Manual page section numbering

● Each manual page is allocated a 'section number' depending on what


kind of thing it describes:
Section Contents Used by
1 Commands for end users End users
2 System Calls Developers
3 Functions and Library Routines Developers
4 Device Files Administrators / developers
5 Configuration files and file formats Administrators
6 Games ?
7 Macro packages and file formats ?
8 System Administration Commands Administrators

● The output of man is piped through less for ease of browsing


Manual page section numbering (continued)

● By default the man command finds the requested page in the lowest-
numbered section
– If the same name appears in more than one section, you may have to
specify the section number explicitly to get the right man page

$ man crontab
... man page for crontab command in section 1 ...

$ man 5 crontab
... man page for crontab file format in section 5 ...

$ man uname
... man page for uname command in section 1 ...

$ man 2 uname
... man page for uname system call in section 2 ...
Searching the manual pages by keyword

● You can search for manual pages by keyword


– man -k keyword or apropos keyword
– keyword must appear in NAME section of man page
$ apropos partition
mpartition (1) - partition an MSDOS hard disk
sfdisk (8) - Partition table manipulator for Linux
gpart (8) - guess PC-type hard disk partitions
ntfsfix (8) - tool for fixing NTFS partitions altered by ...
mkfs.jfs (8) - create a JFS formatted partition
lvmdiskscan (8) - scan for all disks / multiple devices ...
jfs_mkfs (8) - create a JFS formatted partition
pvcreate (8) - initialize a disk or partition for use by LVM
cfdisk (8) - Curses based disk partition table manipulator
partprobe (8) - inform the OS of partition table changes
fdisk (8) - Partition table manipulator for Linux
parted (8) - a partition manipulation program
Exercises: Using the manual pages

1. For this exercise, log in as tux

2. Look up the man page for the ls command

– What option gives a 'long' listing?


– What option gives a color listing?
3. Look up the man page for the write command

What command did you use?


4. Look up the man page for the write system call

What command did you use?


5. Find the name of the command for formatting floppy disks
– Hint: Use apropos (and maybe grep)
6. Find the name of (a) the command and (b) the system call for changing
permissions on a file

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy