Friday, June 8, 2012

Chapter 3 - Fundamental Command Line Skills

Now that I have been doing this blog for about the last 2-3 weeks, realizing I should have started much earlier . What is the proper timeframe to study for the RHCSA and RHCE exams ? 3 months, 6 months ? It depends I guess. I am going to have to pick up the pace here, because my exams are at the end of this month, so from this point on, blogs will unfortunately be less detailed and much more striaght to the point I think .

Shells included within the RHEL6 OS :

  • bash
  • dash
  • tcsh
  • zsh
The start-ttys.conf file at a glance :


[root@server01 init]# cat /etc/init/start-ttys.conf
#
# This service starts the configured number of gettys.

start on stopped rc RUNLEVEL=[2345]
env ACTIVE_CONSOLES=/dev/tty[1-6]
env X_TTY=/dev/tty1
task
script
        . /etc/sysconfig/init
        for tty in $(echo $ACTIVE_CONSOLES) ; do
                [ "$RUNLEVEL" = "5" -a "$tty" = "$X_TTY" ] && continue
                initctl start tty TTY=$tty
        done
end script


From the GUI Desktop to get to a virtual console : CTRL + ALT + F2 


And actually here is a table for commands regarding consoles :



consolekeystrokescontents
1ctrl+alt+f1installation dialog
2ctrl+alt+f2shell prompt
3ctrl+alt+f3install log (messages from installation program)
4ctrl+alt+f4system-related messages
5ctrl+alt+f5other messages
6ctrl+alt+f6x graphical display


Basic syntax for CLI

[root@server01 tmp]# date > file1.txt
[root@server01 tmp]# date > file2.txt
[root@server01 tmp]# date > file3.txt

[root@server01 tmp]# cat file1.txt
Fri Jun  8 05:04:31 CDT 2012

[root@server01 tmp]# cat < file2.txt
Fri Jun  8 05:04:34 CDT 2012

[root@server01 tmp]# cat file1.txt | less
[root@server01 tmp]#


[root@server01 tmp]# ls > filelisting.txt
[root@server01 tmp]# cat filelisting.txt
fcoemon.dcbd.1421
file1.txt
file2.txt
file3.txt
filelisting.txt
keyring-TKyVYZ
orbit-gdm
orbit-root
pulse-ScBzuHCNfj2o
pulse-vqQjuKKOwVS6
system-config-kickstart-tb-hsIvK3
system-config-kickstart-tb-tm98M8
tmp2F8Aw7
tmp4Z8wF7
tmpcP3VHt
tmpCuNCA0
tmpoQvvKy
tmpX6rBZi
yum.log
yum_save_tx-2012-06-06-12-41O9TJcW.yumtx
yum_save_tx-2012-06-06-19-22TMQUsD.yumtx


Again if you want more on shells, and CLI scripting, I have written an excellent article here : http://www.ibm.com/developerworks/aix/library/au-getstartedbash/index.html?ca=drs-

[root@server01 tmp]# netstat -an | head >> netstat.out
[root@server01 tmp]# cat netstat.out
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:38365               0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN
tcp        0      0 192.168.122.1:53            0.0.0.0:*                   LISTEN
tcp        1      0 192.168.0.12:56146          184.84.210.27:80            CLOSE_WAIT
tcp        0     52 192.168.0.12:22             192.168.0.3:54219           ESTABLISHED
[root@server01 tmp]#


Re-directing using /dev/null and the standard ERR handle :
http://www.cyberciti.biz/faq/how-to-redirect-output-and-errors-to-devnull/

[root@server01 tmp]# cd /var/www/html/;touch myfile1.txt
[root@server01 html]# pwd
/var/www/html
[root@server01 html]# ls -lrt myfile1.txt
-rw-r--r--. 1 root root 0 Jun  8 05:16 myfile1.txt


[root@server01 html]# ls -i myfile1.txt
1050775 myfile1.txt
[root@server01 html]# ls -Z myfile1.txt
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 myfile1.txt


[root@server01 ~]# yum install samba -y

Creating a hard link :
[root@server01 ~]# ln /etc/samba/smb.conf smb.conf
[root@server01 ~]# ls -i /etc/samba/smb.conf smb.conf
2756873 /etc/samba/smb.conf  2756873 smb.conf


Creating a symbolic link :
[root@server01 ~]# ln -s /etc/samba/smb.conf smb_sym.conf
[root@server01 ~]# ls -i /etc/samba/smb.conf smb_sym.conf
2756873 /etc/samba/smb.conf  1835109 smb_sym.conf

Removing files :

[root@server01 ~]# rm smb_sym.conf
rm: remove symbolic link `smb_sym.conf'? y
[root@server01 ~]# cp -p /etc/samba/smb.conf /etc/samba/smb.conf.bak
[root@server01 ~]# rm smb.conf
rm: remove regular file `smb.conf'? y
[root@server01 ~]# ls /etc/samba/smb.conf
/etc/samba/smb.conf


Creating and manipulating directories  :

[root@server01 ~]# mkdir a
[root@server01 ~]# mkdir -p a/b/test/c
[root@server01 ~]# ls -lrt a
total 4
drwxr-xr-x. 3 root root 4096 Jun  8 05:34 b


[root@server01 ~]# ls -lrt a
total 4
drwxr-xr-x. 3 root root 4096 Jun  8 05:34 b
[root@server01 ~]# ls -lRt a
a:
total 4
drwxr-xr-x. 3 root root 4096 Jun  8 05:34 b

a/b:
total 4
drwxr-xr-x. 3 root root 4096 Jun  8 05:34 test

a/b/test:
total 4
drwxr-xr-x. 2 root root 4096 Jun  8 05:34 c

a/b/test/c:
total 0


[root@server01 ~]# rmdir -p a/b/test/c
[root@server01 ~]#


 Command  aliases :

[root@server01 ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'


[root@server01 ~]# cat .bashrc
# .bashrc

# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi


Using the find command (much more exmaples later) :

[root@server01 ~]# find /etc -name httpd.conf
/etc/httpd/conf/httpd.conf


[root@server01 ~]# find /etc -name httpd.conf | xargs ls -l
-rw-r--r--. 1 root root 34418 May 28  2010 /etc/httpd/conf/httpd.conf


Using wildcards in the shell  :

[root@server01 ~]# ls -l /etc/httpd/conf/httpd.*
-rw-r--r--. 1 root root 34418 May 28  2010 /etc/httpd/conf/httpd.conf
[root@server01 ~]# ls -l /etc/httpd/conf/httpd.????
-rw-r--r--. 1 root root 34418 May 28  2010 /etc/httpd/conf/httpd.conf
[root@server01 ~]# ls -l /etc/httpd/conf/http[abcd].conf
-rw-r--r--. 1 root root 34418 May 28  2010 /etc/httpd/conf/httpd.conf


Using the locate command :

[root@server01 ~]# /etc/cron.daily/mlocate.cron
[root@server01 ~]#


[root@server01 ~]# locate httpd.conf
/etc/httpd/conf/httpd.conf


Using the find command :

[root@server01 ~]# file /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf: ASCII English text


Using the 'less' and the 'more' commands :

[root@server01 ~]# less /var/log/messages
[root@server01 ~]# more /var/log/messages


Using the 'head' and 'tail' commands :

[root@server01 ~]# cat /var/log/messages |head | tail -5
Jun  6 07:12:50 server01 kernel: Command line: ro root=/dev/mapper/vg_server01-lv_root rd_LVM_LV=vg_server01/lv_root rd_LVM_LV=vg_server01/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet
Jun  6 07:12:50 server01 kernel: KERNEL supported cpus:
Jun  6 07:12:50 server01 kernel:  Intel GenuineIntel
Jun  6 07:12:50 server01 kernel:  AMD AuthenticAMD
Jun  6 07:12:50 server01 kernel:  Centaur CentaurHauls
You have new mail in /var/spool/mail/root



Using sort command :

[root@server01 ~]# cat /etc/passwd | sort  |head
abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:496:490:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
[root@server01 ~]#


 Using 'diff' and 'sdiff' :

[root@server01 tmp]# diff file1.txt file2.txt
1c1
< Fri Jun  8 05:04:31 CDT 2012
---
> Fri Jun  8 05:04:34 CDT 2012
[root@server01 tmp]#
[root@server01 tmp]# sdiff file1.txt file2.txt
Fri Jun  8 05:04:31 CDT 2012                                  | Fri Jun  8 05:04:34 CDT 2012


 Using 'wc' :

[root@server01 tmp]# cat /etc/passwd | wc
     40      74    2037
[root@server01 tmp]# cat /etc/passwd | wc -l
40


Using 'sed' :

[root@server01 tmp]# echo "rasberry pie" > recipe.txt
[root@server01 tmp]# cat recipe.txt
rasberry pie
[root@server01 tmp]# sed 's/rasberry/apple/g' recipe.txt > newrecipe.txt
[root@server01 tmp]# cat newrecipe.txt
apple pie


Using 'awk' :

[root@server01 tmp]# awk '/rhill/ {print $1}' /etc/passwd
rhill:x:500:501:Roger



[root@server01 tmp]# grep rhill /etc/passwd
rhill:x:500:501:Roger Hill:/home/rhill:/bin/bash


[root@server01 tmp]# cat /etc/passwd | grep rhil| awk '{print $1}'
rhill:x:500:501:Roger


A good wiki for the vi editor commands, real admins use vi every day, but maybe not all of the features regularily, so here is the link :

http://forum.synology.com/wiki/index.php/Basic_commands_for_the_Linux_vi_Editor

There is also the commands :

  • vipw (opens /etc/passwd with vi) and put a lock on it
  • vigr (opens /etc/groupwith vi) and put a lock on it
  • vipw -s (opens /etc/shadow with vi) and put a lock on it
  • vigr -s (opens /etc/gshadowwith vi) and put a lock on it
  • visudo (opens /etc/sudoers with vi) and put a lock on it
Next. we'll look at a couple of screen shots of ther nano text editor, for you 'gui text' sissies ... lol :)

[root@server01 tmp]# nano /tmp/filelisting.txt



Those are keys commands listed mu be enter with key CTRL + 'key' ...



The default editor is kept in a file called /etc/environment, you could change it to by adding the line
export EDITOR=/bin/nano, but you don't want to do that becasue you're not really a sissy now, are you ?

If you want to seethe gedit program work (sorta just like Linux notepad) on the GNOME or KDE desktop, just install the gedit package :

yum install gedit -y

Remmber system help can be found with commands  :

  • man
  • info
  • apropos
  • whatis
  • file
  • The directories /usr/share/doc/ and /usr/share/info











 



















No comments:

Post a Comment