Linux Admin Manual
Linux Admin Manual
Table Of Contents
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
Symbolic Links
ls –l shows an (-l) to indicate it is a link
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
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 –smh/*/*/ grep | M
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:
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
Skip Dir > Ignores the man directory during search to reduce return:
find / -name man -prune -or \*.gz -print
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.
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
And then use the following command to check/report any that have changed :
md5sum –c /tmp/md5
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
sed is designed to take lines of text from the standard input and then send them onto
the standard output.
tar --create ↵
--verbose ↵
--gzip ↵
--one-file-system ↵
--same-permissions ↵
--file /dev/hda /home / /usr/local
Comand-Line Examples:
Backup /etc/passwd to backup1.tar
tar cvf /tmp/backup1.tar /etc/passwd
Installing a RPM
RPM – RedHat Package Manager
E-Mail
This is accessed by typing mail.
Or mail <username>
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
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)
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)
At Jobs
Running a file a specific date (23rd October 2001):
at 102301
at> who –u > who.log.102300
To complete use (Control –D)
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