Redhat Enterprise Linux Basics Commands
Redhat Enterprise Linux Basics Commands
Page 1
Page 2
Page 3
Note: Use input redirection when program does NOT default to file as input
Output redirection '>':
1. cat 123.txt > onetwothree.txt
Note: Default nature is to:
1. Clobber the target file
2. Populate with information from input stream
Append redirection '>>':
1. cat 123.txt >> numbers.txt - creates 'numbers.txt' if it doesn't exist, or
appends if it does
2. cat 456.txt >> numbers.txt
Pipes '|':
Features: Connects the output stream of one command to the input stream of a
subsequent command
1. cat 123.txt | sort
2. cat 456.txt 123.txt | sort
3. cat 456.txt 123.txt | sort | grep 3
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
1. cat 123.txt ; ls -l - this runs first command, then second command without
regards for exit status of the first command
2. cat 123.txt && ls -l - this runs second command, if first command is
successful
3. cat 1234.txt && ls -l
4. cat 123.txt || ls -l - this runs second command, if first command fails
24. more|less - paginators, which display text one-page @ a time
1. more /etc/fstab
2. less 1thousand.txt
25. seq - echoes a sequence of numbers
a. seq 1000 > 1thousand.txt - creates a file with numbers 1-1000
26. su - switches users
a. su - with no options attempts to log in as 'root'
27. head - displays opening lines of text files
a. head /var/log/messages
28. tail - displays the closing lines of text files
a. tail /var/log/messages
For Public Use
Page 4
Page 5
'linux' grep1.txt
-i 'linux' grep1.txt - case-insensitive search
'^linux' grep1.txt - uses '^' anchor to anchor searches at the beginning of
-i '^linux' grep1.txt
-i 'linux$' grep1.txt - uses '$' anchor to anchor searches at the end of lines
Page 6
a. BEGIN - optional
b. Body, where the main action(s) take place
c. 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}'
Sed - Stream Editor
Features:
1. Facilitates automated text editing
2. Supports RegExes (POSIX)
3. Like Awk, supports scripting using '-F' option
4. Supports input via: STDIN, pipe, file
Usage:
1. sed [options] 'instruction[s]' file[s]
2. sed -n '1p' grep1.txt - prints the first line of the file
3. sed -n '1,5p' grep1.txt - prints the first 5 lines of the file
4. sed -n '$p' grep1.txt - prints the last line of the file
5. sed -n '1,3!p' grep1.txt - prints ALL but lines 1-3
6. sed -n '/linux/p' grep1.txt - prints lines with 'linux'
7. sed -e '/^$/d' grep1.txt - deletes blank lines from the document
8. sed -e '/^$/d' grep1.txt > sed1.txt - deletes blank lines from the document
'grep1.txt' and creates 'sed1.txt'
9. sed -ne 's/search/replace/p' sed1.txt
10. sed -ne 's/linux/unix/p' sed1.txt
11. sed -i.bak -e 's/3/4' sed1.txt - this backs up the original file and creates a new
'sed1.txt' with the modifications indicated in the command
Note: Generally, to create new files, use output redirection, instead of allowing sed
to write to STDOUT
Page 7
Page 8
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
User/Group Management
Features:
1. The ability to control users and groups
Primary tools:
1. useradd - used to add users and modify group membership
2. system-config-users
Example:
1. Create a user named 'student1' using 'useradd'
Note: Default user settings derive from: /etc/login.defs
a. useradd student1
b. set password for user 'student1': passwd student1
Default User Accounts DB: /etc/passwd
student1:x:501:501::/home/student1:/bin/bash
username:shadow_reference:uid:gid:Description(GECOS):$HOME:$SHELL
Note: /etc/passwd is a world-readable file
Note: /etc/shadow now stores passwords in encrypted form
Note: /etc/shadow is NOT world-readable
Fields in /etc/shadow:
student1:$1$XSFMv2ru$lfTACjN.XxaxbHA0EkB4U0:13891:0:99999:7:::
1.
2.
3.
4.
5.
6.
7.
8.
9.
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)
Page 9
Groups:
1. groupadd - adds new group
2. groups - lists groups on the system: /etc/group
/etc/group - maintains group membership information
Example: Create a 'sales' group and add 'linuxusr' and 'student1' as members
1. groupadd sales
2. usermod -G sales linuxusr
3. 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
Note: Create a new shell session to realize new group membership information
userdel/groupdel are used to delete users and groups, respectively
File Types - Permissions Symlinks
Features:
1. The ability to restrict/control access to files
Note: 10 bits represent permissions for files (including directories)
Note: use 'ls -l' to examine permissions or GUI application like 'Nautilus'
-rwxrwxr-x 1 linuxusr linuxusr 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
Example:
For Public Use
Page 10
Page 11
SETGID:
Features:
1. Ability to enforce permissions to a directory structure
mkdir /sales
chmod 2775 /sales
Create a file in the '/sales' directory as 'linuxusr'
seq 1000000 > linuxusr.1million.txt
chgrp:
Permits updating of group permissions
Sticky Bit:
Features:
1. 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
Example:
1. Set '/sales' using sticky bit and test
a. chmod 3777 /sales && ls -ld /sales OR chmod 777 /sales && chmod +t
/sales
Symlinks
Features:
1. Provides shortcuts to files (including directories)
2. Provides hard links to inode (file system) locations
Soft Links:
1. ln -s source_file target
a. ln -s ./regextest.pl lastscript.pl
Note: Soft links may span multiple file systems/hard drives
Note: Symlink count is NOT increased when using soft links
2. ln -s /home/linuxusr/testRH5/regextest.pl . - this will symlink (soft) to the /boot
file system
Note: With soft links, if you change the name or location of the source file, you will
break ALL of the symlinks (soft)
For Public Use
Page 12
Hard Links:
Features:
1. The ability to reference the same inode/hard drive location from multiple places
within the same file system
a. ln source target
ln regextest.pl ./testhardregextest.pl - creates a hard link
Quotas
Features:
1. Limits disk usage (blocks or inodes)
2. Tied to file systems (set on a per file system basis)
3. Can be configured for users and groups
Steps to enable quota support:
1. Enable quota support per file system in: /etc/fstab
a. defaults,usrquota,grpquota
2. Remount the file system(s)
a. mount -o remount /
b. use 'mount' to confirm that 'usrquota,grpquota' support are enabled
3. Create quota database files and generate disk usage table
a. quotacheck -mcug / - this creates /aquota.user & /aquota.group
b. quotacheck mavug
4. Assign quota policies
a. edquota username - set blocks/inodes soft_limits hard_limit
edquota student1 - sets quotas for user 'student1'
export EDITOR=nano - to have edquota default to 'nano' editor
5. Check quotas
a. quota username
quota student1
Note: place 'quotacheck -avug' in /etc/cron.*(hourly,daily)
6. Report on usage
a. repquota -a - this reports on usage
Note: The blocks are measured in 1K increments. i.e. 20000 blocks is roughly 20MB
Basic Provisioning of Partitions and File Systems
Features:
1. Ability to provision extra storage on-the-fly
Steps:
For Public Use
Page 13
Page 14
Page 15
Page 16
Page 17
f. update: /etc/fstab
Note: use 'mdadm --query /dev/md0' to get information about a RAID device
Note: You may create RAID volumes/devices on a single or on multiple disks
Ideally, your RAID volumes should span multiple physical disks to improve:
a. reliability
b. performance
c. availability
2. Remove the RAID-1 device
a. umount /dev/md0
b. mdadm --manage --stop /dev/md0
3. Create a RAID-5 Volume
a. fdisk /dev/sdb - to create a partition number 7
b. partprobe /dev/sdb - to update the kernel's view of the partition table
c. mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sdb5
/dev/sdb6 /dev/sdb7
d. watch cat /proc/mdstat - refreshes every 2 seconds
e. Overlay a file system: mke2fs -j /dev/md0
f. mount /dev/md0 /raid5
g. Test I/O to RAID-5 device
h. Update: /etc/fstab
RPM
Features:
1. Provides package management
a. Query
b. Install
c. Uninstall
d. Upgrade
e. Verify
2. Auto-verifies packages using GPG, MD5, SHA1SUMs
3. Automatically reports on unresolved dependencies
'rpm'
Query:
1. rpm -qa - dumps all installed packages
2. rpm -qa | wc -l - this dumps all packages and provides a count
3. rpm -qa | grep -i nano
4. rpm -qi nano - dumps info. about the 'nano' package as it's recorded in the local
RPM database
5. rpm -qf /usr/bin/nano - dumps package membership info. for the 'nano' file
6. rpm -qpi http://192.168.1.101/RH5/i386/Server/dhcp-3.0.5-7.el5.i386.rpm dumps info. about the uninstalled 'dhcp' package, which resides on the repository
7. rpm -ql package_name - returns all included files
Page 18
Verify:
1. rpm -Va - verifies ALL packages on the system, returning info. only if there are
discrepancies from the original installation
2. rpm -Vf /usr/bin/nano
Example: Change '/usr/bin/nano' then verify
SM5....T /usr/bin/nano
S(file size), M(mode or permissions), 5(MD5), T(mod time)
3. rpm -Vp nano
Install (Does NOT overwrite previous package):
Note: Use this method to install a new version of the kernel
1. rpm -ivh *.rpm
2. rpm -ivh http://192.168.1.101/RH5/i386/Server/dhcp-3.0.5-7.el5.i386.rpm
Upgrade (Installs or overwrites existing package):
1. rpm -Uvh *.rpm
2. rpm -Uvh http://192.168.1.101/RH5/i386/Server/dhcp-3.0.5-7.el5.i386.rpm
Freshen (Updates an existing package):
Note: Will NOT install the package, if it doesn't exist locally
1. rpm -Fvh *.rpm - freshens the current version of a package
Removal:
1. rpm -ev *.rpm - removes a package
Note: removal process considers dependencies and will complain if the removal will
break 1 or more packages. To get around this, use '--nodeps' option with 'rpm -ev
--nodeps *.rpm'
2. rpm -ev gftp
Package Management GUI:
1. Add/Remove Software
2. system-config-packages
YUM Configuration
Features:
1. The ability to centralize packages (updates)
Installation & Setup:
1. Install 'createrepo*rpm'
2. Setup directory structure
a. /srv/www/linuxsrv.com/RH5/yum
Page 19
Cron - Scheduler
Features:
1. Scheduler
2. Rules (Cron entries) are based on times:
a. minute (0-59)
b. hour (0-23)
c. day of the month (1-31)
d. month (1-12)
e. day of the week (Sun,Mon,Tue, etc. OR 0-7)
f. command to execute (shell, perl, php, etc.)
3. Wakes up every minute in search of programs to execute
4. Reads cron entries from multiple files
5. Maintains per-user and system-wide (/etc/crontab) schedules
/etc:
cron.d/
cron.deny - denies cron execution by user
cron.monthly/ - runs jobs monthly
cron.weekly/ - runs jobs weekly
For Public Use
Page 20
SysLogD
Features:
1. Handles logging
2. Unix Domain Sockets (/dev/log)
3. Internet Sockets (UDP:514)
4. Ability to log to local and remote targets
Implanted as 'sysklogd' package
Primary configuration file: /etc/syslog.conf
Standard syslog.conf file contains:
1. Rules
a.facilities -> applications/daemons/network device/etc.
b. levels -> Importance of message
Range: 0-7
7 = emergency (less information)
6 = alert
5 = critical
4 = error
3 = warning
For Public Use
Page 21
2 = notice
1 = info
0 = debug (more information)
2. Targets
a. file - /var/log/messages
b. tty - /dev/console
c. remote hosts - @IP_ADDR_of_REMOTE_HOST
'*' = catchall/wildcard to mean any facility or level
'.none' = exclusion rule
'man syslog.conf' to learn about the support facilities.levels
Example:
1. Enable UDP logging for remote Cisco gateway (192.168.1.1)
a. netstat -nul | grep 514 - reveals UDP:514 listener
b. nano /etc/sysconfig/syslog
b1. 'SYSLOGD_OPTIONS="-r"'
c. restart syslog and confirm UDP:514 listener
c1. confirm using 'netstat -nul | grep 514'
d. Configure the router using facility 'local0' and level 'info'
e. configure /etc/syslog.conf to accept 'local0.info'
f. restart or reload 'syslog'
Log Rotation
Features:
1. Rotation of logs based on criteria
a. size
b. age (daily, weekly, monthly)
2. Compression
3. Maintain logs for a defined period
/etc/logrotate.conf - primary (global) config file for all logs
-can be overridden by context-sensitive files. i.e. apache
run 'man logrotate'
/etc/logrotate.d - directory for logs to be rotated
-httpd - used to rotate Apache logs
/var/log/httpd/*log {
missingok
notifempty
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
For Public Use
Page 22
}
Example: Setup rotation rule for Cisco log
1. Create entry in: /etc/logrotate.d based on /etc/logrotate.d/syslog
2. Modified the entry to rotate based on new criteria
3. Rotated using: 'logrotate /etc/logrotate.conf'
Note: Force using: 'logrotatate -f /etc/logrotate.conf'
Common Network Utilities
Features:
1. Useful for basic troubleshooting
PING:
Features:
1. ability to communicate with hosts using ICMP
a. PING sends ICMP echo-requests
b. PING expects to receive ICMP echo-replies
Example: PING some hosts and evaluate the output
1. ping localhost (127.0.0.1)
2. ping -c 3 localhost - sends 3 ICMP echo-requests
Note: 'ping localhost' performs name resolution using /etc/hosts
/etc/hosts stores static name-to-IP mappings
Note: 127.0.0.0/8 is fully-reserved to the loopback adapter of ALL IPv4 hosts
3. ping -c 3 192.168.1.99
4. ping -c 3 -i 3 192.168.1.99 - delays PINGs to 3 seconds apart
Note: PING defaults to a standard 1-second interval
Note: Firewall(s) may block ICMP traffic, causing PING to fail
TELNET:
Features:
1. Great for basic TCP port diagnosis
Example:
1. Connect to TCP ports on various hosts
a. telnet 192.168.1.101 22
b. telnet www.linuxsrv.com 80
NETSTAT:
Features:
1. Provides network connection information from /proc/net/*
Example:
For Public Use
Page 23
Page 24
Page 25
b. /boot/grub/grub.conf
3. Remove traces of former kernel using 'rpm -e [--nodeps]'
a. kernel-2.6.18-8.el5 - removes older version
b. kernel-headers-2.6.18-8.el5 - force remove ignoring dependencies 'rpm -e
--nodeps kernel-headers-2.6.18-8.el5'
c. kernel-devel-2.6.18-8.el5
4. Install new 'kernel-headers' and 'kernel-devel' packages using YUM:
a. yum -y install kernel-headers
b. yum -y install kernel-devel
5. Confirm that the 3 'kernel-*' packages are installed:
a. rpm -qa | grep kernel
Note: Removal of older kernel-* packages cleans up:
a. /boot
b. /boot/grub/grub.conf (menu.lst)
Page 26