W2 Lecture Notes For Linux
W2 Lecture Notes For Linux
W2 Lecture Notes For Linux
Management
Week 2
Department of Computing
Students may now try the basic Linux commands described in Table 1.1 of
week 1
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
-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
~/scripts/scr1.sh
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:
-
(avoid using spaces before / after the = sign, Linux will not accept it)
$myval
(try:
echo
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
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
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
Notes
-
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
Note
-
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
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
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