Showing posts with label BASIC LINUX COMMANDS. Show all posts
Showing posts with label BASIC LINUX COMMANDS. Show all posts

Thursday, December 31, 2009

SETUID IN REDHAT LINUX

SETUID:

Features:

a.ability to execute file as owner

b.chmod 4760 regextest.pl - this will ensure that the perl script always
executes as the user 'linuxcbt'

-rwsrw---- 1 linuxcbt sales 787 Jan 13 16:08 regextest.pl

's' in the execute position means that the program will execute as that user


SETGID:

Features:
Ability to enforce permissions to a directory structure
• mkdir /sales
• chmod 2775 /sales

Example:

Create a file in the '/sales' directory as 'linuxcbt'
• seq 1000000 > linuxcbt.1million.txt
chgrp:

Permits updating of group permissions

Sticky Bit:

Features:

Ability to ensure that users cannot delete others' files in a directory

drwxrwxrwt 23 root root 4096 Jan 13 15:05 /tmp/

/tmp - users cannot delete other user's files in '/tmp'

chmod 3777 /sales - ensures that /sales will not lose files from incorrect users

Task:
Set '/sales' using sticky bit and test
a. chmod 3777 /sales && ls -ld /sales OR chmod 777 /sales && chmod +t /sales

FILE TYPES-PERMISSIONS-SYMLINKS IN REDHAT LINUX

FILE TYPES-PERMISSIONS-SYMLINKS

Features:
1. The ability to restrict/control access to files

Note:

• 10 bits represent permissions for files (including directories)

• use 'ls -l' to examine permissions or GUI application like 'Nautilus'

-rwxrwxr-x 1 linuxcbt linuxcbt 681 Jan 13 11:31 regextest.pl

• 1st bit = file type. '-' = file, 'd' = directory

• 2nd - 4th bits = owner's permissions

• r = read = 4

• w = write = 2

• x = execute = 1

• -= none = 0

• 5th - 7th bits = group owner's permissions

• r = read = 4

• w = write = 2

• x = execute = 1

• -= none = 0

• 8th - 10th bits = everyone (world)

• r = read = 4

• w = write = 2

• x = execute = 1

• -= none = 0

Task:

1. Manipulate file permissions using 'chmod'

• chmod -x regextest.pl

• -rw-rw-r-- 1 linuxcbt linuxcbt 681 Jan 13 11:31 regextest.pl

• rw = 6 or 4+2 for owner

• rw = 6 or 4+2 for group owner

• r = 4 for everyone else (world)

• Octal notation: 664 for file 'regexetest.pl'

• chmod 664 regextest.pl - removes execution for ALL users

• chmod 775 regextest.pl - enables execution for ALL users


2. Ensure that 'regextest.pl' is rw by owner and noone else

• chmod 600 regextest.pl

Note: File will now be rw by owner (linuxcbt) and 'root'

3. Ensure that 'regextest.pl' is r by owner and noone else

• chmod 400 regextest.pl && ls -l regextest.pl

Note: chmod supports string values, which represent octal values

• chmod +/- x file

• chmod +/- w file

• chmod +/- r file

• chmod +/- u+x file - updates owner's execute permissions on the file

• chmod +/- o+x file - updates other's execute permissions on the file

• chmod +/- g+x file - updates group's execute permissions on the file

• chmod a+rwx = chmod 777

• chown - permits changing of ownership of files

chown root regextest.pl - changes ownership to 'root'

chown linuxcbt:sales regextest.pl - changes owner and group to
'linuxcbt:sales'

Task:

Update 'regextest.pl' so that owner and group owner may modify the file

• chmod 660 regextest.pl

USER/GRUOP MANAGEMENT IN REDHAT LINUX

USER/GRUOP MANAGEMENT

Features:
The ability to control users and groups

Primary tools:

1. useradd - used to add users and modify group membership

2. system-config-users

Task:

1. Create a user named 'student1' using 'useradd'

Note:

a.Default user settings derive from: /etc/login.defs

b.useradd student1

c.set password for user 'student1': passwd student1

d.Default User Accounts DB: /etc/passwd

e.student1:x:501:501::/home/student1:/bin/bash

f.username:shadow_reference:uid:gid:Description(GECOS):$HOME:$SHELL

Note:

a./etc/passwd is a world-readable file

b./etc/shadow now stores passwords in encrypted form

c./etc/shadow is NOT world-readable

Fields in /etc/shadow:

a.student1:$1$XSFMv2ru$lfTACjN.XxaxbHA0EkB4U0:13891:0:99999:7:::

b.username:

encrypted_password:
Days_since_Unix_epoch_password_was_changed (01/01/1970)
Days before password may be changed
Days after which the password MUST be changed
Days before password is to expire that user is warned
Days after password expires, that account is disabled
Days since Unix epoch, that account is disabled
Reserved field (currently unused)

2. Modify user 'student1' to have password expire after 45 days
usermod

Groups:

1. groupadd - adds new group

2. groups - lists groups on the system: /etc/group
/etc/group - maintains group membership information

Task:
a.Create a 'sales' group and add 'linuxcbt' and 'student1' as members
b.groupadd sales
c.usermod -G sales linuxcbt
d.usermod -G sales student1

Note: 2 types of groups exist:

1. Primary - used by default for a user's permissions
2. Supplemental - used to determine effective permissions

Note:
• use 'id' to determine the group information of user
• Create a new shell session to realize new group membership information
userdel/groupdel are used to delete users and groups, respectively

SYSTEM UTILITIES IN REDHAT LINUX

SYSTEM UTILITIES
Features:
1. Process listing

2. Free/available memory

3. Disk utilization

1. ps - process status/listing
ps -ef or ps -aux

2. top - combines, ps, uptime, free and updates regulary

3. uptime - returns useful system utilization information:
a.current time
b. uptime - days, hours and minutes
c. connected users
d. load averaged - 1,5,15 minute values

4. free - returns memory utilization
a.RAM
b.SWAP
c.free -m - for human readable format

5. df - returns disk partition/mount point information
a.df - returns info. using kilobytes
b.df -h - returns info. using megabytes/human readable (gigs/teray/etc.)

6. vmstat - reports on: processes, memory, paging, block I/O, traps, CPU activity
a.vmstat
b.vmstat -p /dev/hda1 - returns partitions stats for /dev/hda1 (/boot)

7. gnome-system-monitor - GUI, combining most system utilities

8. ls -ltr /proc
a.cat /proc/cpuinfo

9. kill PID - kills the process with a given PID

10. runlevel - returns runlevel information using 2 fields:
a.represents previous runlevel
b.represents current runlevel

AWK

AWK
Features:

1. Field/Column processor

2. Supports egrep-compatible (POSIX) RegExes

3. Can return full lines like grep

4. Awk runs 3 steps:

BEGIN - optional

Body, where the main action(s) take place

END - optional

5. Multiple body actions can be executed by separating them using semicolons. e.g. '{ print $1; print $2 }'

6. Awk, auto-loops through input stream, regardless of the source of the stream. e.g. STDIN, Pipe, File

Usage:

1. awk '/optional_match/ { action }' file_name | Pipe

2. awk '{ print $1 }' grep1.txt

Note: Use single quotes with awk, to avoid shell interpolation of awk's variables

3. awk '{ print $1,$2 }' grep1.txt

Note: Default input and output field separators is whitespace

4. awk '/linux/ { print } ' grep1.txt - this will print ALL lines containing 'linux'

5. awk '{ if ($2 ~ /Linux/) print}' grep1.txt

6. awk '{ if ($2 ~ /8/) print }' /var/log/messages - this will print the entire line for log items for the 8th

7. awk '{ print $3 }' /var/log/messages | awk -F: '{ print $1}'

TAR,GZIP,BZIP2,ZIP

TAR,GZIP,BZIP2,ZIP
Features:
Compression utilities (gzip, bzip2, zip)
File rollers (the ability to represent many files as one)
GZIP:
Includes:
gzip - compresses/decompresses files
gunzip - decompresses gzip files

Tasks:
1. compress '1million.txt' file using gzip
gzip -c 1million.txt > 1million.txt.gz

Note: gzip auto-dumps to STDOUT, by default

gzip -l 1million.txt.gz - returns status information
• gunzip 1million.txt.gz - dumps to file, and removes compressed version
• gzip -d 1million.txt.gz
• zcat 1million.txt.gz - dumps the contents to STDOUT
• less 1million.txt.gzip - dumps the contents of gzip files to STDOUT

Bzip2:

1. bzip2 -c 1million.txt > 1million.txt.bz2
Note: Bzip2 tends to outperform gzip on larger files
2. bunzip2 1million.txt.bz2
3. bzip2 -d 1million.txt.bz2
4. bzcat 1million.txt.bz2 - dumps contents to STDOUT
5. less 1million.txt.bz2 - also dumps the contents to STDOUT
Zip & unzip:
1. zip filename.zip path/ - general usage
2. zip 1million.txt.zip 1million.txt
Note: zip differs slight from gzip and bzip2 in that the destination file (resultant zip file) is specified before the source
3. unzip 1million.txt.zip


Tar & Gzip/Bzip2:
1. tar -cvf filename.tar path/ - creates a non-compressed archive
2. tar -cvf 1million.txt.tar 1million.txt
Note: tar, requires a small overhead for itself in each file
3. tar -czvf 1million.txt.tar.gz 1million.txt - creates, tar/gzip document
4. tar -cjvf 1million.txt.tar.bz2 1million.txt - creates, tar/bzip2 document
5. tar -tzvf
6. tar -cjvf 1million.txt.tar.bz2 1million.txt testRH5/- creates, tar/bzip2
document for the text file and 'testRH5' directory tree

COMMAND CHAINING

COMMAND CHAINING
Features:
1.Permits the execution of multiple commands in sequence
2.Also permits execution based on the success or failure of a previous command
3.cat 123.txt ; ls -l - this runs first command, then second command
without regards for exit status of the first command
4.cat 123.txt && ls -l - this runs second command, if first command
is successful
5.cat 1234.txt && ls -l
6.cat 123.txt || ls -l - this runs second command, if first command fails

1. more|less - paginators, which display text one-page @ a time
1.1)more /etc/fstab
1.2)less 1thousand.txt

2. seq - echoes a sequence of numbers
seq 1000 > 1thousand.txt - creates a file with numbers 1-1000

3. su - switches users
su - with no options attempts to log in as 'root'

4. head - displays opening lines of text files
head /var/log/messages

5. tail - displays the closing lines of text files
tail /var/log/messages

6. wc - counts words and optionally lines of text files
wc -l /var/log/messages
wc -l 123.txt

7. file - determines file type
file /var/log/messages

BASIC LINUX COMMANDS

BASIC LINUX COMMANDS

1. tty - reveals the current terminal
2. whoami - reveals the currently logged-in user
3. which - reveals where in the search path a program is located
4. echo - prints to the screen
1.echo $PATH - dumps the current path to STDOUT
2.echo $PWD - dumps ths contents of the $PWD variable
3.echo $OLDPWD - dumps the most recently visited directory
5. set - prints and optionally sets shell variables
6. clear - clears the screen or terminal
7. reset - resets the screen buffer
8. history - reveals your command history
!690 - executes the 690th command in our history
command history is maintained on a per-user basis via:
~/.bash_history
~ = users's $HOME directory in the BASH shell
9. pwd - prints the working directory
10. cd - changes directory to desired directory
'cd ' with no options changes to the $HOME directory
'cd ~' changes to the $HOME directory
'cd /' changes to the root of the file system
'cd Desktop/' changes us to the relative directory 'Desktop'
'cd ..' changes us one-level up in the directory tree
'cd ../..' changes us two-levels up in the directory tree
11. Arrow keys (up and down) navigates through your command history
12. BASH supports tab completion:
type unique characters in the command and press 'Tab' key


13. You can copy and paste in GNOME terminal windows using:
left button to block
right button to paste OR Ctrl-Shift-v to paste
14. ls - lists files and directories
ls / - lists the contents of the '/' mount point
ls -l - lists the contents of a directory in long format:
Includes: permissions, links, ownership, size, date, name
ls -ld /etc - lists properties of the directory '/etc', NOT the contents
of '/etc'
ls -ltr - sorts chronologically from older to newer (bottom)
ls --help - returns possible usage information
ls -a - reveals hidden files. e.g. '.bash_history'
Note: files/directories prefixed with '.' are hidden. e.g. '.bash_history'
15. cat - catenates files
cat 123.txt - dumps the contents of '123.txt' to STDOUT
cat 123.txt 456.txt dumps both files to STDOUT
cat 123.txt 456.txt > 123456.txt - creates new catenated file
16. mkdir - creates a new directory
mkdir testRH5 - creates a 'testRH5' directory
17. cp - copies files
cp 123.txt testRH5/
By default, 'cp' does NOT preserve the original modification time
cp -v 456.txt testRH5/
18. mv - moves files
mv 123456.txt testRH5/ - moves the file, preserving timestamp
19. rm - removes files/directories
rm 123.txt
rm -rf 456.txt - removes recursively and enforces
20. touch - creates blank file/updates timestamp
touch test.txt - will create a zero-byte file, if it doesn't exist
touch 123456.txt - will update the timestamp
touch -t 200801091530 123456.txt - changes timestamp

21. stat - reveals statistics of files
stat 123456.txt - reveals full attributes of the file
22. find - finds files using search patterns
find / -name 'fstab'
Note: 'find' can search for fields returned by the 'stat' command
23. alias - returns/sets aliases for commands
alias - dumps current aliases
alias copy='cp -v'