Skip to content

Instantly share code, notes, and snippets.

@itbakery
Created May 15, 2015 16:49
Show Gist options
  • Save itbakery/d5069cbfd2b9a054f1e0 to your computer and use it in GitHub Desktop.
Save itbakery/d5069cbfd2b9a054f1e0 to your computer and use it in GitHub Desktop.
lpi1-topic102.md

###Topic 102: การติดตั้ง Linux และ การบริหารจัดการ package 102.1 การออกแบบ Layout ของ hard disk เพื่อการใช้งาน (Design hard disk layout) filesystem ใน linux เริ่มต้นจาก / (root) และแบ่งตามวัตถุประสงค์ มีลักษณะที่เป็น hierarchy

root_file_system

/ (root) เป็นfile systemที่อยู่บนสุดของโครงสร้าง /var ทำหน้าเก็บ variable filesystem /home ทำหน้าเก็บข้อมูลของ User ที่อยู่ใน linux
/boot เก็บ kernel และ init rd จะแบ่ง partition แยกออกมา swap มีขนาดประมาณ 1-2 เท่าของ ram เป็นพื้นที่สำรองไว้ ในกรณีที่ memory ของระบบ หรือ ram ถูกใช้หมด ระบบจะมาใช้หน่วยความจำสำรองบน hard disk แต่จะมีความเร็วน้อยกว่า ใน Partition นี้ filesystemจะ format ให้เป็นชนิด swap

linux มีความยืดหยุ่นในการออกแบบ layout ของ partition การ Mount file system สามารถ mount ไปยัง hard disk ของเครื่อง computer ได้โดยตรง หากมีมากกว่า 1 harddisk สามารถเลือก หรือจะ mount ไปยัง NFS (network file system), Smb (Samba file system) ก็ได้

###102.2 ติดตั้ง Boot manager (Install a boot manager) เรียนรู้การใช้งาน LILO และ GRUB ซึ่งทำหน้าที่เป็น boot loader สำหรับ /boot/grub2/grub.cfg

# cat /etc/default/grub 
GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap crashkernel=auto rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

# cat /boot/grub2/grub.cfg

#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
...

สามารถ reinstall grub ด้วยคำสั่ง grub2-install

# grub2-install /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.

วิธีการ Backup MBR

# dd if=/dev/sda  of=/root/backup.mbr  bs=512 count=1

ความหมายbs = block size, count จำนวน block size ที่ต้องการเขียน

# dd if=/dev/vda  of=/root/backup.mbr  bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.0175651 s, 29.1 kB/s

วิธีการ restore

# dd if=/root/backup.mbr  of=/dev/sda bs=512 count=1

###102.3 บริหารจัดการ Libraries ที่เป็นลักษณะ shared Libraries (Manage shared Library) การเรียนรู้เกี่ยวกับ shared library เป็นเรื่องสำคัญ เนื่องจากเป็นพื้นฐานของการใช้งาน linux ในการบริหารจัดการพื้นที่ โปรแกรมสามารถเรียกใช้งาน library ดังกล่าวได้ทันทีเมื่อมีการโหลด library ลงใน memory

การเรียกดู library ที่ใช้งานของแต่ละโปรแกรมได้ โดยการใช้คำสั่ง ldd ในตัวอย่างนี้ต้องการทราบว่า โปรแกรม ls มีการเรียกใช้ binary อะไรบ้าง

# ldd  /bin/ls

จะเห็นว่า ชี้ไปที่ /lib64/ ซึ่งจะชี้ไปที่ cache /etc/ld.so.cache

# ldd /bin/ls
	linux-vdso.so.1 =>  (0x00007fff2d8d2000)
	libselinux.so.1 => /lib64/libselinux.so.1 (0x00007faa17456000)
	libcap.so.2 => /lib64/libcap.so.2 (0x00007faa17251000)
	libacl.so.1 => /lib64/libacl.so.1 (0x00007faa17047000)
	libc.so.6 => /lib64/libc.so.6 (0x00007faa16c86000)
	libpcre.so.1 => /lib64/libpcre.so.1 (0x00007faa16a25000)
	liblzma.so.5 => /lib64/liblzma.so.5 (0x00007faa167ff000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007faa165fb000)
	/lib64/ld-linux-x86-64.so.2 (0x00007faa1768e000)
	libattr.so.1 => /lib64/libattr.so.1 (0x00007faa163f6000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007faa161d9000)

ตรวจสอบ cache

# cd /etc
# ls | grep  ld
autofs_ldap_auth.conf
firewalld
ld.so.cache
ld.so.conf
ld.so.conf.d
openldap
sudo-ldap.conf

# cat ld.so.conf
include ld.so.conf.d/*.conf

# ls -l ld.so.conf.d/
total 28
-rw-r--r--. 1 root root  19 Mar  6 06:09 dyninst-x86_64.conf
-r--r--r--. 1 root root  63 Mar 27 10:15 kernel-3.10.0-229.1.2.el7.x86_64.conf
-r--r--r--. 1 root root  63 Mar  6 18:47 kernel-3.10.0-229.el7.x86_64.conf
-rw-r--r--. 1 root root  17 Jun 10  2014 libiscsi-x86_64.conf
-rw-r--r--. 1 root root  17 Feb  5 21:46 mariadb-x86_64.conf
-rw-r--r--. 1 root root  24 Mar  6 13:01 tracker-x86_64.conf
-rw-r--r--. 1 root root 276 Apr 25 19:04 vmware-tools-libraries.conf

# cat /etc/ld.so.conf.d/mariadb-x86_64.conf 
/usr/lib64/mysql

# cat /etc/ld.so.conf.d/kernel-3.10.0-229.1.2.el7.x86_64.conf 
# Placeholder file, no vDSO hwcap entries used in this kernel.

Library files ที่อยู่ใน /etc/ld.so.conf.d/* จะถูกโหลด เพื่อระบบเริ่มการทำงาน ค่าเป็นค่า default หากต้องการ reload shared library อยู่ที่อื่นนั้นสามารถทำได้โดยการใช้คำสั่ง ldconfig และสามารถเพิ่มค่าได้โดยเพิ่มไปที่ LD_LIBRARY_PATH เป็นการเพิ่มแบบชั่วคราว

# ldconfig
# export  LD_LIBRARY_PATH=<fold ของ Libs>

###102.4 การบริหาร package ของ Distribution ในกลุ่ม Debian การบริหารจัดการ package ของ Debian สามารถทำได้ด้วยคำสั่ง dpkg, dpkg-reconfigure, apt-get, apt-cache, aptitude แต่ละคำสั่ง มีความแตกต่างและมีเป้าหมายในการใช้งานที่แตกต่างกัน

คำสั่ง options ความหมาย
# dpkg -l 'string'(--list) แสดงผล package ที่ได้จากการค้นหาจาก stringเช่น dpkg -l 'vi'
-L package --listfiles แสดง file ทั้งหมดที่ติดตั้งออกมาจาก package นั้นๆ เช่น dpkg -L vim
-p (--print-avail) แสดงผลของ package แต่ละpackage เช่น dpkg -p vim
-i <package.deb> (--install) ใช้สำหรับติดตั้ง package debian ที่มีนามสกุล deb แต่จะไม่ทำการติดตั้ง dependencies ให้ ซึ่งอาจทำให้มีปัญหา
-r --removd ไม่ต้องมี deb ใช้ชื่อของ package เท่านั้น ไม่ลบ CONFIG
-s package(--search) ค้นหา package
-P --purge ถอนโปรแกรมออกจากเครื่อง เหมือน -r แต่จะลบ ส่วนของ CONFIG FILE ด้วย
-S จะได้ว่า package ใดที่มีคำสั่งนี้อยู่ ภายใน หรือ บอกว่า command นี้มาจาก package ใด

ทดสอบคำสั่งเบื้องต้น

# dpkg -l vim 
Desired=Unknown/Install/Remove/Purge/Hold 
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend 
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) 
||/ Name           Version        Description 
+++-==============-==============-============================================ 
ii  vim            2:7.3.429-2ubu Vi IMproved - enhanced vi editor 

# dpkg  -S /bin/ls
coreutils: /bin/ls        <-- คำสั่ง ls อยู่ package ชื่อว่า coreutils
# dpkg  -p  vim           <-- [-p] print information

# dpkg -i  package.deb    <-- [-i] install ด้วย package.deb แต่จะไม่ทำการโหลด dependencies  ให้
# dpkg -r  package        <-- [-r]  remove package ไม่ต้องตามด้วยนามสกุลdeb
# dpkg -P  package        <-- [-P] [--purge] ลบโปรแกรมพร้อมกับ config

dpkg-reconfigureเป็นเครื่องมือที่ช่วยคืนค่า config แต่ต้องรู้ว่าต้องการ reconfigure Package ชื่ออะไร ลองทดลองคำสั่ง #dpkg-reconfigure postfix

postfix ชุดคำสั่ง apt-get command โดยจะสามารถแบ่งแยกได้เป็น

คำสั่ง ความหมาย
# apt-get upgrade Upgrade packgrade
# apt-get install ติดตั้ง package
# apt-get remove ถอน package

ทดลองคำสั่ง

# apt-get upgrade 
Reading package lists... Done 
Building dependency tree       
Reading state information... Done 
The following packages have been kept back: 
  linux-headers-generic-lts-trusty linux-image-generic-lts-trusty 
The following packages will be upgraded: 
  apparmor apport apt apt-transport-https apt-utils bash curl dbus file gnupg 
  gpgv krb5-locales libapt-inst1.4 libapt-pkg4.12 libc-bin libc6 libcurl3 
  libcurl3-gnutls libdbus-1-3 libgcrypt11 libgssapi-krb5-2 libjs-jquery 
  libk5crypto3 libkrb5-3 libkrb5support0 libmagic1 libssl1.0.0 libudev0 
  linux-firmware linux-generic-lts-trusty man-db multiarch-support openssl 
  python-apport python-problem-report rsyslog udev wpasupplicant 
38 upgraded, 0 newly installed, 0 to remove and 2 not upgraded. 
Need to get 44.0 MB of archives. 
After this operation, 72.7 kB of additional disk space will be used. 
Do you want to continue [Y/n]? 
# apt-get install lynx 
Reading package lists... Done 
Building dependency tree       
Reading state information... Done 
The following extra packages will be installed: 
  lynx-cur 
The following NEW packages will be installed: 
  lynx lynx-cur                 
0 upgraded, 2 newly installed, 0 to remove and 2 not upgraded. 
Need to get 1,065 kB of archives. 
After this operation, 2,512 kB of additional disk space will be used. 
Do you want to continue [Y/n]? 
# apt-get remove lynx 
Reading package lists... Done 
Building dependency tree       
Reading state information... Done 
The following packages will be REMOVED: 
  lynx        <-- ไม่ remove dependency ให้  -->
0 upgraded, 0 newly installed, 1 to remove and 2 not upgraded. 
After this operation, 42.0 kB disk space will be freed. 
Do you want to continue [Y/n]? 

Update & Upgrade

# apt-get update               <-- update local database ก่อนแล้วจึง-->
# apt-get upgrade              <-- upgrade system -->

ปัญหา

W: Failed to fetch http://us.archive.ubuntu.com/ubuntu/dists/precise/multiverse/binary-i386/Packages  Hash Sum mismatch 

คำสั่ง apt-cache ค้นหาข้อมูลlocal file ในเครื่อง

คำสั่ง ความหมาย
# apt-cache search ค้นหา package
# apt-cache show แสดงข้อมูลใน package
# apt-cache depends แสดงว่า package แต่ละ package มี dependencies อะไรบ้าง
# apt-cache rdepends แสดงผลกลับกันกับคำสั่ง depends คือบอกว่า package ดังกล่าวเป็น dependencies ของ package ชื่ออะไร

คำสั่ง aptitude เป็นคำสั่งที่ใช้ในการจัดการ package เหมือนกับ dpkg และ apt-cache แต่มีความสามารถในการใช้งานได้มีประสิทธิภาพมากกว่า ให้ทำการทดสอบคำสั่ง aptitude แต่ไม่จำเป็นต้อง มี flag option ใดๆ

# aptitude

aptitude

การระบุที่อยู่ของ repository อยู่ที่ /etc/apt/sources.list source.list

# lsb_release -a 
No LSB modules are available. 
Distributor ID:	Ubuntu 
Description:	Ubuntu 12.04.5 LTS 
Release:	12.04 
Codename:	precise 

เปลี่ยน http://th.archive.ubuntu.com/ubuntu/ -> http://th.archive.ubuntu.com/ubuntu/ แล้วสั่ง #apt-get update เพื่อ update apt-get update

###102.5 การบริหาร package ของ Distribution ในกลุ่ม Redhat (RPM, YUM package management) บริหารจัดการ rpm package โดยใช้คำสั่ง yum โดยสามารถ install, reinstall, remove, search และสามารถดูข้อมูลที่เกี่ยวข้องกับ package ได้แก่ version, status, dependencies, integrity, signatures

การใช้งานคำสั่ง yum ใช้สำหรับการติดตั้งโปรแกรม

#yum install openssh-clients

openssh-client คำสั่ง yum จะทำการติดตั้ง programs และจะติดตั้ง dependencies ให้ จาก repository repository

มีการร้องขอการยืนยัน Signature key เนื่องจากมีการระบุค่า gpgcheck=1 ใน file /etc/yum.repos.d/ CentOS-Base.repo

centrepo

การใช้งานคำสั่ง rpm2cpio ใช้เมื่อต้องการ extract file จาก package โดยคำสั่งจะทำการ convert rpm package ไปยัง cpio archive

# wget http://mirror.centos.org/centos-7/7.1.1503/os/x86_64/Packages/rsyslog-7.4.7-7.el7_0.x86_64.rpm
# rpm2cpio rsyslog-7.4.7-7.el7_0.x86_64.rpm > rsyslog.cpio      
# file rsyslog.cpio 
rsyslog.cpio: ASCII cpio archive (SVR4 with no CRC)
# less rsyslog.cpio                     ****            // read file
# cpio -ivt  <  rsyslog.cpio                        // list file  not extract

# mkdir test                                        // create test folder
# mv  rsyslog-7.4.7-7.el7_0.x86_64.rpm  test         // move file
# cpio -ivd  <  rsyslog.cpio                        // extract to local directory
# ls 
etc  lib64  rsyslog.cpio  sbin  usr  var 	       // result

การเรียนรู้ การบริหาร config /etc/yum.conf และ folder /etc/yum.repos.d/

#cat  /etc/yum.conf
[main] 
cachedir=/var/cache/yum/$basearch/$releasever 
keepcache=0 
debuglevel=2 
logfile=/var/log/yum.log 
exactarch=1 
obsoletes=1 
gpgcheck=1 
plugins=1 
installonly_limit=5 
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum 
distroverpkg=centos-release 
# in /etc/yum.repos.d 

**เพิ่ม epel repo ** สามารถเพิ่มเติม repo ให้แก่ centos เช่น ต้องการเพิ่ม epel (Extra packages for Enterprise linux) ซึ่งเป็น repo ที่เพิ่ม package (https://fedoraproject.org/wiki/EPEL)

# wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm

จะได้ repo เพิ่ม มา 2 repo ใน /etc/yum.repos.d/ คือ epel.repo และ epel-testing.repo

# cd  /etc/yum.repos.d/
# ls  
CentOS-Base.repo       CentOS-Media.repo  epel.repo            
CentOS-Debuginfo.repo  CentOS-Vault.repo  epel-testing.repo   

การใช้งาน yumdownloader ใช้สำหรับ download package rpm โปรแกรม yumdownloader จะอยู่ในpackage yum-utils

# yum provides */yumdownloader
yum-utils-1.1.31-29.el7.noarch : Utilities based around the yum package manager
Repo        : base
Matched from:
Filename    : /usr/bin/yumdownloader



yum-utils-1.1.31-29.el7.noarch : Utilities based around the yum package manager
Repo        : @anaconda
Matched from:
Filename    : /usr/bin/yumdownloader

# yum install yum-utils
# yumdownloader  openssh-clients
# ls 
...   ...  ...  openssh-clients-5.3p1-94.el6.x86_64.rpm

แนวทางการบริหารจัดการ package RPM การบริหารจัดการ package rpm ด้วย yum นั้นไม่ใช่เรื่องซับซ้อน yum จะทำการติดตั้ง package รวมไปถึง dependencies ให้เอง พร้อมทั้งทำการ Check signatures ที่ถูกsign ด้วย gpg ที่ถูกต้องให้ด้วย เพื่อเป็นการรับรองว่าเป็น package ที่ได้รับการรับรองอย่างถูกต้อง ต่อมา yumจะทำการ verify ความถูกต้องของการติดตั้ง และสามารถใช้คำสั่ง yum ทำการค้นหา (queries) package ในฐานข้อมูลได้ หากใช้ rpm command จำเป็นต้องแก้ปัญหาของ dependencies ด้วยตนเอง

# rpm -i openssh-clients-5.3p1-94.el6.x86_64.rpm 
error: Failed dependencies:          <--rpm ไม่สามารถจัดการ dependencies ได้
	libedit.so.0()(64bit) is needed by openssh-clients-5.3p1-94.el6.x86_64 

# yumdownloader libedit
# rpm -i libedit-2.11-4.20080712cvs.1.el6.x86_64.rpm
# rpm -i openssh-clients-5.3p1-94.el6.x86_64.rpm
# rpm -e  openssh 
error: Failed dependencies: 
	openssh = 5.3p1-94.el6 is needed by (installed) openssh-server-5.3p1-94.el6.x86_64 
	openssh = 5.3p1-94.el6 is needed by (installed) openssh-clients-5.3p1-94.el6.x86_64 
# rpm -Vv openssh-clients                  <-- verify -->
.........  c /etc/ssh/ssh_config 
.........    /usr/bin/.ssh.hmac 
.........    /usr/bin/scp 
.........    /usr/bin/sftp 
.........    /usr/bin/slogin 
.........    /usr/bin/ssh 
.........    /usr/bin/ssh-add 
.........    /usr/bin/ssh-agent 
.........    /usr/bin/ssh-copy-id 
.........    /usr/bin/ssh-keyscan 
.........    /usr/libexec/openssh/ssh-pkcs11-helper 
.........  d /usr/share/man/man1/scp.1.gz 
.........  d /usr/share/man/man1/sftp.1.gz 
.........  d /usr/share/man/man1/slogin.1.gz 
.........  d /usr/share/man/man1/ssh-add.1.gz 
.........  d /usr/share/man/man1/ssh-agent.1.gz 
.........  d /usr/share/man/man1/ssh-copy-id.1.gz 
.........  d /usr/share/man/man1/ssh-keyscan.1.gz 
.........  d /usr/share/man/man1/ssh.1.gz 
.........  d /usr/share/man/man5/ssh_config.5.gz

# vi /etc/ssh/ssh_config              <---  try to add something -->
# rpm -Vv openssh-clients 
S.5....T.  c /etc/ssh/ssh_config      <---[S size change,  5 md5 change,  T Time change from origin ]-->
.........    /usr/bin/.ssh.hmac 
.........    /usr/bin/scp 
...

ทดลองค้นหาโดยใช้คำสั่ง rpm ตามด้วย options [-q] และ [-qi]

# rpm -q  openssh
openssh-5.3p1-94.el6.x86_64

# rpm -qi  openssh
Name        : openssh                      Relocations: (not relocatable) 
Version     : 5.3p1                             Vendor: CentOS 
Release     : 94.el6                        Build Date: Sat 23 Nov 2013 05:40:05 AM ICT 
Install Date: Mon 20 Oct 2014 09:25:14 PM ICT      Build Host: c6b8.bsys.dev.centos.org 
Group       : Applications/Internet         Source RPM: openssh-5.3p1-94.el6.src.rpm 
Size        : 738990                           License: BSD 
Signature   : RSA/SHA1, Mon 25 Nov 2013 02:32:56 AM ICT, Key ID 0946fca2c105b9de 
Packager    : CentOS BuildSystem <http://bugs.centos.org> 
URL         : http://www.openssh.com/portable.html 
Summary     : An open source implementation of SSH protocol versions 1 and 2 

# rpm -qa |  grep openssh

ขั้นตอนการ build package ด้วย rpmbuild -b ติดตั้ง rpmbuild

# yum install rpm-build  make gcc
# yum groupinstall "Development Tools"
# mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
Options Stage Execute ทำงาน Usage
# rpmbuild -ba <file.spec> %prep, %build, %install, %check Build ทั้ง binary และ source RPM
# rpmbuild -bb <file.spec> %prep, %build, %install, %check Build binary อย่างเดียว
# rpmbuild -bc <file.spcec> %prep, %build ทำการ compile โปรแกรม แต่ ไม่สร้าง full RPM จะทำจนสิ้นสุด %build section
# rpmbuild -bp <file.spec> %prep ทำการ compile โปรแกรม แต่ไม่สร้าง full RPM จะทำจนสิ้นสุด %prep section
# rpmbuild -bi <file.spec> สร้าง binary file RPM และหยุดหลังจาก %install section
# rpmbuild -bl <file.spec> ตรวจสอง file ของ rpm และ แสดง errors หาก file ที่ต้องการใน buildroot มีไม่ครบตามที่ต้องการ
# rpmbuild -bs uild source RPM อย่างเดียว

**ทดสอบการ build **

# cd ~
# wget http://vault.centos.org/6.6/updates/Source/SPackages/iproute-2.6.32-33.el6_6.src.rpm
# rpm -i iproute-2.6.32-33.el6_6.src.rpm
# cd  ~/rpmbuild/SPECS
# ls
...  iproute.spec
# cd ~/rpmbuild/SOURCES
....
iproute2-2.6.31-arpd_usage.patch 
iproute2-2.6.31-fix-changing-of-ip6ip6-tunnel-parameters.patch 
iproute2-2.6.31-lnstat-dump.patch 
iproute2-2.6.31-lnstat-typo.patch 
iproute2-2.6.31-ss_miss_parameter.patch 
iproute2-2.6.31.tar.bz2             <------ source
iproute2-2.6.31-testsuite.patch 
iproute2-2.6.32-add-nl-attr-to-filter-dump-req.patch 
iproute2-2.6.32-add-qfq-scheduler.patch 
iproute2-2.6.32-bridge-fdb-additional-man-changes.patch 
iproute2-2.6.32-bridge-man.patch
.... 

จะได้ iproute.specอยู่ใน folder ~/rpmbuild/SPECS และมี source files (iproute2..tar.bz2) และ patch file

# rpmbuild -bp iproute.spec 
error: Failed build dependencies:
	tex(latex) is needed by iproute-2.6.32-33.el7.centos.x86_64
	tex(dvips) is needed by iproute-2.6.32-33.el7.centos.x86_64
	linuxdoc-tools is needed by iproute-2.6.32-33.el7.centos.x86_64
	psutils is needed by iproute-2.6.32-33.el7.centos.x86_64
	db4-devel is needed by iproute-2.6.32-33.el7.centos.x86_64
	iptables-devel >= 1.4.5 is needed by iproute-2.6.32-33.el7.centos.x86_64

# yum install tex linuxdoc-tools psutils db4-devel  iptables-devel 
# rpmbuild -bp iproute.spec  
Copy link

ghost commented Dec 21, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment