Linux硬件資源管理與外設設備使用、系統運行機制及用戶管理
Linux硬件資源管理
PCI設備
顯卡
$>>dmesg |grep -i vga
[ 0.000000] Console: colour VGA+ 80x25
[ 0.262267] vgaarb: device added: PCI:0000:00:0f.0,decodes=io+mem,owns=io+mem,locks=none
[ 0.262271] vgaarb: loaded
[ 0.262272] vgaarb: bridge control possible 0000:00:0f.0
[ 1.969021] fbcon: svgadrmfb (fb0) is primary device
網卡
$>>lscpi |grep -i eth
02:01.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
聲卡
$>>lspci |grep -i vga
00:0f.0 VGA compatible controller: VMware SVGA II Adapter
CPU信息
通過/proc文件系統查看 $>> cat /proc/cpuinfo
processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 94 model name : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz stepping : 3 microcode : 0x6a cpu MHz : 2601.000 cache size : 6144 KB physical id : 0 siblings : 1 core id : 0 cpu cores : 1 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 22 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ida arat epb pln pts dtherm hwp hwp_noitfy hwp_act_window hwp_epp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 invpcid rtm rdseed adx smap xsaveopt bogomips : 5202.00 clflush size : 64 cache_alignment : 64 address sizes : 42 bits physical, 48 bits virtual power management:
內存信息
$>>free -m 或 cat /proc/meminfo
total used free shared buff/cache available Mem: 1824 713 300 10 810 912 Swap: 2047 0 2047 total //內存總數 used //已經使用的內存數 free //空閑的內存數 shared //多個進程共享的內存總額 page cache //磁盤緩存的大小
磁盤分區信息
$>>fdisk -l //分區情況
$>>df -h //大小情況
$>>du -h //使用情況
[root@ ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000ab2c2 Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 41943039 20458496 8e Linux LVM Disk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes [root@ ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 18G 4.2G 14G 24% / devtmpfs 898M 0 898M 0% /dev tmpfs 913M 100K 913M 1% /dev/shm tmpfs 913M 9.0M 904M 1% /run tmpfs 913M 0 913M 0% /sys/fs/cgroup /dev/sda1 497M 157M 341M 32% /boot tmpfs 183M 20K 183M 1% /run/user/1000 tmpfs 183M 0 183M 0% /run/user/0
外部設備使用
在Linux系統中硬件設備都以文件的形式存在;不同的硬件設備具有不同的文件類型;設備文件在/dev目錄下
[root@ ~]# cd /dev/ [root@ dev]# ls agpgart crash initctl mqueue rfkill stdin tty18 tty3 tty41 tty53 tty8 vcs1 vfio autofs disk input net rtc stdout tty19 tty30 tty42 tty54 tty9 vcs2 vga_arbiter block dm-0 kmsg network_latency rtc0 tty tty2 tty31 tty43 tty55 ttyS0 vcs3 vhost-net bsg dm-1 log network_throughput sda tty0 tty20 tty32 tty44 tty56 ttyS1 vcs4 vmci btrfs-control dmmidi loop-control null sda1 tty1 tty21 tty33 tty45 tty57 ttyS2 vcs5 vsock bus dri lp0 nvram sda2 tty10 tty22 tty34 tty46 tty58 ttyS3 vcs6 zero cdrom fb0 lp1 oldmem sg0 tty11 tty23 tty35 tty47 tty59 uhid vcsa centos fd lp2 port sg1 tty12 tty24 tty36 tty48 tty6 uinput vcsa1 char full lp3 ppp shm tty13 tty25 tty37 tty49 tty60 urandom vcsa2 console fuse mapper ptmx snapshot tty14 tty26 tty38 tty5 tty61 usbmon0 vcsa3 core hidraw0 mcelog pts snd tty15 tty27 tty39 tty50 tty62 usbmon1 vcsa4 cpu hpet mem random sr0 tty16 tty28 tty4 tty51 tty63 usbmon2 vcsa5 cpu_dma_latency hugepages midi raw stderr tty17 tty29 tty40 tty52 tty7 vcs vcsa6
系統運行機制
系統啟動過程
開機自檢->MBR引導->GRUB菜單->加載內核->init進程初始化->登錄系統
系統運行級別
$>>cat /etc/inittab
# 0 - halt (Do NOT set initdefault to this) //關機 # 1 - Single user mode //單用戶模式 # 2 - Multiuser, without NFS (The same as 3, if you do not have networking)//無網絡支持的多用戶模式 # 3 - Full multiuser mode //有網絡支持的多用戶模式 # 4 - unused //保留,未使用 # 5 - X11 //有網絡支持的X-Windows支持的多用戶模式 # 6 - reboot (Do NOT set initdefault to this) //重新引導,重啟 # id:5:initdefault: eg:切換到第3運行級別 $>>systemctl isolate multi-user.target 或 systemctl isolate runlevel3.target eg:切換到第5運行級別 $>>systemctl isolate graphical.target 或 systemctl isolate runlevel5.target eg:設置默認第三啟動級別 $>>systemctl set-default multi-user.target $>>systemctl get-default //查看當前運行啟動級別 eg:設置默認第五啟動級別 $>>systemctl set-default graphical.target
系統用戶管理
用戶賬號分類
用戶賬號密碼配置文件
/etc/passwd 每個字段的作用
root:x:0:0:root:/root:/bin/bash
用戶名:密碼占位符:UID:GID:用戶描述:用戶主目錄:登錄後使用的shell
系統中的偽用戶
Linux中任何一個命令的操作都必須有一個用戶的身份。偽用戶一般和系統或者程序服務相關
bin,daemon,shutdown,halt,linux默認都有這些偽用戶,偽用戶通常不需要或無法登陸系統,可以沒有宿主目錄
eg:/etc/passwd文件中常見的偽用戶Nobody
添加系統用戶
[root@ dev]# useradd -d, --home-dir HOME_DIR home directory of the new account //目錄,創建用戶時指定用戶主目錄 -g, --gid GROUP name or ID of the primary group of the new account//用戶組,指定用戶所屬組 -G, --groups GROUPS list of supplementary groups of the new account//用戶組,指定用戶所屬的附加組 faillog databases -m, --create-home create the user‘s home directory //不創建用戶宿主目錄 -s, --shell SHELL login shell of the new account //指定用戶的登錄shell -u, --uid UID user ID of the new account //指定用戶號 eg:創建一個用戶名稱為sam,指定宿主目錄為/opt/sam,登錄的shell為/bin/sh. [root@ dev]# useradd -d /opt/sam -s /bin/sh sam [root@ dev]# tail -1 /etc/passwd sam:x:1001:1001::/opt/sam:/bin/sh eg:創建一個用戶名為test,UID為2010,指定起始組為root,附加組為ftp,登錄shell為/bin/sh [root@ dev]# useradd -u 2010 -g root -G ftp -s /bin/sh test [root@ dev]# tail -1 /etc/passwd test:x:2010:0::/home/test:/bin/sh 註:當所創建用戶無法正常顯示時!將/etc/skel/下的所有文件拷貝到新建用戶的宿主目錄下 [root@ skel]# cd /etc/skel/ [root@ skel]# ls [root@ skel]# ls -a . .. .bash_logout .bash_profile .bashrc .mozilla [root@ skel]# ls -a /opt/sam/ . .. .bash_logout .bash_profile .bashrc .mozilla
刪除用戶
如果一個用戶的賬號不再使用,可以從系統中刪除,刪除用戶賬號就是要/etc/passwd等系統文件中的該用戶記錄刪除,必要時刪除用戶宿主目錄
[root@ skel]# userdel -r, --remove remove home directory and mail spool //把用戶的主目錄一起刪除 eg:刪除用戶sam [root@ skel]# userdel sam [root@ skel]# tail -1 /etc/passwd test:x:2010:0::/home/test:/bin/sh [root@ skel]# ls /opt/ //不加-r ,其主目錄還是存在 rh sam eg:刪除用戶test,並刪除宿主目錄 [root@ skel]# userdel -r test [root@ skel]# tail -1 /etc/passwd enmoedu:x:1000:1000:enmoedu:/home/enmoedu:/bin/bash [root@ skel]# ls /home/ enmoedu
修改用戶信息
根據實際情況更改用戶的相關屬性,如用戶號、主目錄、用戶組等,跟useradd差不多
[root@ skel]# usermod -d, --home HOME_DIR new home directory for the user account -g, --gid GROUP force use GROUP as new primary group -G, --groups GROUPS new list of supplementary GROUPS -a, --append append the user to the supplemental GROUPS -s, --shell SHELL new login shell for the user account -u, --uid UID new UID for the user account
用戶密碼管理
eg:交互式配置用戶密碼 [root@ skel]# useradd sam Creating mailbox file: File exists [root@ skel]# passwd sam Changing password for user sam. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. eg:無交互式配置用戶密碼 [root@ skel]# echo 123456 | passwd --stdin sam Changing password for user sam. passwd: all authentication tokens updated successfully.
密碼配置文件
/etc/shadow [root@ skel]# cat /etc/shadow|grep root root:$6$hvU.3oadtx9DHSDw $Qq27h05p4y2KlIa5BTeu0y7U3plXmPQJqBCQWwvsi1E6hJ27JflijHHnLTH/HyDgacxip76PoAFJ.G7mKyLRj/::0:99999:7:::
Linux硬件資源管理與外設設備使用、系統運行機制及用戶管理