Các thao tác quản trị hệ thống cơ bản
Ngô Văn Công
Siêu tài khoản người dùng(superuser
account)
Quá trình khởi động và thoát khỏi hệ thống Quản lý tài khoản người dùng Quản lý hệ thống tập tin Quản lý pakage Điều khiển các tiến trình
Nội dung
Tải khoản có quyền cao nhất trong hệ thống Không có hạn chế nào được áp đặt trên tài khoản này Thường dùng để duy trì và quản trị hệ thống cách thông dụng để chuyển vào tài khoản root là:
root - Superuser account
$ su Password: #
khi đang ở tài khoản root, chú ý khi dùng lệnh
# rm -rf / usr/local/oldir # rm -rf bad *
Khởi động hệ thống và khởi tạo(System
Startup and Initialization)
tập tin /etc/inittab chuyển đổi mức chạy(runlevel) các tập tin rc Chế độ đơn người dùng(Single-user mode) Thoát khỏi hệ thống(shutdown system)
Quá trình khởi động và thoát khỏi hệ thống
Booting the kernel Initializing device drivers Executing init in /sbin init reading /etc/inittab Executing rc files based on runlevel
Khởi động hệ thống và khởi tạo
Boot loader(LILO,GRUB)
Kernel
/etc/init.d/syslogd /etc/init/mysqld …. /etc/cupsd
init
Symbol link
/etc/inittab
3
/etc/rc3.d
/etc/rc3.d/[SK]*
5
/etc/rc5.d
/etc/rc5.d/[SK]*
0 1 2 3 4 5 6
File format
/etc/inittab file
code:runlevels:action:command
Runlevels
# Default runlevel. The runlevels used by RHS are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:5:initdefault:
Generally, Linux operates in runlevel 3 or 5 Edit initdefault in /etc/inittab Running init manually
Switching runlevel
# init 5 (switch to X11 mode) # init 3 (switch to multiuser mode) # init 1 (switch to single user mode)
Finding previous and current system run level
# runlevel N 5
Place in /etc/rc.d/rcN.d (N=runlevel) Shell scripts Start script: Snnxxxx Kill script: Knnxxxx nn = 00 - 99: enforce executing order
Services start/stop at boot time
S10network S12syslog S99local K90network K10xfs
S* and K* are symlinks to real scripts Central directory for real scripts: /etc/rc.d/init.d, /etc/init.d
Linux Services
There are 113 deamons, Out of them, the following are most widely
used:
apmd : Power Management crond : Periodic Command Scheduler cups : Common Unix Printing System dhcpd : The DHCP server httpd : Apache Web server iptables : Kernel based Packet Filtering firewall mysqld : MySQL server sendmail : Sendmail Mail Server squid : Squid Proxy Server sshd : Open SSH and SFTP server xinetd : Provides support for telnet, ftp, talk, tftp etc. ypbind : NIS Server
(tt)
If a startup script exists in the /etc/init.d directory
Start and stop daemon
Fedora and Redhat also come with the service
command
(tt)
Trong tình trạng khẩn cấp, phục hồi lại hệ
thống
Chế độ đơn người dùng(Single- User Mode)
No login prompt Truy cập với người dùng root Chỉ ở chế console boot: linux single boot: linux -s boot: linux 1 # init 1
shutdown - tắt hệ thống
shutdown hệ thống
# shutdown -r +10 (reboot after 10 minutes)
# /sbin/shutdown -r +10 "Rebooting to try new
kernel"
# shutdown -h now (halt immediately)
Other ways
Ctrl-Alt-Del # reboot # halt # poweroff
Người dùng và nhóm Tạo, chỉnh sửa, xóa tài khoản người dùng
/etc/passwd file /etc/shadow file
Tạo, chỉnh sửa, xóa nhóm
/etc/group file
Quản lý tài khoản
Tài khoàn người dùng và tài khoản hệ thống mỗi người dùng sẽ có 1 tên và định danh(id) duy nhất
(unsigned number)
Mỗi người dùng thuộc về ít nhất 1 nhóm Mỗi nhóm có tên và định danh duy nhất (unsigned
number)
Tên tài khoản người dùng thường là chữ thường Tài khoản siêu người dùng luôn có id là 0
Người dùng và nhóm
Thêm tài khoản người dùng
useradd [-u uid] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] name
# useradd -g students -c "Student 001" stud001
passwd - set user password
# passwd stud001
Chỉnh sửa và xoá tài khoản người dùng
usermod [-u uid] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-l new_name] name
# usermod -u 1001 stud001
userdel [-r] name
-r: remove user’s home directory
# userdel -r stud001
/etc/passwd file File format
username:password:uid:gid:gecos:homedir:shell
username - bất kỳ chuỗi ký tự nào dùng để xác định tài khoản
(human convenient)
password - mật khẩu người dùng(mã hóa) uid - user ID (system convenient) gid - group ID gecos - thông tin khác của người dùng homedir - thư mục home của người dùng shell - chương trình sẽ chạy khi người dùng login vào, thường là
/bin/bash
Tăng bảo mật cho mật khẩu: /etc/shadow
Ví dụ các mục trong /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/bin/false
student:x:500:500:Student:/home/student:/bin/bash
Tập tin /etc/shadow
/etc/shadow chứa thông tin mật khẩu đã được mã hóa cho tài khoản người dùng Login name Encrypted password
(tt)
Đặt ! trong trường password của tập tin
Khóa tài khoản người dùng
/etc/shadow
Thay đổi shell đăng nhập thành
/sbin/nologin
Tài khoản nhóm
Adding group groupadd [-g gid] group # groupadd students
Modifying group groupmod [-g gid] [-n name] group # groupmod -g 1000 students
Deleting group
groupdel group
# groupdel students
Định dạng
Tập tin /etc/group
groupname:password:gid:members
groupname - bất kỳ chuỗi ký tự nào password - tùy chọn mật khẩu gid - group ID members - danh sách người dùng, phân cách bằng dấu ,
Ví dụ các mục trong /etc/group
root:x:0:root
bin:x:1:root,bin,daemon
student:x:500:
Chỉnh sửa người sở hữu và nhóm
chown - Thay đổi người sở hữu và nhóm
# chown -R stud001:students /home/stud001
sudo command /etc/sudoers: Tập tin chứa thông tin người dùng
được phép sudo
Cú pháp của một dòng trong file sudoers như sau:
User host computer=command Defaults logfile=/var/log/sudolog
Cấp quyền root khi chạy 1 lệnh: lệnh sudo
Sudo(tt)
Username:user1 Group:%group1 user1 ALL=(ALL) ALL user1 có thể chạy bất kỳ câu lệnh nào, dưới quyền bất kỳ người dùng nào nhưng phái biết password của user1. user1 ALL=(root) shutdown
user1 chỉ có quyền chạy câu lệnh shutdown, nhưng phải biết passwrd của user1
user1 ALL=(root) NOPASSWD: /usr/bin/id
user1 chỉ có thể chạy câu lệnh /usr/bin/id và không cần password; no password will be needed.
user1 ALL=(accounts) /bin/kill, /usr/bin/kill, /usr/bin/pkill sudo -u accounts pkill monthend
Các kiểu hệ thống tệp Gắn kết hệ thống tệp Tập tin /etc/fstab Tạo ra một hệ thống tệp Kiểm tra và chỉnh sửa hệ thống tệp
Quản lý hệ thống tập tin(filesystem)
ext2 - common Linux filesystem msdos - MS-DOS FAT vfat - Windows 95/98 FAT ntfs - Windows NT/2000/XP NTFS iso9660 - CD-ROMS smbfs - SMB sharing filesystem nfs - Network File System proc - /proc filesystem (process information) ext3, reiserfs, jfs, xfs - journaling filesystems
Các kiểu hệ thống tệp
Gắn kết(Mounting)
Gắn kết hệ thống tệp
mount -t type /what /where # mount -t ext2 /dev/hda5 /mnt/backup # mount -t msdos /dev/fd0 /mnt/floppy # mount -t iso9660 /dev/hdd /mnt/cdrom # mount -t vfat /dev/hdc1 /mnt/c Hiển thị các hệ thống tệp gắn kết
# mount /dev/hda1 on / type ext3 (rw) Gỡ bỏ gắn kết(Un-mounting)
# umount /mnt/c # umount /dev/fd0
Floppy disk /dev/fd0 USB drives /dev/sdX Hard drives /dev/hdX, /dev/sdX Windows Shares //server/share NFS Shares server:/share
(tt)
Tự động gắn kết các hệ thống tệp tại thời điểm khởi
động
Dùng để gắn kết những phân vùng hay sử dụng Định dạng tập tin
Tập tin /etc/fstab
device mount-point type options
Ví dụ các mục trong /etc/fstab
Phân vùng đĩa
Định dạng hệ thống tệp
fdisk
Định dạng hệ thống tệp
mkfs -t type device blocks blocks: size of the filesystem (1024-byte blocks)
# mkfs -t ext2 /dev/fd0 1440 # mkfs -t ext3 /dev/hda6
Chú ý Định dạng hệ thống tệp trên thiết bị sẽ xóa hết dữ liệu
trên thiết bị
mkfs không thông báo trước khi định dạng
Kiểm tra sự toàn vẹn của hệ thống tệp Sửa chữa sau các sự cố hệ thống Ngoại trừ phân vùng root, umount thiết bị
trước khi kiểm tra
Kiểm tra và chỉnh sửa hệ thống tệp
fsck -t type device
# fsck -t ext3 /dev/hda2
Quản lý package
Có 2 định dạng khác nhau
RPM Package Manager (RPM) – Red hat, Suse Debian Package (DEB) – Debian, Ubuntu
Tarball
Quản lý Software
RPM - Red Hat Package Management Cài đặt gói Cập nhật gói Xóa các gói Truy vấn thông tin
Quản lý package
Các gói cài đặt trong các hệ thống red hat Thông tin về gói được lưu trữ trong cơ sở dữ
liệu
Lưu vết các phụ thuộc Dễ sử dụng Bất tiện: Giải quyết vấn đề phụ thuộc là công
RPM - Red Hat Package Management
việc khó chịu rpm [options] [packages]
Cài đặt và cập nhật gói
rpm -i|-U
# rpm -i openssh-3.1p1-3.i386.rpm
# rpm -ihv openssh-server-3.1p1-3.i386.rpm
# rpm -ihv openssh-askpass-3.1p1-3.i386.rpm \
openssh-askpass-gnome-3.1p1-3.i386.rpm
# rpm -Uhv xvnkb-0.2.4-1.i386.rpm
rpm -e
# rpm -e openssh-server-3.1p1-3
# rpm -e xvnkb
Xóa gói
Truy vấn thông tin gói
rpm -q
# rpm -ql fileutils: Liệt kê các tập tin của gói
# rpm -qip xvnkb-0.2.4-2.i386.rpm: in ra thông tin
tóm tắt của gói
# rpm –qa: Liệt kê tất cả gói
Vấn đề phụ thuộc
# rpm -ihv MagicPoint-1.09a-1.i386.rpm
error: failed dependencies: VFlib2 >= 2.25.6-4 is needed by MagicPoint-1.09a-1 libpng.so.2 is needed by MagicPoint-1.09a-1
# rpm -ihv MagicPoint-1.09a-1.i386.rpm \ VFlib2-2.25.6-4.i386.rpm \ libpng-1.0.12-2.i386.rpm
dpkg –i : install package dpkg –r : remove package dpkg –l : query package installed on your
system
Dpkg(.deb)
/etc/apt/sources.list Lệnh cài đặt
sudo apt-get install tên-gói
Lệnh tìm gói
sudo apt-cache search tham số
Lệnh cập nhật
sudo apt-get update
Quản lý gói với APT
Hiểu về tiến trình Liệt kê danh sách tiến trình đang chạy Kết thúc tiến trình Giám sát hệ thống
Điều khiển tiến trình
Tiến trình là một chương trình được nạp vào bộ nhớ để
chạy
Nhiều tiến trình cùng chạy đồng thời tại một thời điểm Mỗi tiến trình sẽ được gắn kết id duy nhất: PID (process
ID)
Tiến trình có người sở hữu(owner) và quyền giống như
tập tin
Các kiểu tiến trình Daemon processes Background processes Foreground processes
Hiểu về tiến trình
Tiến trình nền(Background processes)
Đặt ký tự & sau tên tập tin khi chạy
Cat > vidu.txt &
Ctrl+Z : Chuyển tiến trình từ tiền cảnh sang
tiến trình nền
jobs: hiển thị tất cả tiến trình nền. bg: bắt đầu tiến trình ở chế độ nền fg: chuyển tiến trình sang thành tiến trình tiền
cảnh
ps - liệt kê danh sách tiến trình
ps [options] a: select all processes on a terminal x: select processes without controlling ttys u: display user-oriented format w: wide output $ ps
PID TTY TIME CMD 728 pts/3 00:00:00 bash 1010 pts/3 00:00:00 ps
Uptime: how long server is up Free: information about memory usage
kill - Kết thúc tiến trình kill [ -s signal ] pid kill -l [ signal ]
default signal to send is signal TERM
$ kill 728 $ kill -s SIGKILL 728 $ kill -KILL 728 $ kill -9 728
killall - kết thúc tiến trình qua tên
killall [ -s signal ] name
$ killall -SIGHUP syslogd
$ killall -9 man
top - process CPU/Memory consumption Press h at anytime for list of interactive commands
Các công việc đòi hỏi tính tự động Hệ thống cron
Chạy tiến trình 1 cách tự động
Giúp hệ thống chạy ổn định hơn Quan sát hệ thống Chạy thường xuyên: hourly, daily, weekly,
yearly
Tự động chạy mà không cần sự can thiệp
của người dùng
Shell scripts
Các tác vụ tự động
crontab files: cấu hình của cron crontab commmand: câu lệnh dùng để chỉnh
sửa tập tin crontab
crond daemon: đọc tập tin crontab và thực hiện
các công việc trong đó
Hệ thống cron
Định dạng của tệp crontab
minute hour day month weekday command
Phút(0-59) Giờ (0-23) Ngày (1-31) Tháng (1-12) Ngày trong tuần (Sun=0, Mon=1, ... Sat=6) Chương trình có thể được chạy vào một ngày nào đó và
tại một thời điểm cụ thể.
0 2 * * * /bin/organize_data */15 * * * * /usr/bin/get_email 30 9-17 * 1 sun,wed,sat $HOME/bin/my_report
command is run at one min past every
hour
command is run daily at 8:17 am command is run daily at 8:17 pm command is run at 4 am every Sunday command is run 4:42 am every 1st of the
month
command is run hourly on the 19th of July
crontab command - maintain crontab files
crontab [ -u user ] file crontab [ -u user ] { -l | -r | -e } -l: display current crontab on stdout -r: remove current crontab -e: edit current crontab
$ crontab -l 5 0 * * * echo “this is a test”
/etc/crontab /etc/cron.daily/ /etc/cron.hourly/ /etc/cron.monthly/ /etc/cron.weekly/
Red Hat System wide cron