W2 Lecture Notes For Linux

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

ISCG 6403 Network Operating Systems

Management
Week 2
Department of Computing

Prepared by: Dr. Bashar Barmada


Revised by: Sachin Sen

Install Fedora Live on USB1


This process takes around 30 minutes
1. Shift to Lab 2001 or 2003
2. On any machine go to D:\Documents and Settings\User\Desktop\Fedora 12
3. In this directory, there is Fedora Live Creator program, and the IOS image of
Fedora 12.
4. Install and Run Fedora Live Creator
5. Put USB1 on the machine, then from Fedora live creator window browse to Fedora
IOS image and create Fedora live on the USB.

Install full version of Fedora on USB2


This process takes around 30 minutes.
1. Back to Lab 2005
2. Boot the machine using Fedora Live USB
3. Follow the steps of installing the full version of Fedora to USB2 as described in week
1
4. Reboot the machine using USB2

Students may now try the basic Linux commands described in Table 1.1 of
week 1

Linux Command Syntax


Command

Options (or Switches)

Arguments

Options: (or switches) are to direct the Linux to use the command is a specific way.
Arguments: Are the objects that the command is applied on.

Examples:
To copy a file from one place to another:
cp

/home/tom/file1.txt

/home/tom/documents

but to copy a directory, use the option r:


cp

-r

/home/tom/documents

/home/tom/backup

To create a directory:
mkdir /home/tom/dir1
but to create a nested directory, use the option p:
mkdir -p

/home/tom/dir1/dir2/dir3/dir4

Scripts
Scripts allow Linux to deal with group of commands much easier. Several commands are
written in a file, and when this file is executed the commands will run in a sequential order.
Scripting is more complicated that only sequential execution of commands, where control
statements can be included such as while, for and if statements. Several examples will be
introduced during the course time.
Script 1
vi

~/scripts/scr1.sh
(this create a file called scr1.sh under directory called "scripts"
in the home directory. Suppose students created the scripts
directory in their home directory to put all their script files)

(i for insert)
clear
ls
date
whoami
Esc : wq

(this will save the file and quit)

Make the script executable:


At this stage scr1.sh is not a script, it is a normal text file. To make it a script it has to be
executable by adding the attribute x to it:
chmod +x

~/scripts/scr1.sh

To run the script:


./~/scripts/scr1.sh
Note: We are adding the full path to run the above command from any place in the file
system. If you are inside the working directory of the script you dont need to add the full
path, the above commands will be:
cd
~/scripts
vi
scr1.sh
chmod +x
scr1.sh
./scr1.sh
Notes:
-

To add an attribute to a file or directory use + sign (e.g. +x), to remove attribute use
sign (e.g. x)
The attributes are: x for execute, r for read and w for write. For more information
about how to use deal with the attributes use:
man chmod
The view the attribute for a directory or file use: ls -l
The executable file will be shown in a different colour in the file system.
ls
to see the colour of scr1.sh
Try

Script 2
Assume from now on you are inside the working directory of the scripts (type cd ~/scripts)
vi

scr2.sh

(i for insert)

Esc

useradd
passwd
userdd
passwd
useradd
passwd
:
wq

sue
sue
kim
kim
joe
joe

chmod +x
./scr2.sh

scr2.sh

Notes:
-

To be able to add users you need to be a superuser. Use su to become a superuser.


Linux will not accept a capital letters in the username (cannot use Sue for example)
When a new user is created, the user account is disabled until the password is set.

Deal with variables:


To create a variable called myval with value 10, simple type on the command prompt:
myval=10

(avoid using spaces before / after the = sign, Linux will not accept it)

To view the content of a variable use $ sign:


echo

$myval

(try:

echo

myval without the $ )

Script 3
Create a script to read usernames from a file then add them.
The Users list
vi

userlist1.txt

(i for insert)

Esc

sue1
kim1
joe1
:

wq

The script
vi

scr3.sh

(i)
cat
do

userlist1.txt

useradd

$un

while read

un

Esc

done
:
wq

chmod +x
./scr3.sh

scr3.sh

What is happening in scr3.sh


The script reads the content of userlist1.txt using cat but instead of printing the content to
the terminal it pipes it (using | ) to the next command to become an input. The next
command is while read where it reads the content of userlist1.txt line by line and each
line is stored in the variable un, then processes each variable with the statements
between do . done, in this case only useradd $un. Note that to deal with the content of a
variable use the $ sign.
Note: this script can be run for only one time. For the second time, Linux will give a warning
message as these users are already created.
Script 4:
Deal with the userlist file as a variable inside the script. So we dont hard coded userlist.txt,
but we provide it as an input variable for the script command
The Users list
vi

userlist2.txt

(i for insert)

Esc

sue2
kim2
joe2
:

wq

The script
vi

scr4.sh

(i)
cat
do

$1

useradd
Esc

done
:
wq

while read
$un

un

chmod +x
./scr4.sh

scr4.sh
userlist2.txt

What is happening in scr4.sh


$1 inside the script will be replaced with the 1st argument when we execute the script, in
this case the userlist2.txt. This will increase the usability of scr4.sh compared to scr3.sh, as
it can be used with a different users list without the need to change the code.
Note:
A command and its arguments are represented with variables starting from 0. To use any of
these variables we use $ sign in front of them. Example:
./scr4.sh
userlist2.txt
./scr4.sh
represented by $0
userlist2.txt represented by $1
./scr10.sh
userlist11.txt userlist12.txt userlist13.txt userlist14.txt
./scr10.sh
represented by $0
Userlis11.txt represented by $1
Userlist12.txt represented by $2
Userlist13.txt represented by $3
Userlist14.txt represented by $4

Script 5
Adding a group for the user
The Users list
vi

userlist3.txt

(i for insert)

Esc

sue3
kim3
joe3
:

wed
mon
wed
wq

The script
vi
(i)

scr5.sh
cat

$1

while read

un

gn

do
groupadd
useradd

$gn
-G $gn $un

done
:
wq

Esc

chmod +x
./scr5.sh

scr5.sh
userlist3.txt

What is happening in scr5.sh


-

Replace $1 with userlist3.txt


Read userlist3.txt line by line, put the username in un variable and the groupname
in gn variable
Add the group to the system
Add the user to the system and put it inside its group (using G $gn
in the
command useradd)

Notes
-

Use: man useradd


to see the different options of this command
When executing this script, Linux will give a warning if you try to add a group for the
second time. Later on, we will improve the script so it checks whether the group or
user exist before adding.

Script 6
Adding a group and comments for the user
The Users list
vi

userlist4.txt

(i for insert)

Esc

sue4
kim4
joe4
:

wed
mon
wed
wq

First year
First year
Second year

$1

The script
vi
(i)

scr6.sh
cat

while read

un

gn

com

do
groupadd
useradd

$gn
-G $gn

-c $com

$un

done
:
wq

Esc

chmod +x
./scr6.sh

scr6.sh
userlist4.txt

What is happening in scr6.sh


-

Replace $1 with userlist4.txt


Read userlist4.txt line by line, put the username in un variable, the groupname in
gn variable, and the rest of the line, which is the comment in com variable
Add the group to the system
Add the user to the system, put it inside its group (using G option) and add its
comment (using c option)

Note
-

The username comes at the end of useradd command


As variable com will have spaces in its value (First year or Second year), then
when using variables with useradd command, these variables mush be between the
double quotations .
To change the options of an existing user, use the command:
usermod
Type man usermod for help
To deal with the users password (for example change the expiry date of the
password), use the command:
chage
For example to view information about the password for user tom:
chage -l
tom

Exercise:
Add users with the following information to the system:
listuser5.txt
Username

Group

Password

Comments

sue5
kim5
joe5

Wed
Thu
Tue

Pass1
Pass2
Pass3

First year
Second year
Third year

Important files for users and groups


/etc/passwd
A text file contains the users in the system. Each user will have an entry with 7 fields:
username: x (use encrypted password) : UID (user id) : GID (group id): Home Dir :
comment : Login shell
Note: UID and GID below 1000 are reserved for system use.
To view the contents of the file:

cat

/etc/passwd

/etc/group
A text file contains the groups in the system. Each group will have an entry with 4 fields:
groupname: x: groupID : users of this group (if it contains users)
Notes
-

when a user is created, and User Private Group (UPG) with the same name of the
user is created as well, and that user is the only member of the UPG.
use gpasswd command to administer /etc/group (type man gpasswd for more
information)
The encrypted passwords of group are stored in /etc/gshadow

/etc/shadow
A text file contains the encrypted passwords of the user

The She-Bang line


It is always advised to direct the Linux to use a particular shell to interpret the commands
inside the script. Otherwise, Linux will use the default script. This line is called she-bang
and it should be the first line in the script. For example to use the bash shell, the first line
in the script will be:
#!/bin/bash

Command find
Used to search for a file inside a directory. For example, to search for a file called
scr3.shstaring from the root directory, the command will be:

find

-name

src3.sh

Note:
-

To use a different login terminal, use Ctrl+Atl+F2 , Ctrl+Alt+F3, .... (up to F6)
Linux will not let you login with a user, if that user does not a password. Use the
command passwd to set a valid password for a user
To go back to the original GUI terminal, use Crtl+Alt+F1

You might also like