Operating Systems Management Peter Davies
Linux Administration Manual
by Pete Davies
Table Of Contents
Table Of Contents ........................................................................................................1
Linux file system ..........................................................................................................2
Basic Commands..........................................................................................................2
System........................................................................................................................2
Help Pages .................................................................................................................2
Adding & deleting users/groups ................................................................................2
Linux File And Process Handling...............................................................................3
Inodes.........................................................................................................................3
Symbolic Links ..........................................................................................................3
List processes .............................................................................................................3
Stop processes............................................................................................................3
Operating Commands ................................................................................................3
File/Directory-Handling.............................................................................................3
Setting Up Shared Directories ...................................................................................3
Make a file .................................................................................................................4
Filesystem Commands ...............................................................................................4
Find A File .................................................................................................................5
Sticky-Bits..................................................................................................................5
SETGID .................................................................................................................5
SETUID .................................................................................................................6
Summary................................................................................................................6
Suspicious Files .........................................................................................................6
Using checksums to determine file alterations ......................................................6
Regular Expressions ....................................................................................................7
Backing-Up using TAR................................................................................................7
Comand-Line Examples: ...........................................................................................7
Installing a RPM ..........................................................................................................8
E-Mail............................................................................................................................8
Startup Scripts & Files ................................................................................................9
Run-levels ..................................................................................................................9
Startup ........................................................................................................................9
Login ..........................................................................................................................9
Cron Jobs & Timing ..................................................................................................10
Cron Jobs .................................................................................................................10
At Jobs .....................................................................................................................10
Code To Use................................................................................................................11
05/09/2006, 16:22 Page 1 of 11
Operating Systems Management Peter Davies
Linux file system
/sbin - executables used only by root
/bin - command executables used by all users
/usr - shared executables
/etc - configuration files
/var - system commands can write log files, lock files here
/proc - files that are either read or written to the Kernel
/etc/skel - these files are copied into new users home directory (templates)
Basic Commands
System
pwd display current directory
su root become root. Asks for root password
date +%m/%d/%y display current directory
free display free memory
alias dir=’ls’ setup aliases (unalias dir to remove)
Help Pages
man –k word looks-up word in whatis database and returns
topic & description.
whatis word searches short manual pages for keyword
apropos word finds all commands relating to the word
Adding & deleting users/groups
adduser pete add user “pete”
-G <group> add user & assign to group
userdel –r pete delete user “pete” and home directory (-r)
usermod pete modify user “pete” (-G <group> aswell)
passwd pete followed by prompt x2 asking for password
chfn pete add details to /etc/passwd
w lists all logged in users
groups lists all groups in /etc/group
groupadd <group> add group. Also <group1>,<group2>
groupdel <group> delete group name
gpasswd –a user grp adds user to group (–d to remove from group).
groupmod –n new old rename group old to new
05/09/2006, 16:22 Page 2 of 11
Operating Systems Management Peter Davies
Linux File And Process Handling
Inodes
Inodes specify where each files data blocks or set of data blocks are stored.
ls –i (-d) display inode number of file or (d)irectory
stat file display inode details of a specified file
touch alter the files last modified time
Symbolic Links
ls –l shows an (-l) to indicate it is a link
hard links use the same inode to reference files
ln existingfile.txt hardlink.txt
ln –s existingfile.txt softlink.txt
List processes
To list the currently running processes (ps = print status)
ps –e basic info
ps –au by user
ps –aef combo + daemon processes
top is a more user friendly UI
ls –l /proc/*/fd list files open by any process
fuser file list processes using <file>
jobs –l list all processes + PID
Stop processes
kill –9 pid -9 (or SIGKILL) kills difficult progs or
–1 restarts a daemon you just killed)
killall kills all running processes
Operating Commands
halt shuts machine down
init runlevel[0-6] changes runlevel of O/S
runlevel returns the current runlevel
File/Directory-Handling
cat file1 adds files together and displays/redirects
chmod perm file sets permissions for file
chown newowner file changes the owner of the file
chgrp newgroup file changes the group the file belongs to
Setting Up Shared Directories
The best solution for setting up a shared directory amongst several users is as follows:
1. create a group called “lefthanded”
2. make all left-handed people a member
3. create “lefthanded” directory
a. set the directory group to “lefthanded”
b. set the SETGID bit for the directory
05/09/2006, 16:22 Page 3 of 11
Operating Systems Management Peter Davies
Commands
1. groupadd lefthanded
2. adduser <user> -G lefthanded
3. passwd <user>
4. mkdir /lefthanded
5. chmod 2777 /lefthanded
Now, this means that, (due to the umask default 022), other users can’t edit your docs,
just read them. To overcome this simply create the directory and change the
ownership of the directory to that of the “lefthanded” group. Then set the default
umask to 002 (775) of the directory :
mkdir /lefthanded
chown root.lefthanded /lefthanded
chmod 2775 /lefthanded
Make a file
echo “hello” > file
sort namelist > newfile
cat newfile > newerfile
vi filename
Filesystem Commands
fdisk –l display all file information
showmount shows all mounted devices
mount /dev/floppy mount the floppy
umount /dev/floppy un-mount the floppy
du disk usage (see below for examples)
df disk filesystem usage
To determine how much diskspace is left on the system you can use the command
“df”.
Col.1 lists the pathname of file representing the partition.
Col.2 lists the number of 1K blocks in a particular partition.
Col.3 lists the blocks in use.
Col.4 lists the blocks available.
To determine disk usage and get meaningful output, use the following :
du </*> The space root directories are using.
Col.1 represents the disk space in kilobytes of
files within directories listed in Col.2
du –s /etc Summarise total disk space of /etc
or the 2nd level directories using >= 1MB space
du –smh/*/*/ grep | M
05/09/2006, 16:22 Page 4 of 11
Operating Systems Management Peter Davies
Find A File
To display the diskspace used by all Text files starting in the root directory where the
file extension ends with txt, and then print the firstline of the file:
du –ch `find / –name *.txt –print` | head –1
To find all MS-Word file use the following line of code :
find /usr -name *.doc –print
To processes files matching criteria a certain criteria you can use -exec.
find /usr –name \*.doc -exec cp {} {}.bak \;
You can then use the following arguments to set the output :
cp to copy files
rm to remove files
{} removes all files
{}{} copies all files to .bak
\; terminates command
-ok gives a visible confirmation on the decision
-iname if you want the search to be case-sensitive
find / -group <groupname> –print
-user <username> -print
Skip Dir > Ignores the man directory during search to reduce return:
find / -name man -prune -or \*.gz -print
To list all (from root /)regular files with > 5 hardlinks
find / -type f –and –links +5 –ls
Sticky-Bits
Sticky-bits allow groups and users to have different permissions and access rights on
an individual or per group basis. I shall explain:
SETGID
Ensures that all members of a particular group get “group permissions” for files
within the directory. This is done by creating a directory and chgrp-ing it to set the
directories group then use the SETGID for the directory.
“Owner” is always the username of the user who created the file
“Group” is now the group owning the containing directory
Any files created in directory are accessible by the group’s members.
05/09/2006, 16:22 Page 5 of 11
Operating Systems Management Peter Davies
SETUID
This allows an executable script to run with the privileges of the person who is
running it. This is a bad idea as I will demonstrate :
Steps
1. Login as root, and then chmod 4755 /bin/cat
2. SETUID sticky-bit is now set. The command cat is owned by root so it will
run with the privileges of root.
Test
1. Now login as another user and cat /etc/shadow
2. The complete shadow file is visible due to the SETUID sticky-bit.
Summary
chmod 0xxx turn off both setuid & setgid bits
2xxx set setgid bit
4xxx set setuid bit
Suspicious Files
Check for executable files owned by root that have SETUID bit set.
ls –l /bin | grep `^…s`
ls –l /usr/bin | grep `^…s`
This checks column 4 for “s”. Any command that modifies the password or shadow
files will appear: su, mount, passwd, chfn, chsh
Using checksums to determine file alterations
md5sum /bin/* make checksums for all files
Produces a checksum for each file when logged in as root
md5sum /bin/* > /tmp/md5
Now change the information in a file (e.g. modify the comments)
vi /bin/vimtutor
And then use the following command to check/report any that have changed :
md5sum –c /tmp/md5
05/09/2006, 16:22 Page 6 of 11
Operating Systems Management Peter Davies
Regular Expressions
grep is a filter that searches for string patterns within text. For example, to find out
if a user exists on a system you can :
grep pete /etc/passwd
Special characters :
^ sol
$ eol
\< start of word
\> end of word
. any single text character
[str] any single character in str
[^str] not in str
\ turns off special meaning of chars
Searching for a directory where users have write permissions (7 dots):
ls –l / | grep `^d…….w`
sed is designed to take lines of text from the standard input and then send them onto
the standard output.
sed 3q /etc/passwd prints three lines then quits
Backing-Up using TAR
The following example backs-up the root (/), /home, and /usr/local to the
hard disk a.
tar --create ↵
--verbose ↵
--gzip ↵
--one-file-system ↵
--same-permissions ↵
--file /dev/hda /home / /usr/local
tar cvz1pf /dev/hda /home / /usr/local
tar –cvMf /dev/fd0 /tmp/file
and when the disk runs out of space you will be prompted for another.
Comand-Line Examples:
Backup /etc/passwd to backup1.tar
tar cvf /tmp/backup1.tar /etc/passwd
Restore everything. (\*.doc) restore only doc files
tar xvf /tmp/backup1.tar ()
Backup all except (subdirectory)
tar cvf /tmp/docs.tar `find / -name (proc) -prune -o –name \*.doc -print`
05/09/2006, 16:22 Page 7 of 11
Operating Systems Management Peter Davies
Examine a tar file
tar tvf (/tmp/docs.tar)
Copy osman directory to a backup directory :
mkdir /backup
tar cvf /tmp/backup.tar /osman
cd /backup
tar xvf /tmp/backup.tar
Copy without making filename
cd /backup && tar cf - /osman | tar xf
Lists /home files changed since backup.tar was created
find /home –newer /tmp/backup.tar -print
Lists all files changed in last 7days.(-atime or -amin) minutes
find / -atime +7 -print
Lists all modified lines between 2 files
diff <file1> <file2>
Installing a RPM
RPM – RedHat Package Manager
There are basically 5 steps :
1.) Install an rpm file (first check it out) :
rpm –qip /osman/mcountd-0_4-1_i386.rpm
2.) Now install
rpm –ivh /osman/mcountd-0_4-1_i386.rpm
3.) Find out what exactly you installed :
rpm –qlp /osman/mcountd-0_4-1_i386.rpm
4.) To un-install it
rpm –e mcountd
5.) And to simply upgrade the package
rpm –Uvh /osman/fortune-mod-1_2_1-1_i386.rpm
E-Mail
This is accessed by typing mail.
Or mail <username>
05/09/2006, 16:22 Page 8 of 11
Operating Systems Management Peter Davies
Startup Scripts & Files
Run-levels
Runlevel 0 halt
1 single user
3 full multi-user
6 reboot
init(1)
Only root can set the runlevel
Startup
/etc/skel/.bash_profile startup bash script
/etc/skel/.cshrc startup c shell script
by putting the following code into this you can display user info :
echo “your home directory is ‘pwd’”
alias ls=’ls –al’
Login
vi /etc/rc.d/init.d/autoexec
#!/bin/sh
. /etc/rc.d/init.d/functions
case “$1” in start
action “start commands here” /bin/true;;
action “stop commands” /bin/true;;
esac
exit 0
To add the above script to the system when it starts at runlevel 3 do :
ln –s/etc/rc.d/init.d/autoexec /etc/rc.d/rc3.d/S99autoexec
And then to start and stop a runlevel login script you can simply :
chmod +x /etc/rc.d/init.d/autoexec
/etc/rc.d/init.d/autoexec start (or stop)
05/09/2006, 16:22 Page 9 of 11
Operating Systems Management Peter Davies
Cron Jobs & Timing
Cron Jobs
crontab (-l)(-r) will list and remove respectively
Type the following command: cron
x1 x2 x3 x4 x5
x1 minute 0-59
x2 hour 0-23
x3 dom 1-31
x4 month 1-12
x5 dow 0-7 (0 and 7 are both Sunday)
the wildcard stars (*) mean “on which ever x”
a) The following runs a script called birthday every year on my birthday
25 8 29 Jan * birthday
b) Run a script only during week days
25 8 * * mon,tue,wed,thu,fri appointments
At Jobs
Running a file a specific date (23rd October 2001):
at 102301
at> who –u > who.log.102300
To complete use (Control –D)
Running a file a specific time (12:00):
at 12:00
at> backup_script
Running a file one hour from now:
at now + 1 hour
at> clean_up_script
05/09/2006, 16:22 Page 10 of 11
Operating Systems Management Peter Davies
Code To Use
If the case may arise that you need to develop any code for say logging in, or listing
users, the following code may help :
#!/bin/bash
# go through listing and rename and zip
for d in `ls /usr/local/calldocs/*`;
do mv $d $d.2001
gzip $d.2001
#usage is: user1 login_name
if grep “^$1:” /etc/passwd > /dev/null 2 > /dev/null
then echo “$1 is a valid login name”
else echo “$1 is not a valid login name”
fi
exit 0
#usage is: user1 login_name
if who | grep $1 > /dev/null
then echo “User $1 is logged in”
else echo “User $1 is not logged in”
fi
NB: /dev/null is used as a dump for any unwanted data
05/09/2006, 16:22 Page 11 of 11