Sunday, 31 May 2015

Redhat Linux, CentOS - Basic

Basic Commands

who
who am i
w
logname
id
groups

last
last reboot
utmpdump /var/log/wtmp  == recent login and reboot

lastb -- list unsuccessful logins
utmpdump /var/log/btmp

uname -a --> uname --all == display OS name
date --set "Tue Jul 24 07:48:00 SGT 2014"

cal  == calendar
which == shows absolute path of the command that will execute
whereis == displays the binary name and full pathname of command along with locaiton of man

Compression/Zip Commands

tar cvf /tmp/home.tar /home     == create home.tar from /home
tar rvf /tmp/home.tar /etc/xinetd.d  ==append /etc/xinetd.d to home.tar
tar tvf /tmp/home.tar  == list content of home.tar
tar cvf /tmp/files.tar /etc/host.conf /etc/ntp.conf /etc/yum.conf
tar xvf /tmp/files.tar ==to extract

tar cvzf /tmp/home.tar.gz /home ==create a tarball and compress it with gzip
tar cvjf /tmp/home.tar.bz /home ==create a tarball and compress it with bzip

zip /tmp/files.zip /etc/host.conf /etc/ntp.conf /etc/yum.conf == compressed & add a zip file
unzip /tmp/files.zip

gzip /root/install.log /root/install.log.syslog == compress the files & add .gz extension
gunzip /root/install.log.gz
gzip -d install.log.syslog.gz

bzip2 /root/install.log /root/install.log.syslog == compress the files & add .bz2 extension
bunzip2 /root/install.log.bz2 /root/install.log.syslog.bz2
bzip2 -d /root/install.log.bz2

Linux File Systems Tree

/bin (binary) contains crucial user executable commmands.

/lib (library) contains shared library files required by kernel and other programs.

/sbin (system binary) contains most commands required at system boot up, crucial system administration commands that are not intended for normal users (require root priviledge)
/etc (etcetera) holds most system configuration files (eg. sysconfig, default). Dynamic data files.

lost+found directory hold files that become orphan(a file that has lost its name) after system crash; hold dynamic information.

/root - default home directory for root user

/net - all available NFS file systems on the network get mounted under their corresponding hostnames beneath /net.

/media - is used to automatically mount removable media.

/boot (boot file system) -Disk-based contains linux kernel, bootloader, boot config files and other files required to boot RHEL/Cent OS.

/var (variable file system) -Disk-based contains data that frequently changes while system is operational (e.g /var/log)
  /var/log contains most system log files./var/spool/mail - user mailboxes. /var/opt contains log, status for softwares installed in /opt. /var/spool holds print jobs, cron jobs, email messages, other queued items being sent out. /var/tmp contains large temp file or temp files that need to exist for extended periods of time than what is allowed in  /tmp, are stored here. These files are not automatically deleted after system reboot.

/usr (unix system resource file system) -Disk-based contains general files related to system.
  /usr/bin - additional user executable commands. /usr/sbin - additional system administration commands. /usr/local - system administrator repository for storing commands andtools that administrators download from the web, develop in-house or obain elsewhere. usr/share - location for man pages, documentation, etc.

/tmp (temporary file system) -Disk-based is a repository for temp files. Many programs create temp files as they run or as they are being installed.

/opt (optional file system) -Disk-based hold additional software installed on system. A sub-directory is created for each.

/home (home file system) -Disk-based hold user home directories.

/dev (device file system) -Virtual contains device files for hardware and virtual devices. Some key sub-directories are disks, pts and vg00 - hard disks, pseudo terminals and root volume group.

/proc (process file system) -Virtual maintains info about current state of the running kernel including details on CPU, memory, paritioning, running processes. It contains dynamic files and it is automatically maintained by the system.

/sys (system file system) -Virtual stores and maintains info about currenlty configure hardware. It is automatically maintained by the system.

/selinux (selinux file system) -Virtual stores all current settings for SELinux if installed.

Absolute and Relative Path

Absolute path points to a file or direcotry in relation to the root (/); always starts with (/). Relative path points to a file or dir in relation to your current location; starts with ./folder or .. or sub-directory name.

File Types

Normal files, directories
Executable files - a file that has x in the 4th, 7th or 10th field of ll output.

Symbolic link files - a link; begins with the letter l and there is an arrow pointing to the linked file or directory.

Named Pipe Files - allows 2 unrelated process running on the same system or on 2 different systems to communicate with each other and exchange data. It's unidirectiona; starts with 'p' in ll output.

Socket Files - is a named pipe that works in both directions; 2 way named pipe; used in client server programs; starts with 's' in ll ouput.

Files and Directory Operatoins

Creating files
touch (creates an empty file and update its timestamp) - $touch file1
cat (creates short text files) - $cat > newfile
vi, vim, nano

Displaying files
cat, more, less, head, tail, nano. view, vi, vim display the file in vi editor.
tail -f  == to view the content of the log file in real time

Copying files and directories
$cp file1 newfile1  == file1 is copied and name newfile1 in the same directory
$cp file1 scripts.dir1 == file1 is copied to scripts.dir1 directory
By default,when copying a file, the destination is overwritten and a warning message is not generted. To avoid this, use -i option which prompts for confirmation before overwriting.

$cp -r folder1 folder2  == using Recursive, copy folder1 along with its contents to folder2
-i option can be used together as well.

Moving and renaming files and directories
$mv -i file1 dir1 == move file1 into dir1
$mv newfile newfile1 == rename as newfile1
$mv dir1! dir2 == move dir1 into dir2 (dir2 must exist)
$mv dir1 dir2 == rename as dir2 (dir2 must exist)

Removing files and directories
$rm newfile == remove newfile
$rm dir1 == remove empty directory dir1
$rm -r dir1 == remove non-empty directory dir1



Files and Directory Control Attribute

$lsattr file1
$chattr +i file1 == make file1 cannot be changed, renamed, deleted

Pattern Matching

$grep thurein /etc/passwd
$grep thurein /etc/passwd /etc/group
$grep thurein /etc/passwd /etc/group /etc/hosts == display only file names which contain the pattern thurein
$grep -v root /etc/group == serach 'root' in /etc/group and exclude the lines that contains 'root'
$grep ^root /etc/passwd == search all line begins with 'root'
$grep bash$/etc/passwd == search all line end with 'bash'
$grep -i root /etc/passwd  == search root; ignores the letter case

Finding files

$find . -name newfile == search for newfile in my home directory
$find ~ -size -1M == find files smaller than 1MB
$find /usr -size +10M == find files lager than 10MB in /usr
$find /home -user thurein -not -gorup thurein == find files in /home with ownership set to thurein and group membership set to any group but thurein
$find /etc/rc.d -mtime +120 == find files in the /etc/rc.d directory that were modified more than 120 days ago
$find /etc/rc.d -atime -90 == find files in the /etc/rc.d directory that have not been accessed in the last 90 days
$find /etc/rc.d -mtime 10 == find files in the /etc/rc.d directory that were modified exactly 10 days ago

$locate passwd
$locate -n 3 passwd == display the first 3 file names from the results

Sorting

$sort file10
$sort -r file10
$sort -k 2 -n file10 == sort this file numberically on the second column
$ll / | sort
$ll / | sort -k 9 == sort according to filename
$ll -a /etc/skel | sort -k 6 -k 7 == sort on the 6th and then 7th comlumn

Linking files and directories

Each file in the system has a unique number assigned to it.The number is called inode(index node).

Soft link - it's like a shortcut pointing to an actual file. A soft link can cross file system boundaries and can be used to link directories.

[thurein@localhost temp]$ ln -s newfile newfilelink
[thurein@localhost temp]$ ll -i
total 4
655497 -rw-rw-r--. 1 thurein thurein 64 May 30 23:56 newfile
655498 lrwxrwxrwx. 1 thurein thurein  7 May 31 03:05 newfilelink -> newfile

Hard link - A hard link associates two or more files with a single inode number.These files have identical permissions, ownership, time stamp and file contents. A hard link cannot cross file system boundaries and cannot be used to link directories.

[thurein@localhost temp]$ ln newfile newfile10
[thurein@localhost temp]$ ll -i
total 8
655497 -rw-rw-r--. 2 thurein thurein 64 May 30 23:56 newfile
655497 -rw-rw-r--. 2 thurein thurein 64 May 30 23:56 newfile10

File Permission

3 Permission classes - User(u), Group(g) and Others(o)
4 Permission types - Read(r), Write(w), Execute(x) - executes a file or cd into the directory, Access Denied(-)
The output of ll command list the file/dir type and permission settings. The first character indicates the type of file or d for directory.  The next nine characters - three groups of three characters - show read(r),write(w),execute(x),or  none(-) permission for the three user classes: user, group, and others, respectively.

Changing Access Permission
chmod can modify the permission by using symbolic or octal notation.



File Permission

3 Permission classes - User(u), Group(g) and Others(o)
4 Permission types - Read(r), Write(w), Execute(x) - executes a file or cd into the directory, Access Denied(-)
The output of ll command list the file/dir type and permission settings. The first character indicates the type of file or d for directory.  The next nine characters - three groups of three characters - show read(r),write(w),execute(x),or  none(-) permission for the three user classes: user, group, and others, respectively.

Changing Access Permission

chmod can modify the permission by using symbolic or octal notation.

$chomd u+x file1
$chomd go+w file1
$chomd o-w file1

$chmod 544 file2
$chmod 744 file2

Default file and directory permission is handled by umask value. Subtract umask value from 666(file) and 777(dir) to have default permission.
$umask
$umask -S

Every file and directory has an owner associated with it. chown and chgrp commands are used to alter ownership and group membership.
$chown user2 file1
$chgrp user2g file1
$chown user2:user2g file1
$chown -R user2:user2g dir1 == recursive for all files and sub-directories

Special permission - setuid, setgid, sticky bit may be set up on executable files and directories.
The sticky bit is typically set on public writable directories (or other directories with rw permissions for everyone) to protect files and sub-directories owned by regular users from being deleted or moved by other regular users.

[thurein@localhost ~]$ ll -d /tmp
drwxrwxrwt. 14 root root 4096 May 31 19:40 /tmp

To set the sticky bit on /var, do either
$chmod 1755 /var
$chmod o+1 /var
$chmod 755 /var == unset sticky bit
$chmod o-1 /var == unset sticky bit
$find / -type d -perm -1000 == find all directories with sticky bit set

Manipulating Input Output

Redirecting Standard input and output
$mailx user1 < /etc/group
$sort 11.out > sort.out
$sort 11.out >> sort.out  == to append the output to sort.out

Command line history is stored in user's home directory/.bash_history. Also can use
$history

~ can be used with echo, ls, cd that refers to a location in the directory structure.
~  == $HOME directory of the user
~+ == current directory
~- == previous directory

(|) pipe sends output of one command as input to another command.
$ll /etc | more
$ll /etc | grep root | grep -i apr | nl

The tee filter can be used to send an output to more than one destination.
$ll /etc | nl | tee /tmp/ll.out
$date | tee -a /tmp/ll.out  == append the output to a file



No comments:

Post a Comment