好用小套件-perl-Net-IP

功能:主機數量與遮罩的關係
安裝套件 perl-Net-IP
# yum install -y perl-Net-IP
# iptab


好用小套件-nc

功能:網路工具

簡單的聊天功能
Host_A(192.168.18.128)
# iptables -A INPUT -p tcp -m tcp --dport 65534 -j ACCEPT
# nc -l 65534  // 開啟本地通訊埠 65534,參數 -l:listening
 i
 love
 you

Host_B(192.168.18.131)
# nc 192.168.18.128 65534
 i
 love
 you

通訊埠監聽
# nc -v 192.168.18.128 -z 80
 Connection to 192.168.18.128 80 port [tcp/http] succeeded!
# nc -v 192.168.18.128 -z 110
 Connection to 192.168.18.128 110 port [tcp/pop3] succeeded!

安裝及移除套件-rpm

20121109 新增補充資訊
20121120 新增指令說明

RPM(Redhat Package Manager)是 Linux 必學的套件管理程式,yum 的運行也是透過 rpm,差別在於 yum 可以自動解決相依性的問題,不必使用者自行尋找安裝套件來源。

RPM相依性的問題非常麻煩,安裝完下面所有的套件後(由下而上),才能安裝套件a,而在yum出現之前,都要使用者自行解決相依性的問題。

rpm 安裝流程有下列 5 個步驟:
1. 檢查數位簽章
2. 進行相依性檢查(檢查失敗中斷安裝)
3. 執行 pre script
4. 解壓縮並複製檔案至指定路徑
5. 執行 post script

安裝


# rpm -ivh xxx.rpm  // 本地安裝
# rpm -ivh http://aaa.bbb.com/xxx.rpm  // 網路安裝

更新


參數 U 和 F 的差別:
下載 pidgin 套件並使用參數 U 和 F 來安裝
參數 F:若 pidgin 原先並未安裝,則該軟體不會安裝,若原先有安裝,就會升級(upgrade)
參數 U:若 pidgin 原先並未安裝,則該軟體會安裝在系統上,若原先有安裝,就會升級(upgrade)
# rpm -Fvh pidgin-2.7.9-5.el6.2.x86_64.rpm  // 無後續安裝步驟
# rpm -Uvh pidgin-2.7.9-5.el6.2.x86_64.rpm  // 執行安裝步驟,但相依性檢查失敗中斷安裝
 error: Failed dependencies:
 libgstfarsight-0.10.so.0()(64bit) is needed by pidgin-2.7.9-5.el6.2.x86_64
 libpurple = 2.7.9-5.el6.2 is needed by pidgin-2.7.9-5.el6.2.x86_64
 libpurple.so.0()(64bit) is needed by pidgin-2.7.9-5.el6.2.x86_64

移除


# rpm -e firefox

GPG(GNU Private Guard)key 數位簽章驗證


安裝(匯入)原廠公開金鑰: # rpm --import ftp://ftp.nsysu.edu.tw/Unix-like/CentOS/6.3/os/x86_64/RPM-GPG-KEY-CentOS-6
使用原廠公開金鑰驗證該網站下載套件的數位簽章 # rpm -K firefox-10.0.10-1.el6.centos.x86_64.rpm
 firefox-10.0.10-1.el6.centos.x86_64.rpm: rsa sha1 (md5) pgp md5 OK
# rpm -K google-chrome-stable_current_x86_64.rpm
 google-chrome-stable_current_x86_64.rpm:(SHA1) DSA sha1 md5 (GPG) NOT OK (MISSING KEYS:
 GPG#7fac5991)  // 用 CentOS 的 key 來驗證 google 的套件


查詢


查詢檔案屬於哪一個已安裝的套件: # rpm -qf /etc/ntp.conf
 ntp-4.2.4p8-2.el6.centos.x86_64

查詢套件資訊: # rpm -qi httpd
查詢所有已安裝的套件: # rpm -qa
# rpm -q httpd

查詢已安裝的套件的所有檔案及路徑: # rpm -ql ntp
 /etc/dhcp/dhclient.d
 /etc/dhcp/dhclient.d/ntp.sh
 /etc/ntp.conf
 ........

查詢套件安裝狀態: # rpm -qs httpd | tail -n4
 normal /var/www/icons/world1.gif  // normal 表示安裝正常
 normal /var/www/icons/world1.png
 normal /var/www/icons/world2.gif
 normal /var/www/icons/world2.png
 ........

查詢套件相依性: # rpm -qR httpd
 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
 rpmlib(VersionedDependencies) <= 3.0.3-1
 rtld(GNU_HASH)
 system-logos >= 7.92.1-1
 rpmlib(PayloadIsXz) <= 5.2-1
 ........

查詢套件安裝或移除腳本(script): # rpm -q --scripts httpd
查詢未安裝套件相關指令: # rpm -qp pidgin
# rpm -qpl pidgin
# rpm -qpR pidgin
# rpm -qpi pidgin
# rpm -q --scripts pidgin
補充:不能與參數 s 同時使用,因為還沒有安裝,不會有安裝時的狀態。

重新安裝套件: # rpm -ivh --replacepkgs vsftpd
補充:此參數也可以解釋為 reinstall,但並不會覆蓋原先的檔案,只是將缺少的檔案再補回去。

驗證


檢查檔案內容是否被更動過: # rpm -Vf /etc/httpd/conf/httpd.conf
 S.5....T. c /etc/httpd/conf/httpd.conf  // 若檔案未遭到改變,就不會出現此行訊息

檢查套件檔案狀態: # rm -rf /etc/vsftpd/vsftpd.conf
# rpm -V vsftpd
 missing c /etc/vsftpd/vsftpd.conf

補充:
S:file Size differs(檔案的容量大小已被改變)
M:Mode differs (includes permissions and file type)(檔案的類型或檔案的屬性 (rwx) 已被改變)
5:MD5 sum differs(加密總合已被改變)
D:Device major/minor number mismatch(裝置的主 / 次代碼已經改變)
L:readLink(2) path mismatch(Link 路徑已被改變)
U:User ownership differs(檔案的所屬人已被改變)
G:Group ownership differs(檔案的所屬群組已被改變)
T:mTime differs(檔案的建立時間已被改變)
P:caPabilities differ

c:%config configuration file.
d:%doc documentation file.
g:%ghost file (i.e. the file contents are not included in the package payload).
l:%license license file.
r:%readme readme file.
資料來源

iSCSI 伺服器(IP-SAN)

iSCSI 範例:準備 2 台電腦
Target(192.168.18.128):建置及規劃 iSCSI 儲存設備
Initiator(192.168.18.131):主要功能是提供電腦主機連線到 Target 作磁碟存取

(注意:使用 VMware Player,在安裝完 1 個虛機後,雖然可以複製更名成另一個新的虛機,但在 iSCSI 的練習中,iSCSI Initiator 會在開機時(/etc/fstab)掛載失敗(fsck fail),除非你有時間去打怪,不然請避免使用專案複製方式練習

iSCSI Target(192.168.18.128)

# yum search scsi | grep --color target
 scsi-target-utils.x86_64 : The SCSI target daemon and utility programs
# yum install -y scsi-target-utils  // 安裝 iSCSI Target 套件
# cp /etc/tgt/targets.conf /etc/tgt/targets.conf.backup  // 備份原始設定檔
# fdisk -l
 Disk /dev/sdb: 1073 MB, 1073741824 bytes  // 使用 VMware Player 配置了一個 1GB
 255 heads, 63 sectors/track, 130 cylinders
 Units = cylinders of 16065 * 512 = 8225280 bytes
 Sector size (logical/physical): 512 bytes / 512 bytes
 I/O size (minimum/optimal): 512 bytes / 512 bytes
 Disk identifier: 0x00000000
# fdisk /dev/sdb              // 配置分享磁區,不用格式化
# fdisk -l


# vim /etc/tgt/targets.conf  // 設定分享磁區
 <target iqn.2012-10.com.example:server.target1="iqn.2008-09.com.example:server.target1">
backing-store /dev/sdb1
 </target>

啟動 iSCSI  Target 服務:服務名稱為 tgtd
# service tgtd start
 Starting SCSI target daemon: [ OK ]

開啟防火牆:通訊埠為 3260
# iptables -A INPUT -p tcp -m tcp --dport 3260 -j ACCEPT

iSCSI Initiator(192.168.18.131)

# yum search scsi | grep --color initiator
 iscsi-initiator-utils.i686 : iSCSI daemon and utility programs
# yum install -y iscsi-initiator-utils.i686  // 安裝 iSCSI Initiator 套件
# iscsiadm -m discovery -t st -p 192.168.18.128  // 搜尋定分享磁區
 Starting iscsid: [ OK ]             // 啟動 iSCSI Initiator 服務
 192.168.18.128:3260,1 iqn.2008-09.com.example:server.target1

補充:若要再偷賴點,可在搜尋後就立即登入
# iscsiadm -m discovery -t st -p 192.168.18.128 -l
登入 iSCSI Target
# iscsiadm -m node -l  // -l:login;-u:logout
 Logging in to [iface: default, target: iqn.2008-09.com.example:server.target1, portal: 192.168.18.128,3260] (multiple)
 Login to [iface: default, target: iqn.2008-09.com.example:server.target1, portal: 192.168.18.128,3260] successful.
補充:以下 2 種方式皆可登入。
  • # iscsiadm -m node -l
  • # iscsiadm -m node -T iqn.2008-09.com.example:server.target1 -l

  • 查看 iSCSI 載入狀態 # dmesg | tail


    查看磁區狀態
    # fdisk -l

    硬碟配置、格式化及掛載
    # fdisk /dev/sdb
    # fdisk /dev/sdc
    # mkfs.ext4 /dev/sdb1 ; mkfs.ext4 /dev/sdc1
    # mkdir /mnt/iscsi1
    # mkdir /mnt/iscsi2
    # mount /dev/sdb1 /mnt/iscsi1
    # mount /dev/sdc1 /mnt/iscsi2

    開機時掛載
    # vim /etc/fstab
     /dev/sdb1 /mnt/iscsi1 ext4 _netdev 1 2  // 裝置名稱亦可使用 UUID,使用指令# blkid /dev/sdb1 查看
     /dev/sdc1 /mnt/iscsi2 ext4 _netdev 1 2
    # mount -a
    # df
     /dev/sdb1 2060144 35840 1919656 2% /mnt/iscsi1
     /dev/sdc1 1027004 17668 957168 2% /mnt/iscsi2

    若下次開機時不再掛載使用,除了移除/etc/fstab的設定外,也要登出並停止相關服務,否則下次開機會停頓很久,因為服務程式在搜尋網域可用的target,但仍然可以開機。 # iscsiadm -m node -u
    # chkconfig iscsi off
    # chkconfig iscsid off

    問題探討


    當有二個 initiator 同時存取同一個 iSCSI 裝置時,資料會毀損。
    因為這二個 initiator 並不會互相通知更新資料,故除了資料毀損外,各自上傳的檔案,對方也看不到。
    叢集檔案系統(Cluster File System、CFS)應用,像是 RedHat GFS 或是 Oracle OCFS2 檔案系統,便是解決此問題的方法。

    好用小套件-gpm

    可以在終端機介面(terminal或console 模式)時使用滑鼠
    # yum install -y gpm


    在X-Windows模式時,使用以下按鍵組合,切換成terminal或console 模式 [Ctrl] + [Alt] + [F1]~[F6]

    防火牆-TCP Wrapper

    安裝 TCP Wrapper
    # yum install -y tcp_wrappers

    查看網路服務是否可套用 TcpWrappers 的設定
    # ldd $(which http vsftpd sshd) | grep -E 'libwrap|bin'
     /usr/sbin/httpd:
     /usr/sbin/vsftpd:
     libwrap.so.0 => /lib64/libwrap.so.0 (0x00007ff8725a9000)
     /usr/sbin/sshd:
     libwrap.so.0 => /lib64/libwrap.so.0 (0x00007fcfbca96000)
    由以上結果可知 httpd 服務並不支援 TcpWrappers 功能,故在 /etc/hosts.allow 或 /etc/hosts.deny 設定規則,並不會產生任何作用。

    範例:限制使用者透過 SSH 連線
    使用 VM 準備 3 台電腦:
    server(192.168.18.128):設定 /etc/hosts.allow 和 /etc/hosts.deny 條件,限制 host_A 連線
    host_A(192.168.18.131):遠端連線至 server
    host_B(192.168.18.132):遠端連線至 server

    server(192.168.18.128):限制 host_A 連線
    # vim /etc/hosts.deny
     ALL: ALL

    # vim /etc/hosts.allows
     sshd: ALL EXCEPT 192.168.18.132

    host_A(192.168.18.131)遠端連線至 server
    # ssh 192.168.18.128
     The authenticity of host '192.168.18.128 (192.168.18.128)' can't be established.
     RSA key fingerprint is a1:2b:03:12:a5:f4:a9:1d:eb:52:b2:d4:2f:f3:66:d6.
     Are you sure you want to continue connecting (yes/no)? yes
     Warning: Permanently added '192.168.18.128' (RSA) to the list of known hosts.
     root@192.168.18.128's password:
     Last login: Sun Sep 23 22:50:12 2012  // 登入成功 
    # hostname
     host128.ntucc.edu.tw

    host_B(192.168.18.132)遠端連線至 server
    # ssh 192.168.18.128
     ssh_exchange_identification: Connection closed by remote host // 登入失敗

    Linux 小技巧-在CLI介面中顯示行數

    20121210 新增 iptables 顯示行數用法:iptables 專有的參數 --line-numbers

    範例 1:需要刪除 iptables 的規則時,若規則太多,容易誤刪規則,加上行數後,即可正確指出要刪除的規則。
    # iptables-save | grep '\-A' | nl -bt

    範例 2:iptables 專有的參數 --line-numbers # iptables -L INPUT --line-numbers

    想爬牆的要看這篇-SSH Tunnel

    若在公司使用 SSH Tunnel 必須非常小心,因為既然可以翻出去,別人也可以翻進來(Reverse SSH Tunnel),若沒有好好處理,可是安全的一大威脅!

    範例:SSH_Client_A 無法直接存取 Web_Server,故透過 SSH_Server_B 存取 Web_Server。
    使用 VM 準備 3 台電腦,每台電腦的角色如下:
    SSH_Client_A(192.168.18.131):存取網頁伺服器(Web_Server)主頁
    SSH_Server_B(192.168.18.129):與 SSH_Client_A 建立 SSH Tunnel
    Web_Server(192.168.18.128):網頁伺服器


    Web_Server(192.168.18.128):
    # vim /var/www/html/index.html  // 記得防火牆要開
     <h1> SSH Tunnel </h1>

    SSH_Client_A(192.168.18.131):輸入建立 SSH Tunnel 指令
    # ssh -L 65534:192.168.18.128:80 lee@192.168.18.129

    在 SSH_Client_A 測試:
    # elinks http://localhost:65534

    參數說明:
    -L:本地主機(SSH_Client_A)開啟通訊埠,本範例為 65534
    網頁伺服器:192.168.18.128:80
    SSH_Server_B:192.168.18.129
    elinks 是文字介面的瀏覽器(yum instll -y elinks)

    SSH_Server_B 連線狀態
    # netstat -anp | grep --color sshd
     tcp 0 0 192.168.18.129:22 192.168.18.131:60861 ESTABLISHED 3014/sshd

    Web_Server 連線狀態
    請注意來源 IP,雖然是 SSH_Client_A 存取 Web Server,但來源 IP 卻是 SSH_Server_B 的 IP。 # tail -f /var/log/httpd/access_log
     192.168.18.129 - - [25/Sep/2012:05:06:40 -0700] "GET / HTTP/1.1" 200 22 "-" "ELinks/0.12pre5 (textmode; Linux; 80x24-2)"

    網路介面設定-靜態路由設定

    方法一:
    # route add -net network/netmask gw router_ip // network 是指網段,netmask 可以用 prefix

    方法二:
    # ip route add network/netmask via router_ip

    寫入網路組態檔
    # vim /etc/sysconfig/network-scripts/route-iface
     ADDRESSX = 網段    // 『X』從『0』開始 
     NETMASKX = netmask
     GATEWAYX = router_ip

    調整系統核心參數-sysctl

    指令 sysctl 可在系統運作時,變更設定核心參數(configure kernel parameters at runtime)
    sysctl 的根目錄在 /proc/sys(The parameters available are those listed under /proc/sys/),而 /proc 目錄不佔硬碟空間,執行時載入記憶體,因此空間顯示為 『0』。
    # ll -d /proc/
     dr-xr-xr-x. 171 root root 0 Oct 5 20:10 /proc/

    列出所有參數設定
    # sysctl -a
    查詢有關 icmp 及路由轉發的參數
    # sysctl -a | grep --color icmp
     net.ipv4.ip_forward = 0
     net.ipv4.icmp_echo_ignore_all = 0
     net.ipv4.icmp_echo_ignore_broadcasts = 1
     ...

    變更參數設定,但重開機後會失效
    # sysctl -w net.ipv4.icmp_echo_ignore_all=1  // 等號不能有空格
    # sysctl -w net.ipv4.ip_forward = 1

    將參數設定寫入組態檔『/etc/sysctl.conf』,以防重開機後失效
    方法一
    # vim /etc/sysctl.conf
     net.ipv4.icmp_echo_ignore_all = 1
     net.ipv4.ip_forward = 1
    # sysctl -p  // 不用重開機就能生效

    方法二
    # echo "net.ipv4.icmp_echo_ignore_all = 1" >> /etc/sysctl.conf
    # echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

    指令特殊用法

    1. 輸出前一次指令執行狀態,『0』代表成成,『1』代表失敗。
    # tcpdump -D
     1.eth0
     2.usbmon1 (USB bus number 1)
     3.usbmon2 (USB bus number 2)
     4.any (Pseudo-device that captures on all interfaces)
     5.lo
    # echo $?
     0

    2. 依時間排序檔案
    # ls -lt  // 最新的檔案在最上面
    # ls -ltr  // 最舊的檔案在最上面

    NTP 伺服器

    NTP (Network Time Protocol)是網路校時的協定,台灣 NTP 伺服器由中華電信研究所時間與頻率國家標準實驗室(http://www.stdtime.gov.tw/chinese/bulletin/NTP%20promo.txt)提供
    1. tock.stdtime.gov.tw
    2. watch.stdtime.gov.tw
    3. time.stdtime.gov.tw
    4. clock.stdtime.gov.tw
    5. tick.stdtime.gov.tw
    設定開機時啟動 NTP 服務
    # chkconfig ntpd on
    # chkconfig --list ntpd
     ntpd      0:off 1:off 2:on 3:on 4:on 5:on 6:off

    啟動 NTP 服務
    # service ntpd start
     Starting ntpd:           [ OK ]
    # service ntpd status
     ntpd (pid  3633) is running...

    設定 NTP 伺服器
    # vim /etc/ntp.conf
     server time.stdtime.gov.tw iburst  // iburst 加速對時
    # service ntpd restart

    手動對時:若不想開啟 NTP 服務,可將手動對時的指令寫入排程(crontab),定時執行
    # ntpdate time.stdtime.gov.tw
     19 Sep 10:16:32 ntpdate[3707]: the NTP socket is in use, exiting  // 必需關閉 NTP
    # service ntpd stop
    # ntpdate time.stdtime.gov.tw
     19 Sep 10:17:07 ntpdate[3758]: step time server 59.124.196.84 offset 1.352043 sec

    網路校時排程(在未開啟 NTP 服務情況)
    # crontab -e
     */10 * * * * /usr/sbin/ntpdate time.stdtime.gov.tw && hwcolck -w
    # date 01010909  // 調整成錯誤時間,以測試排程是否有正常動作
     Sun Jan  1 09:09:00 CST 2012   // 10 分鐘後會自動網路校時成正確時間
    # date
     Wed Sep 19 10:51:57 CST 2012

    註:『&&』的功用是若前一個指令執行成功,才會執行後面的指令,『||』的功用是若前一個指令執行失敗,才會執行後面的指令

    NTP 狀態查詢
    # netstat -nap | grep ntp  // NTP 的通訊埠號為 123

    ntpstat
     synchronised to NTP server (59.124.196.84) at stratum 3
     time correct to within 1000 ms
     polling server every 64 s
    # ntpq -p
     remote       refid   st t  when poll reach delay   offset   jitter
     ==============================================================================
     *59-124-196-84.H 59.124.196.86 2  u    8  64 1  19.389  39.112  6.558


    變更時區
    # date
     Wed Sep 19 11:22:27 CST 2012
    # ls /usr/share/zoneinfo/Asia/ | grep --color Taipei
     Taipei
    # vim /etc/sysconfig/clock
     ZONE="Asia/Taipei"
    # \cp /usr/share/zoneinfo/Asia/Taipei /etc/localtime
    # date
    Wed Sep 26 20:56:04 CST 2012

    註:全球各地區的時區對應檔在 /usr/share/zoneinfo/

    透過別台主機對時
    主機 A:連線至 time.stdtime.gov.tw 對時
    主機 B:連線至主機 A 對時
    此時主機 A 的通訊埠 123(udp)要開啟,並在 /etc/ntp.conf 設定允許主機 B 連線

    主機 A
    # iptables -A INPUT -p udp -m state --state NEW -m udp --dport 123 -j ACCEPT
    # vim /etc/ntp.conf
     restrict 140.112.163.58

    主機 B
    # ntpdate 主機 A_IP
     19 Sep 15:20:53 ntpdate[8971]: adjust time server  主機 A_IP  offset 0.184811 sec

    安裝及移除套件-YUM

    20121120新增指令

    YUM 與 RPM 皆可安裝、移除、查詢及升級套件,差別在於 YUM 可透過網路自動下載安裝與升級的套件。

    本人常用的三個伺服器
    1. 中華電信:http://mirror01.idc.hinet.net/CentOS/6.3/os/x86_64/
    2. 台灣高品質學術研究網路:http://ftp.twaren.net/Linux/CentOS/6.3/os/x86_64/
    3. 國立中山大學:ftp://ftp.nsysu.edu.tw/Unix-like/CentOS/6.3/os/x86_64/
    yum 組態檔設定:以中山大學伺服器為例
    # vim /etc/yum.repos.d/nsysu.repo
      [nsysu]
      name=nsysu
      baseurl=ftp://ftp.nsysu.edu.tw/Unix-like/CentOS/6.3/os/i386/
      enabled=1
      gpgcheck=1
      gpgkey=ftp://ftp.nsysu.edu.tw/Unix-like/CentOS/6.3/os/i386//RPM-GPG-KEY-EPEL-6

    upgrade/downgrade:升級/降級套件
    範例:升級 rpm 套件
    # yum upgrade [-y] rpm  // -y 自動安裝
     Loaded plugins: fastestmirror, refresh-packagekit, security
     Loading mirror speeds from cached hostfile
     * base: mirror01.idc.hinet.net
     * epel: mirror01.idc.hinet.net
     * extras: mirror01.idc.hinet.net
     * updates: centosa5.centos.org
      ......
     Dependencies Resolved
       ==================================================================================
     Package   Arch  Version    Repository  Size
     ==================================================================================
     Updating:
     rpm      i686  4.8.0-27.el6         base         898 k
     Updating for dependencies:
     rpm-libs    i686  4.8.0-27.el6         base          313 k
     rpm-python  i686  4.8.0-27.el6        base          53 k
     Transaction Summary
     ==================================================================================
     Upgrade 3 Package(s)
     Total download size: 1.2 M
     Is this ok [y/N]: 詢問是否安裝

    info:查詢套件資訊
    範例:查詢 vsftpd 套件資訊
    # yum info vsftpd
     Loaded plugins: fastestmirror, refresh-packagekit, security
     Loading mirror speeds from cached hostfile
     * base: mirror01.idc.hinet.net
     * epel: mirror01.idc.hinet.net
     * extras: mirror01.idc.hinet.net
     * updates: centosa5.centos.org
     Installed Packages
     Name : vsftpd
     Arch : i686
     Version : 2.2.2
     Release : 11.el6
     Size : 344 k
     Repo : installed
     From repo : base
     Summary : Very Secure Ftp Daemon
     URL : http://vsftpd.beasts.org/
     License : GPLv2 with exceptions
     Description : vsftpd is a Very Secure FTP daemon. It was written completely from : scratch.

    search:搜尋套件
    範例:搜尋 vsftpd 套件
    # yum search vsftpd
     Loaded plugins: fastestmirror, refresh-packagekit, security
     Loading mirror speeds from cached hostfile
     * base: mirror01.idc.hinet.net
     * epel: mirror01.idc.hinet.net
     * extras: mirror01.idc.hinet.net
     * updates: centosa5.centos.org
     ============================= N/S Matched: vsftpd ============================
     vsftpd.i686 : Very Secure Ftp Daemon
     Name and summary matches only, use "search all" for everything.

    provides查詢檔案位於屬於哪個套件
    範例:查詢 sshd_config 屬於哪個套件
    # yum provides */sshd_config
     Loaded plugins: fastestmirror, refresh-packagekit, security
     Loading mirror speeds from cached hostfile
     * base: mirror01.idc.hinet.net
     * epel: mirror01.idc.hinet.net
     * extras: mirror01.idc.hinet.net
     * updates: centosa5.centos.org
     openssh-server-5.3p1-81.el6.i686 : An open source SSH server daemon
     Repo : base
     Matched from:
     Filename : /etc/ssh/sshd_config

    clean:清除 repolist 資料
    # yum clean all
     Loaded plugins: fastestmirror, refresh-packagekit, security
     Cleaning repos: base epel extras isu updates
     Cleaning up Everything
     Cleaning up list of fastest mirrors

    reinstall:重新安裝
    # yum reinstall vsftpd
    update/check-update:更新
    # yum check-update openssh
    erase/remove:移除
    # yum remove vsftpd
    localinstall:本地套件安裝
    # yum localinstall vsftpd
    群組安裝相關指令
    grouplist
    groupinstall
    grouperase
    groupinfo

    網路介面設定-IP Aliases

    2 種 IP 別名的設定方式

    方法 1. 推薦此方法
    # ifconfig eth0:0 192.168.0.1/24
    方法 2.
    # ip addr add 192.168.0.1/24 dev eth0 label eth0:0
    寫入網路組態檔,重開機時不會消失
    # vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
     DEVICE=eth0:0
     IPADDR=192.168.0.1
     PREFIX=24   // NETMASK 255.255.255.0
     ONPARENT=yes
    # service network restart

    網路介面設定-Bonding

    將多個網路介面整合成單一通道的技術
    在 KVM 中新增 2 張網卡實作 Bonding,而網路卡組態檔路徑在 /etc/sysconfig/network-scripts/ifcfg-*
    實作前請先關閉 NetworkManager 服務(含開機生效設定)

    新增 Bonding 網路組態檔
    # cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-band0
    # vim /etc/sysconfig/network-scripts/ifcfg-band0
     DEVICE=bond0
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
     BONDING="mode=1 miimon=50" // mode 1:Active-Backup

    設定 eth0 的網路組態檔
    # vim /etc/sysconfig/network-scripts/ifcfg-eth0
     DEVICE=eth0
     HWADDR=52:54:00:6b:bf:b2
     NM_CONTROLLED=no
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
     MASTER=bond0
     SLAVE=yes

    設定 eth1 的網路組態檔
    # vim /etc/sysconfig/network-scripts/ifcfg-eth1
     DEVICE=eth1
     HWADDR=52:54:00:11:fe:26
     NM_CONTROLLED=no
     ONBOOT=yes
     BOOTPROTO=dhcp
     TYPE=Ethernet
     USERCTL=no
     PEERDNS=yes
     IPV6INIT=no
     MASTER=bond0
     SLAVE=yes

    設定 Bonding 設定檔
    # vim /etc/modprobe.d/bonding.conf
      alias bond0 bonding

    查看 Bonding 狀態
    # cat /proc/net/bonding/band0

    NFS伺服器

    20121211 修訂 NFS 伺服器內容,請參考 NFS 伺服器(第二版)

    NFS(Network FileSystem)的功能就是可以透過網路,讓不同的機器、不同的作業系統、可以彼此分享個別的檔案 (share files) 並進行操作。

    NFS Server(10.10.10.128)
    NFS 需要遠端程序呼叫 (RPC) 的服務,因此安裝 rpcbind 套件。
    # yum install -y nfs-utils rpcbind
    開機時啟動
    # chkconfig nfs on
    # chkconfig rpcbind on

    啟動服務
    # service rpcbind start  // 先啟動
    # service nfs start

    關閉防火牆
    # iptables -F

    NFS 伺服器端分享資料夾設定
    # vim /etc/exports
     /var/ftp *(ro)
    # service nfs restart

    補充:





  • 修改 /etc/exports 內容後,重啟 NFS 服務套用更新,但會使得在線上的使用者斷線,使用以下指令可避免此情況發生。
  • # exportfs -rv  // 參數『v』代表詳細模式,可以不用加
  • 以下四個參數,讓 NFS 客戶端使用者登入 NFS 伺服端時,轉換身份(以 UID 為主)並取得相對應的權限操作。


  • NFS Client(10.10.10.129) # rpcinfo -p 10.10.10.128  // 查看 NFS 伺服器是否有提供 NFS 服務
    # showmount -e 10.10.10.128
     Export list for 10.10.10.128:
     /var/ftp *
    # mkdir /mnt/nfsclient
    # mount 10.10.10.128:/var/ftp /mnt/nfsclient
    # df
     檔案系統                1K- 區段   已用     可用     已用 % 掛載點
     10.10.10.128:/var/ftp 15163392 2951168 11441664 21% /mnt/nfsclient
    注意:無法對資料夾 /mnt/nfsclient 進行新增刪除的操作。
    NFS 客戶端的身份為管理者 root,但登入 NFS 伺服端後,系統會變更成使用者 nfsnobody,而通常使用者的權限對資料夾只有讀的權限。
    有 2 種方法可以解決這個問題,都是在 NFS 伺服器端上設定
    1. 將共享資料夾的使用者權限變更成 xx7
    2. 在 /etc/exports 組態檔中加入 no_root_squash 參數,讓客戶端的管理者登入後亦維持管理者身份但此舉有降低系統安全性的風險
    開機時掛載
    # vim /etc/fstab
     nisserver128.example.com:/var/ftp /mnt/nfsclient nfs ro 0 0


    補充:NFS 伺服器端防火牆設定


    NFS 服務埠號為 2049,rpcbind 服務埠號為 111,除此之外 STATD_PORT、MOUNTD_PORT、LOCKD_UDPPORT、LOCKD_TCPPORT 埠號皆系統會隨機配置,造成 NFS 客戶端無法連線,因此若不固定埠號,就是要把整個防火牆關閉,但此舉非常危險。
    # vim /etc/sysconfig/nfs
     RQUOTAD_PORT=875
     LOCKD_TCPPORT=32769
     LOCKD_UDPPORT=32769
     MOUNTD_PORT=892
     STATD_PORT=662
    # iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 111 -j ACCEPT
    # iptables -A INPUT -p udp -m state --state NEW -m udp --dport 111 -j ACCEPT
    # iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 875 -j ACCEPT
    # iptables -A INPUT -p udp -m state --state NEW -m udp --dport 875 -j ACCEPT
    # iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 32769 -j ACCEPT
    # iptables -A INPUT -p udp -m state --state NEW -m udp --dport 32769 -j ACCEPT
    # iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 662 -j ACCEPT
    # iptables -A INPUT -p udp -m state --state NEW -m udp --dport 662 -j ACCEPT
    # iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 892 -j ACCEPT
    # iptables -A INPUT -p udp -m state --state NEW -m udp --dport 892 -j ACCEPT
    # iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 2049 -j ACCEPT
    # iptables -A INPUT -p udp -m state --state NEW -m udp --dport 2049 -j ACCEPT
    # service nfs restart


    Troubleshooting


  • 無法掛載:若出現被 NFS Server 拒? 的訊息,請先檢查是否有權限存取。假設 NFS Server 設定只有 10.10.10.128 可以存取,但目前的 NFS Client 的 IP 是 10.10.10.129,因此會被拒絕。
  • client # mount nfs_server_ip:/var/ftp /mnt/nfsclient
       mount.nfs: access denied by server while mounting nfs_server_ip:/var/ftp
    client # showmount -e nfs_server_ip
       Export list for nfs_server_ip:
       /var/ftp 10.10.10.128