Quản trịLinux - hoacuong.files.wordpress.com · Lập trình với shell script. Mạng TCP/IP...

Preview:

Citation preview

Quản trị Linux

� Tổng quan – cài đặt HĐH Linux� Làm việc với Linux� Cấu hình hệ thống, thiết bị� Quản trị Linux trên LAN

� Mạng TCP/IP� Cài đặt, quản lý các dịch vụ LAN

� Lập trình với shell script

Mạng TCP/IP

� Interface cho NIC� Thiết lập interface� Thiết lập route� Danh mục Host� DNS� Các công cụ chuẩn đoán, kiểm tra

Thiết lập Interface cho NIC

� Tệp cấu hình� /etc/sysconfig/network(có thể thay đổi đ/c theo distro hay version)

� Dùng ifconfig (IP tĩnh)� CMD ifconfig� Hay công cụ GUI theo từng distro

� Dùng DHCP (IP được cấp bởi DHCP server)� Tồn tại 1 DHCP server đang chạy

Thiết lập Interface cho NIC - ifconfig

� Địa chỉ� /sbin/ifconfig

� Xem& cấu hình các interface� Xem

� ifconfig [interface] ; mặc định : tất cả interface đang active

� Cấu hình1. ifconfig interface IP netmask đ/c-mask broadcast đ/c-BC� Ví dụifconfig eth0 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255

2. ifconfig interface <mô tả interface>ifconfig eth0 192.168.1.2/24

Thiết lập Interface cho NIC - ifconfig

� Thao tác trên các interface� Active

� ifconfig interface up

� DeActive� ifconfig interface down

� Hiển thị trạng thái� netstat –in ; i: trạng thái các interface, n: dạng số� Kết quả

� R: đã được cấu hình� B: Broadcast – capable� U: active� L: loopback

Thiết lập Interface cho NIC - ifconfig

� Thao tác trên các interface� Hiển thị tất cả cấu hình

� Tệp /etc/sysconfig/network-scripts/ifcfg-<interface>Ví dụ: cat /etc/sysconfig/network-scripts/ifcfg-eth0

Ip command

IP v6

IP Aliasing

� single host to have multiple IP addresses� Each interface in Linux system can have multiple IP addresses assigned� Same Windows

� Ex� eth0 is the main interface, � eth0:0 is aliased interface, � eth0:1 is aliased interface, ..

IP Aliasing

Thiết lập Route� Gateway

� Đường đi giao tiếp hệ thống với bên ngoài (LAN, iternet)� Cùng LAN

� Các PC có cùng gateway� Internet

� Dùng router

� Các kiểu route� Minimal

� Được thêm vào interface khi cấu hình NIC. Dùng trên LAN cố định� Static

� LAN có dùng router (liên lạc với LAN khác)� Dùng cmd route

� Dynamic� Dùng Routing Internet Protocol,…

Thiết lập Route - Minimal

� Hiển thị các route� Cmd: netstat –rn � Hay /sbin/route� Kết quả

� Destination� Đ/c mạng hay Host đích

� Gateway� Route

� Flags� U: up , G: gateway, H:host

� Interface� NIC

Thiết lập Route - Static

� CMD route� Cấu hình bảng định tuyếnroute [option] [net|host|default] gw <IP> [interface]� Option

� Add: thêm, del: xóa (route)� Net

� -net <interface của net>: -net 192.168.100.0/24� -host <IP của host>� Default

� Route được xác định là route mặc định của hệ thống

� IP:� IP của route

� Interface: I kết hợp với NIC

Thiết lập Route - Static� Route – Ví dụ

� Tạo route mặc định cho hệ thống � Route add default gw 172.16.12.1

� Thêm 1 route cho mạng LAN� Route add –net 192.168.100.0/24 gw 192.168.2.1 eth0� Route add –net 172.16.1.0 netmask 255.255.255.0 gw 192.168.2.1 eth0

� Tệp cấu hình � /etc/sysconfig/static-routes

� Route cho LAN 0.0.0.0� Route default

� Kiểm tra� Route -n

Classless interdomain routing

� classes use of address space � waste some addresses

� Ex: class C address space to a certain university campus

� � inefficient� Solution

� no restriction on classes, emerged� Classless interdomain routing (CIDR)

Classless interdomain routing � allow variable-length prefix

� to represent network ID � remaining bits of 32-field address � represent hosts within network

� For example� 1 organization use 20-bit network ID� another organization use 21-bit network ID� with first 20 bits of these 2 network IDs is identical� � address space of 1organization contains that of another one

router using CIDR address space

� increase in speed of routers � greatly reduced size of routing tables � Supernetting (for routing table)

� a single routing entry � a group of adjacent addresses

� 2 entries with same prefix � router chooses between entries, using longest-prefix-match technique

router using CIDR address space

� Assume that a packet with destination IP 205.101.0.1 is received by router R1

� In entries of this router, 2 routes, � L1 205.101.8.0/20 � L2 205.101.0.0/21

� CIDR choose longer prefix be eligible match� link L1, with its 21-bit prefix, � This link eventually routes packet to network N3.

CIDR routing

CIDR routing� CIDR allows us to reduce number of entries in a router's

table � using aggregate technique,

� all entries have some common partial prefix can � combined into 1 entry.

� For example� 2 entries 205.101.8.0/20, 205.101.0.0/21 be combined into

205.101.0.0/19

� Combining entries in routing tables not only saves space but also enhances speed of routers, � routers search among fewer addresses

DHCP

� DHCP� Cấp IP dynamic khi client kết nối đến hệ thống

� DHCP server� Kèm theo distro� Tên

� Dhcpd� Đ/c: /etc/init.d/dhcpd

� Tệp cấu hình� /etc/dhcpd.conf� Mô tả các subnet và group cho mạng LAN

Install

Install on ubuntusudo apt-get install dhcp3-server

Download & Installhttp://ftp.isc.org/isc/dhcp/dhcp-4.1.0a1.tar.gz

Thiết lập DHCP

� DHCP server -/etc/dhcpd.conf

Dhcpd.conf

Common dhcpd.conf Options

Thiết lập DHCP client

� DHCP Client� Kèm theo distro Workstation� Tên

� Hay dhclient

DHCP client’s option

Danh mục host

� Định danh mục tên kết hợp với NIC� Tệp cấu hình

� /etc/hosts

� Kiểm tra� CMD hostname [-i]

DNS

� Domain Name server� Được cài đặt bởi BIND (Berkeley Internet Name Domain)

� Tên: named

� Cấu hình� Cấu hình tệp resolve.conf� Cấu hình Bind: named.conf� Tạo các tệp CSDL cho các zone

DNS -resolve.conf

� Cấu hình việc quản lý tên của server� Đ/c: /etc/resolv.conf. Gồm

� nameserver� IP về server để DNS có thể dùng làm root

� search� Chuỗi domain làm đường dẫn tìm kiếm host

DNS -named.conf

� Cấu hình server DNS� Đ/c: /etc/named.conf

� Gồm� Tên, đ/c các tệp CSDL chứa thông tin cho DNS� Các khai báo

� Options� Các tuy chọn cấu hình toàn cục cho server & các giá trị default

� Zone� Các vùng tên miền của server

� Server� Định nghĩa các đặc tính của remote server

� Include� Bao hàm tệp CSDL khác

DNS -named.confThư mục default chứa các tệp CSDL

Zone Root của DNS

Zone exemple.fr -> cần tệp named.exemple.fr

Zone local của hệ thống

Zone reverser

Tệp chứa mô tả root

Domain & Zone

� Domains � designated along organizational boundaries.

� Single organization � separated into smaller administrative subdomains. � Each subdomain

� gets its own zone.

� All of zones collectively form entire domain.

Zone� example.org is a domain.

� Within it are subdomains � .engr.example.org, � .marketing.example.org, � .sales.example.org, and .admin.example.org.

� Each of four subdomains has its own zone. � .example.org has some hosts within it

� that do not fall under any of subdomains; � Thus, it has a zone of its own. � � “example.org” domain is actually composed of five zones in total.

Server

� Primary servers � ones considered authoritative for a particular domain.� primary name server for a domain is

� a DNS server that knows about all hosts and subdomains existing under its domain.

� Secondary servers� backups & as load distributors for primary name servers.

� a site queries a secondary name server, secondary responds with authority.

Server

� Caching servers� contain no configuration files for any particular domain.

� Caching data� root name servers

� act as the first port of call for topmost parts of domain namespace.

� root zone file describes where authoritative servers for DNS top-level domains

DNS -named.conf

Tệp named.exemple.frTên của PC dùng làm server của zone

DNS –Các tệp named

� Named.local� Dùng để đổi IP: 127.0.0.1 thành localhost� Thuộc zone 0.0.127.IN-ADDR-ARPA (zone reverse)

DNS –Các tệp named� Các ghi chú trong named reverse

� SOA� Tham số cho toàn zone

� NS� Name server

� A� Tên -> IP

� PTR� IP-> tên

� MX� Mail server

� CNAME� Alias cho tên

DNS –Các tệp named

� Named.rev� Dùng để đổi IP thành tên& ghi các PTR

DNS –khởi động� Tệp /etc/rc.d/init.d/named

� Tham số� Start� Stop

� Kiểm tra trạng thái� Ndc

� Status� Start� Restart

� Công cụ tìm tên� Nslookup

� Nslookup [tên]

DNS –công cụ GUI

� Kèm theo distro� Tên

� DNS config

� Khởi động� System/bind config

Các công cụ TEST� Netstat

� Xem trạng thái của TCP/IP, NIC� Option thông dụng

� -I : thông tin về NIC� -nr: thông tin về route

� Ping� Kiểm tra thông mạng giữa các PC, LAN� Ping <machine|IP đích>

� Nslookup� Kiểm tra DNS

� Traceroute� Traceroute <host | NET>� Hiển thị lộ trình đi đến 1 host hay LAN đích

NIS

NIS

NIS server

NIS server- install

NIS install

1. Domain name

Start NIS

Edit makefile

� Designating Slave Servers: NOPUSH� Minimum UIDs and GIDs: MINUID and MINGID

� Merging Shadow Passwords with Real Passwords: MERGE_PASSWD

� Merging Group Shadow Passwords with Real Groups: MERGE_GROUP

� Designating Filenames� What Gets Shared: The all Entry

4. Ypinit

Config client NIS

NIS

� Editing the /etc/yp.conf File

NIS

� Enabling and Starting ypbind

EDITING THE /ETC/NSSWITCH.CONF

NIS working

Quản trị Linux

� Quản trị Linux trên LAN� Mạng TCP/IP� Cài đặt, quản lý các dịch vụ LAN

� Chia sẻ tài nguyên� NFS, Máy in� Samba

� Mail server: Sendmail, Postfix� Web server: Apache� Proxy � Client- server VNC

� Nhập môn bảo mật� IP table, công cụ ipchains

� Lập trình với shell script

Chia sẻ tài nguyên - NFS

� Network File System� Client –server dùng chia sẻ các tệp giữa các hệthống Linux

� Cài đặt� Khởi động server� Client

� Kích hoạt & dùng thư mục chia sẻ

Chia sẻ tài nguyên - NFS

� Cài đặt� Server

� Gói APP nfsd (Fedora)� Dùng rpm với đĩa cài đặt Linux (nếu là PC workstation)� Xác định& khởi động tiến trình portmap dùng cho các RPC

� /etc/rc.d/init.d/portmap

� Gói APP trên debian: nfs-user-server

� Client� Khởi động mount với option –V

� Mount –V

ENABLING NFS IN FEDORA

ENABLING NFS IN FEDORA

NFS on Ubuntu

NFS - Cài đặt

� Tệp cấu hình /etc/exports� Mô tả các tài nguyên cần chia sẻ� Nội dung

� Mỗi tài nguyên: 1 dòng� <Tên tài nguyên> <IP|tên PC client> (options)� Tên tài nguyên

� Path đến thư mục, tên mô tả thiết bị (máy in)

� IP| tên PC client� PC Client phải được phép truy cập hệ thống với /etc/hosts.allow

Chia sẻ tài nguyên – NFS - Cài đặt

� Tệp cấu hình /etc/exports� Nội dung

� Options� Ro: read only� Rw: read&write� No_root_squash

� Client truy cập với root sẽ được xác định như root (default: ngược lại)

� Ví dụ� /home/share/ 192.168.1.2(rw) 192.168.32.2(ro)

exportfs

NFS server configuration utility

showmount Command

NFS - Cài đặt

� Tệp cấu hình /etc/hosts.deny� Cấm những client (không được phép trong hosts.allow) truy cập vào daemon hệ thống

� Nội dung� Daemon (ALL) : ALL| danh sách IP(tên) client

� Ví dụ� portmap: 192.168.32.2� lockd: 192.168.32.2� rquotad: 192.168.32.2� mountd: 192.168.32.2� statd:192.168.32.2

NFS - Cài đặt

� Tệp cấu hình /etc/hosts.allow� Cho phép các client truy cập vào daemon hệ thống � Nội dung

� Daemon (ALL) : ALL| danh sách IP(tên) client

� Ví dụ� portmap: 192.168.32.2� lockd: 192.168.32.2� rquotad: 192.168.32.2� mountd: 192.168.32.2� statd:192.168.32.2

Chia sẻ tài nguyên - NFS

� Khởi động server� Gọi daemon nfs

� /etc/rc.d/init.d/nfs start

� Stop� Option stop

� Đọc lại /etc/exports� Option reload� CMD exportfs

� Kiểm tra trạng thái� Rpcinfo -p

Chia sẻ tài nguyên - NFS

� Client� Mount

� Mount IP|tên client:/tên thư mục <mount point>

� Chú ý� Quyền truy cập của thư mục phải được định nghĩa lại với nhóm other.

� Ví dụ� Mount 192.168.1.1:/home/share /mnt/share

NFS Client

NFS Client

NFS Client

Chia sẻ tài nguyên – Máy in

� GUI� Dùng Printer Configurator

� Cài đặt máy in kiểu UNIX thay cho local� Chỉ địa chỉ của remote printer (PC!printer)

� Tệp /etc/printcap� Khai báo remote printer

� Option: rm=<IP|tên>:rp=tên máy in� Máy in đã được khai báo

� Ví dụ� Lw:\

� :lp=:rm=bean:rp=Hplaser:\

Chia sẻ tài nguyên – Samba

� App Client –Server� Chia sẻ tài nguyên (tệp, máy in) giữa các hệ thống Linux-

Windows

� Daemon cơ sở� NETBios

� Giao thức chia sẻ tài nguyên giữa các hệ thống Windows� Smbd

� Daemon chính của samba� Nmbd

� Domain Name server của NetBIOS. Đảm bảo tương ứng IP và đ/c NETBios

Chia sẻ tài nguyên – Samba

� Cài đặt server� Gói APP

� Samba version 2.0. Web site: www.samba.org� Dùng rpm

� Rpm – ivh samba-2.0.5a-3mdk.i586.rpm

� Cấu hình� Tệp cấu hình /etc/samba/smb.conf� SWAT

� Khởi động� /etc/rc.d/init.d/samba start

� Kiểm tra� /etc/rc.d/init.d/samba status

Chia sẻ tài nguyên – Samba

� Tệp /etc/samba/smb.conf� Gồm nhiều section

� Global� Các thông số cấu hình chung cho server Samba

� Homes� Mô tả thư mục cần chia sẻ

� Printers� Mô tả máy in cần chia sẻ

Chia sẻ tài nguyên – Samba

� Tệp /etc/samba/smb.conf

Chia sẻ tài nguyên – Samba

� Tệp /etc/samba/smb.conf� Global

� Security: cấp độ bảo mật (thường dùng: user)� Smb passwd file: nơi đặt file chứa thông tin password� ..

� Homes� Path: Đ/c của thư mục chia sẻ� Readonly | Writeable: (yes/no) � Browseable: (yes/no) cho phép browse (không) thư mục� Create mask: định nghĩa ATTR tệp được người dùng tạo� Directory mode: định nghĩa ATTR thư mục được người dùng tạo

Chia sẻ tài nguyên – Samba

� Chia sẻ thư mục� [dospc]

� Comment = Documentation PC� Path = /usr/doc/dospc� Browseable = yes� Writeable = no� Public = yes // Quyền truy cập : ALL

� [research]� Comment = Research dept’s shared document� Path = /home/research� Browseable = no� Writeable = yes� Create mode = 0750� Host allow = bean, xavier

Chia sẻ tài nguyên – Samba� Chia sẻ máy in

� [printers]� Comment = All shared printers� Path = /var/spool/samba� Browseable = no� Guest ok = no� writeable = no � Printable = yes

� [HP4]� Comment = Laser HP 4� Path = /var/spool/samba� public = no� Create mode = 0700� Printable = yes� Printer = lp

Selected Samba Commands

Remote access using mount

SMBFS

SMB users

A Basic Domain Member

� In smb.conf� security = server� password server = <NT Server Name>

� Part of an Active Directory Network

� security = ads

Chia sẻ tài nguyên – Sendmail

� Client-Server� Nhận gởi mail giữa các hệ thống

� Server� Gói APP: sendmail. Đ/c /usr/sbin� Tệp cấu hình /etc/sendmail.cf

� Client� Daemon

� IMAP: imapd (/usr/share/)

� Sendmail agent� /etc/rc.d/init.d/sendmail

Chia sẻ tài nguyên – Sendmail

� Cấu hình sendmail (sendmail.cf)� Cấu hình trung gian với tệp /etc/mail/sendmail.mc� Dùng macro m4 để sinh tệp sendmail.cf

� m4 /etc/mail/sendmail.mc > /etc/sendmail.cf

� Khởi động lại sendmail� /sbin/service sendmail restart

� Các tệp cấu hình trong /etc/mail� Domaintable

� Tên miền mail

� Local-hosts-name� Các alias cho machine

Chia sẻ tài nguyên – Sendmail

� Mask� Machine đóng vai trò gateway cho hệ thống mail� Chứa trong tệp /etc/mail/local-hosts-names� Ví dụ: PC mail.bigcorp.com là gw

Chia sẻ tài nguyên – Sendmail

� Mask� Các PC torgo, poodle,.. khai báo gw trong sendmail.cf với option DM, DH

Chia sẻ tài nguyên – Sendmail

� Dùng mail với LDAP� Lightweight Directory Access Protocol� Tìm nhanh 1 user trên 1 domain rộng� Với sendmail

� Dùng kết quả của LDAP trong xác định đ/c mail.

� Khởi động với sendmail LDAP� Cài đặt & cấu hình đúng LDAP (OpenLDAP)� Thêm vào sendmail.cf khai báo LDAP

Chia sẻ tài nguyên – Sendmail

� Fetchmail� Kết nối & lấy mail từ remote server về MUA� Gồm POP3 và IMAP� Gói APP: fetchmail� Cấu hình

Apache – Tổng quan

� Web server khả chuyển� Trên nhiều OS khác nhau

� Trên Redhat - Linux� Gói APP: apache.rpm

� Tác vụ� Cài đặt apache server� Khởi động web server� Cấu hình apache server

Apache – Cài đặt server

� Dùng rpm� Rpm –ivh apache.rpm

� Các thư mục� /etc/httpd/conf

� Chứa các tệp cấu hình

� /etc/rc.d� Chứa tệp script khởi động server

� /home/httpd� Các tệp tài liệu, script của server

Apache – Khởi động server

� Khởi động� Gọi daemond httpd

� /etc/rc.d/initd.d/httpd start

� Kiểm tra� Mở 1 web browser� Mở trang: http://localhost.domain :8080

� Stop� /etc/rc.d/initd.d/httpd stop

� Cấu hình với GUI� System/ Apache Configurator

Apache – Cấu hình apache

� Các tệp cấu hình� httpd.conf

� Cấu hình cách thực hiện server

� access.conf� Mô tả người dùng truy cập server

� srm.conf� Mô tả CSSDL tài liệu của server

Apache – Cấu hình httpd.conf

Apache – Cấu hình apache

� Các option� ServerType

� Quy định cách khởi động apache� Standalone: như 1 daemon độc lập� Xinetd: qua xinetd

� ServerRoot� Đ/c đến root của server

� DocumentRoot� Đ/c đến CSDL root của server

Apache – Cấu hình apache

� Các khai báo� Mô tả thư mục tương đối trong document Root

� <Directory> � Options

� Định nghĩa các option cho thư mục� AllowOverride

� Cách thức sinh tệp .htaccess của thư mục� Order

� Thứ tự áp dụng allow deny� Allow (hay Deny)

� Định các client có (không) quyền truy cập� </Directory>

Apache – Cấu hình apache

� Các khai báo� Mô tả thư mục tương đối trong document Root

Apache – Cấu hình apache

� Các khai báo� Mô tả host virtual

� <VirtualHost đ/c ip | host name> � ServerAdmin

� Đ/c mail của admin� DocumentRoot

� Thư mục root của host� ServerName

� Tên domain kết hợp với host� ErrorLog

� Định file log của host� </VirtualHost>

Apache – Cấu hình apache

� Các khai báo� Mô tả host virtual

Apache – Cấu hình apache

� access.conf� Định nghĩa cách truy cập toàn cục cho người dùng

� Mỗi directory có thể định nghĩa kiểu riêng

� Dùng khi cần bảo mật doc với người dùng

Apache – Cấu hình apache

� srm.conf� Cấu hình tài nguyên của site� UserDir

� Đ/c của thư mục tương đối với Root

� DocumentRoot� Đ/c của cây tài liệu

Virtual Network Computing

� Remote access với GUI� Hai trường hợp dùng remote access

� Client – server có cùng OS (Linux)� Một chức năng của Linux� Ví dụ: start prog từ PC bean và hiển thị nó trên màn hình (default) của PC quang

� ./prog –display quang:0

� Client –Serveur khác OS (Windows, Linux)� Serveur: Linux� Client: Windows� Dùng VNC

VNC – Cài đặt

� Gói APP� Distro Redhat vnr.rpm� Download

� www.uk.research.att.com/vnc/download.html

� Cài đặt� Dùng rpm� Chú ý

� Không đặt vnc như service khởi động auto

VNC – Cài đặt & sử dụng Server

� Cài đặt� Gói vncserver.rpm

� Khởi động� Gọi daemon vncserver

� Vncserver� 1 số ID được trả về cho người dùng: X

� Ngưng� Vncserver –kill :X

� Sử dụng� Sửa đổi ~/.vnc/xstartup: chọn X env

� Startgnome hay startkde

VNC – Cài đặt & sử dụng Client

� Cài đặt� Gói vnc.rpm

� Khởi động� Gọi daemon vncviewer

� vncviewer� Vào tên server: X

� Ngưng� Tương tự X env khác

create database using mySQL

� MySQL � Open Source,

� www.mysql.com� install

� MySQL-server-4.0.14.i386.rpm.� MySQL-client-<version>.<architecture>.rpm.� MySQL-shared-<version>.<architecture>.rpm,� MySQL-devel-<version>.<architecture>.rpm.

MySQL

� Start� using /etc/rc.d/init.d/mysql start

MySQL - working

� Logon� mysql -u root mysql

� running server with mysqladmin command� mysqladmin -u root version� mysqladmin -u root password newpassword

Working

Working

Working – create database

Working – create table

Working – insert data

Configure MySQL for Websites

� review the list is with the following command:� cache search mysql | grep apache

MySQL

MySQL

Administrator

Users

mysqladmin

Creating a Database

Create table

Insert data

MySQL Control Center

Connect to server

Connect to server

Connect to server

Recommended