syslog是Linux系統默認的日志守護進(jìn)程。syslog 接受來(lái)自系統的各種功能的信息,每個(gè)信息都包括重要級。/etc/syslog.conf
文件通知 syslogd 如何根據設備和信息重要級別來(lái)報告信息??梢允褂胠ogger命令通過(guò)syslogd記錄日志。
要向syslog文件 /var/log/messages
中記錄日志信息:
logger this is a test log line
輸出:
tail -n 1 messagesJan 5 10:07:03 localhost root: this is a test log line
記錄特定的標記(tag)可以使用:
logger -t TAG this is a test log line
輸出:
tail -n 1 messagesJan 5 10:37:14 localhost TAG: this is a test log line
systemctl命令是系統服務(wù)管理器指令,它實(shí)際上將 service 和 chkconfig 這兩個(gè)命令組合到一起。
任務(wù) | 舊指令 | 新指令 |
---|---|---|
使某服務(wù)自動(dòng)啟動(dòng) | chkconfig —level 3 httpd on | systemctl enable httpd.service |
使某服務(wù)不自動(dòng)啟動(dòng) | chkconfig —level 3 httpd off | systemctl disable httpd.service |
檢查服務(wù)狀態(tài) | service httpd status | systemctl status httpd.service (服務(wù)詳細信息) systemctl is-active httpd.service (僅顯示是否 Active) |
顯示所有已啟動(dòng)的服務(wù) | chkconfig —list | systemctl list-units —type=service |
啟動(dòng)某服務(wù) | service httpd start | systemctl start httpd.service |
停止某服務(wù) | service httpd stop | systemctl stop httpd.service |
重啟某服務(wù) | service httpd restart | systemctl restart httpd.service |
啟動(dòng)nfs服務(wù)
systemctl start nfs-server.service
設置開(kāi)機自啟動(dòng)
systemctl enable nfs-server.service
停止開(kāi)機自啟動(dòng)
systemctl disable nfs-server.service
查看服務(wù)當前狀態(tài)
systemctl status nfs-server.service
重新啟動(dòng)某服務(wù)
systemctl restart nfs-server.service
查看所有已啟動(dòng)的服務(wù)
systemctl list -units --type=service
開(kāi)啟防火墻22端口
iptables -I INPUT -p tcp --dport 22 -j accept
關(guān)閉SElinux:
運行命令getenforce
,驗證SELinux狀態(tài)。返回結果若是enforcing
,表明SELinux已開(kāi)啟。
setenforce 0
vi /etc/selinux/config
SELINUX=enforcing
,按i鍵進(jìn)入編輯模式,將參數修改為SELINUX=disabled
。:wq
,保存并退出文件。
shutdown -r now
getenforce
,驗證SELinux狀態(tài)為disabled
,表明SELinux已關(guān)閉。徹底關(guān)閉防火墻:
sudo systemctl status firewalld.servicesudo systemctl stop firewalld.servicesudo systemctl disable firewalld.service
crontab命令被用來(lái)提交和管理用戶(hù)的需要周期性執行的任務(wù),當安裝完成操作系統后,默認會(huì )安裝此服務(wù)工具,并且會(huì )自動(dòng)啟動(dòng)crond進(jìn)程,crond進(jìn)程每分鐘會(huì )定期檢查是否有要執行的任務(wù),如果有要執行的任務(wù),則自動(dòng)執行。
crontab (選項) (參數)
-e:編輯該用戶(hù)的計時(shí)器設置;
-l:列出該用戶(hù)的計時(shí)器設置;
-r:刪除該用戶(hù)的計時(shí)器設置;
-u<用戶(hù)名稱(chēng)>:指定要設定計時(shí)器的用戶(hù)名稱(chēng)。
crontab文件:指定包含待執行任務(wù)的crontab文件。
mount命令用于加載文件系統到指定的加載點(diǎn)。此命令的最常用于掛載cdrom,使我們可訪(fǎng)問(wèn)cdrom中的數據,因為將光盤(pán)插入cdrom中,Linux并不會(huì )自動(dòng)掛載,必須使用Linux mount命令來(lái)手動(dòng)完成掛載。
mount (選項) (參數)
-V:顯示程序版本;
-l:顯示已加載的文件系統列表;
-h:顯示幫助信息并退出;
-v:冗長(cháng)模式,輸出指令執行的詳細信息;
-n:加載沒(méi)有寫(xiě)入文件“/etc/mtab”中的文件系統;
-r:將文件系統加載為只讀模式;
-a:加載文件“/etc/fstab”中描述的所有文件系統。
設備文件名:指定要加載的文件系統對應的設備名
加載點(diǎn):指定加載點(diǎn)目錄。
mount -t auto /dev/cdrom /mnt/cdrom
mount: mount point /mnt/cdrom does not exist /mnt/cdrom目錄不存在,需要先創(chuàng )建。
cd /mnt
-bash: cd: /mnt: No such file or directory
mkdir -p /mnt/cdrom 創(chuàng )建/mnt/cdrom目錄
ls
bin dev home lib media mnt proc sbin srv tmp
var
boot etc initrd lost+found misc opt root selinux sys usr
mount -t auto /dev/cdrom /mnt/cdrom 掛載cdrom
mount: block device /dev/cdrom is write-protected, mounting read-only 掛載成功
ll /mnt/cdrom 查看cdrom里面內容
total 859
dr-xr-xr-x 4 root root 2048 Sep 4 2005 CentOS
-r--r--r-- 2 root root 8859 Mar 19 2005 centosdocs-man.css
-r--r--r-- 9 root root 18009 Mar 1 2005 GPL
dr-xr-xr-x 2 root root 241664 May 7 02:32 headers
dr-xr-xr-x 4 root root 2048 May 7 02:23 images
dr-xr-xr-x 2 root root 4096 May 7 02:23 isolinux
dr-xr-xr-x 2 root root 18432 May 2 18:50 NOTES
-r--r--r-- 2 root root 5443 May 7 01:49 RELEASE-NOTES-en.html
dr-xr-xr-x 2 root root 2048 May 7 02:34 repodata
-r--r--r-- 9 root root 1795 Mar 1 2005 rpm-GPG-KEY
-r--r--r-- 2 root root 1795 Mar 1 2005 RPM-GPG-KEY-centos4
-r--r--r-- 1 root root 571730 May 7 01:39 yumgroups.xml
fsck命令被用于檢查并且試圖修復文件系統中的錯誤。當文件系統發(fā)生錯誤時(shí),可用fsck指令嘗試加以修復。
fsck (選項) (參數)
-a:自動(dòng)修復文件系統,不詢(xún)問(wèn)任何問(wèn)題;
-A:依照/etc/fstab配置文件的內容,檢查文件內所列的全部文件系統;
-N:不執行指令,僅列出實(shí)際執行會(huì )進(jìn)行的動(dòng)作;
-P:當搭配”-A”參數使用時(shí),則會(huì )同時(shí)檢查所有的文件系統;
-r:采用互動(dòng)模式,在執行修復時(shí)詢(xún)問(wèn)問(wèn)題,讓用戶(hù)得以確認并決定處理方式;
-R:當搭配”-A”參數使用時(shí),則會(huì )略過(guò)/目錄的文件系統不予檢查;
-s:依序執行檢查作業(yè),而非同時(shí)執行;
-t<文件系統類(lèi)型>:指定要檢查的文件系統類(lèi)型;
-T:執行fsck指令時(shí),不顯示標題信息;
-V:顯示指令執行過(guò)程。
文件系統:指定要查看信息的文件系統。
Linux的文件系統損壞會(huì )導致不正常關(guān)機,出錯時(shí)若系統告知某塊硬盤(pán)的分區有問(wèn)題,比如/dev/hda2,可用如下命令處理:
fsck -y /dev/hda2
結束后使用reboot命令重啟系統。
若不知何處出問(wèn)題,可以直接輸入命令:
fsck
在隨后的多個(gè)確認對話(huà)框中輸入:y
結束后同樣使用reboot命令重啟系統。
ulimit命令用來(lái)限制系統用戶(hù)對shell資源的訪(fǎng)問(wèn),它是一種簡(jiǎn)單并且有效的實(shí)現資源限制的方式。
作為臨時(shí)限制,ulimit 可作用于使用命令登錄的 shell 會(huì )話(huà),在會(huì )話(huà)終止時(shí)便結束限制。對于長(cháng)期的固定限制,ulimit 命令語(yǔ)句可被添加到由登錄 shell 讀取的文件中,作用于特定的 shell 用戶(hù)。
ulimit (選項)
-a:顯示目前資源限制的設定;
-c
-d <數據節區大小>:程序數據節區的最大值,單位為KB;
-f <文件大小>:shell所能建立的最大文件,單位為區塊;
-H:設定資源的硬性限制,也就是管理員所設下的限制;
-m <內存大小>:指定可使用內存的上限,單位為KB;
-n <文件數目>:指定同一時(shí)間最多可開(kāi)啟的文件數;
-p <緩沖區大小>:指定管道緩沖區的大小,單位512字節;
-s <堆疊大小>:指定堆疊的上限,單位為KB;
-S:設定資源的彈性限制;
-t
-u <程序數目>:用戶(hù)最多可開(kāi)啟的程序數目;
-v <虛擬內存大小>:指定可使用的虛擬內存上限,單位為KB。
[root@localhost ~]# ulimit -a
core file size (blocks, -c) 0 #core文件的最大值為100 blocks。
data seg size (kbytes, -d) unlimited #進(jìn)程的數據段可以任意大。
scheduling priority (-e) 0
file size (blocks, -f) unlimited #文件可以任意大。
pending signals (-i) 98304 #最多有98304個(gè)待處理的信號。
max locked memory (kbytes, -l) 32 #一個(gè)任務(wù)鎖住的物理內存的最大值為32KB。
max memory size (kbytes, -m) unlimited #一個(gè)任務(wù)的常駐物理內存的最大值。
open files (-n) 1024 #一個(gè)任務(wù)最多可以同時(shí)打開(kāi)1024的文件。
pipe size (512 bytes, -p) 8 #管道的最大空間為4096字節。
POSIX message queues (bytes, -q) 819200 #POSIX的消息隊列的最大值為819200字節。
real-time priority (-r) 0
stack size (kbytes, -s) 10240 #進(jìn)程的棧的最大值為10240字節。
cpu time (seconds, -t) unlimited #進(jìn)程使用的CPU時(shí)間。
max user processes (-u) 98304 #當前用戶(hù)同時(shí)打開(kāi)的進(jìn)程(包括線(xiàn)程)的最大個(gè)數為98304。
virtual memory (kbytes, -v) unlimited #沒(méi)有限制進(jìn)程的最大地址空間。
file locks (-x) unlimited #所能鎖住的文件的最大個(gè)數沒(méi)有限制。
sysctl命令用于在內核運行時(shí)動(dòng)態(tài)修改內核的運行參數,可用的內核參數在目錄/proc/sys中。它包含一些TCP/IP堆棧和虛擬內存系統的高級選項,用sysctl可以讀取設置超過(guò)五百個(gè)系統變量。
sysctl (選項) (參數)
-n:打印值時(shí)不打印關(guān)鍵字;
-e:忽略未知關(guān)鍵字錯誤;
-N:僅打印名稱(chēng);
-w:當改變sysctl設置時(shí)使用此項;
-p:從配置文件“/etc/sysctl.conf”加載內核參數設置;
-a:打印當前所有可用的內核參數變量和值;
-A:以表格方式打印當前所有可用的內核參數變量和值。
變量=值:設置內核參數對應的變量值
sysctl -a 讀一個(gè)指定的變量,例如kern.maxproc:
sysctl kern.maxproc kern.maxproc: 1044
要設置一個(gè)指定的變量,直接用variable=value這樣的語(yǔ)法:
sysctl kern.maxfiles=5000kern.maxfiles: 2088 -> 5000
可用sysctl修改系統變量,也可通過(guò)編輯sysctl.conf文件來(lái)修改系統變量。它用variable=value的形式來(lái)設定值。sysctl變量的設置通常是字符串、數字或者布爾型(用 1 來(lái)表示’yes’,用 0 來(lái)表示’no’)。
sysctl -w kernel.sysrq=0
sysctl -w kernel.core_uses_pid=1
sysctl -w net.ipv4.conf.default.accept_redirects=0
sysctl -w net.ipv4.conf.default.accept_source_route=0
sysctl -w net.ipv4.conf.default.rp_filter=1
sysctl -w net.ipv4.tcp_syncookies=1
sysctl -w net.ipv4.tcp_max_syn_backlog=2048
sysctl -w net.ipv4.tcp_fin_timeout=30
sysctl -w net.ipv4.tcp_synack_retries=2
sysctl -w net.ipv4.tcp_keepalive_time=3600
sysctl -w net.ipv4.tcp_window_scaling=1
sysctl -w net.ipv4.tcp_sack=1
編輯此文件:/etc/sysctl.conf
如果該文件為空,則輸入以下內容,請根據情況自己做調整:
# Controls source route verification
# Default should work for all interfaces
net.ipv4.conf.default.rp_filter = 1
# net.ipv4.conf.all.rp_filter = 1
# net.ipv4.conf.lo.rp_filter = 1
# net.ipv4.conf.eth0.rp_filter = 1
# Disables IP source routing
# Default should work for all interfaces
net.ipv4.conf.default.accept_source_route = 0
# net.ipv4.conf.all.accept_source_route = 0
# net.ipv4.conf.lo.accept_source_route = 0
# net.ipv4.conf.eth0.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
# Increase maximum amount of memory allocated to shm
# Only uncomment if needed!
# kernel.shmmax = 67108864
# Disable ICMP Redirect Acceptance
# Default should work for all interfaces
net.ipv4.conf.default.accept_redirects = 0
# net.ipv4.conf.all.accept_redirects = 0
# net.ipv4.conf.lo.accept_redirects = 0
# net.ipv4.conf.eth0.accept_redirects = 0
# enable Log Spoofed Packets, Source Routed Packets, Redirect Packets
# Default should work for all interfaces
net.ipv4.conf.default.log_martians = 1
# net.ipv4.conf.all.log_martians = 1
# net.ipv4.conf.lo.log_martians = 1
# net.ipv4.conf.eth0.log_martians = 1
# Decrease the time default value for tcp_fin_timeout connection
net.ipv4.tcp_fin_timeout = 25
# Decrease the time default value for tcp_keepalive_time connection
net.ipv4.tcp_keepalive_time = 1200
# Turn on the tcp_window_scaling
net.ipv4.tcp_window_scaling = 1
# Turn on the tcp_sack
net.ipv4.tcp_sack = 1
# tcp_fack should be on because of sack
net.ipv4.tcp_fack = 1
# Turn on the tcp_timestamps
net.ipv4.tcp_timestamps = 1
# Enable TCP SYN Cookie Protection
net.ipv4.tcp_syncookies = 1
# Enable ignoring broadcasts request
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Enable bad error message Protection
net.ipv4.icmp_ignore_bogus_error_responses = 1
# make more local ports available
# net.ipv4.ip_local_port_range = 1024 65000
# set TCP Re-Ordering value in kernel to ‘5′
net.ipv4.tcp_reordering = 5
# Lower syn retry rates
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 3
# Set Max SYN Backlog to ‘2048′
net.ipv4.tcp_max_syn_backlog = 2048
# Various Settings
net.core.netdev_max_backlog = 1024
# Increase the maximum number of skb-heads to be cached
net.core.hot_list_length = 256
# Increase the tcp-time-wait buckets pool size
net.ipv4.tcp_max_tw_buckets = 360000
# This will increase the amount of memory available for socket input/output queues
net.core.rmem_default = 65535
net.core.rmem_max = 8388608
net.ipv4.tcp_rmem = 4096 87380 8388608
net.core.wmem_default = 65535
net.core.wmem_max = 8388608
net.ipv4.tcp_wmem = 4096 65535 8388608
net.ipv4.tcp_mem = 8388608 8388608 8388608
net.core.optmem_max = 40960
這個(gè)文件描述系統中各種文件系統的信息。文件 /etc/fstab 包含了靜態(tài)文件系統信息,定義了存儲設備和分區整合到整個(gè)系統的方式。mount 命令會(huì )讀取這個(gè)文件,確定設備和分區的掛載選項。
<file system> <dir> <type> <options> <dump> <pass>
通過(guò)空格或 Tab 分隔。<file systems>
- 要掛載的分區或存儲設備.<dir>
- <file systems>
的掛載位置。<type>
- 要掛載設備或是分區的文件系統類(lèi)型,支持許多種文件系統<options>
- 某些特定文件系統掛載時(shí)使用的參數。一些比較常用的有:
auto - 在啟動(dòng)時(shí)或鍵入了 mount -a
命令時(shí)自動(dòng)掛載。
noauto - 只在您的命令下被掛載。
exec - 允許執行此分區的二進(jìn)制文件。
noexec - 不允許執行此文件系統上的二進(jìn)制文件。
ro - 以只讀模式掛載文件系統。
rw - 以讀寫(xiě)模式掛載文件系統。
user - 允許任意用戶(hù)掛載此文件系統,若無(wú)顯示定義,隱含啟用 noexec, nosuid, nodev 參數。
users - 允許所有 users 組中的用戶(hù)掛載文件系統.
nouser - 只能被 root 掛載。
owner - 允許設備所有者掛載.
sync - I/O 同步進(jìn)行。
async - I/O 異步進(jìn)行。
dev - 解析文件系統上的塊特殊設備。
nodev - 不解析文件系統上的塊特殊設備。
suid - 允許 suid 操作和設定 sgid 位。
nosuid - 禁止 suid 操作和設定 sgid 位。
noatime - 不更新文件系統上 inode 訪(fǎng)問(wèn)記錄,可以提升性能(參見(jiàn) atime 參數)。
nodiratime - 不更新文件系統上的目錄 inode 訪(fǎng)問(wèn)記錄,可以提升性能(參見(jiàn) atime 參數)。
relatime - 實(shí)時(shí)更新 inode access 記錄。只有在記錄中的訪(fǎng)問(wèn)時(shí)間早于當前訪(fǎng)問(wèn)才會(huì )被更新。
flush - vfat 的選項,更頻繁的刷新數據,復制對話(huà)框或進(jìn)度條在全部數據都寫(xiě)入后才消失。
defaults - 使用文件系統的默認掛載參數,例如 ext4 的默認參數為:rw, suid, dev, exec, auto, nouser, async.
<dump>
- dump 工具通過(guò)它決定何時(shí)作備份. dump 會(huì )檢查其內容,并用數字來(lái)決定是否對該文件系統進(jìn)行備份。 允許的數字是 0 和 1 。0 表示忽略, 1 則進(jìn)行備份。
<pass>
- fsck 讀取 <pass>
的數值來(lái)決定需要檢查的文件系統的檢查順序。允許的數字是0,1,和2。 根目錄應當獲得最高的優(yōu)先權 1, 其它所有需要被檢查的設備設置為 2,0 表示設備不會(huì )被 fsck 檢查。
使用內核名稱(chēng)標識磁盤(pán):
/etc/fstab
# <file system> <dir> <type> <options> <dump> <pass>
tmpfs /tmp tmpfs nodev,nosuid 0 0
/dev/sda1 / ext4 defaults,noatime 0 1
/dev/sda2 none swap defaults 0 0
/dev/sda3 /home ext4 defaults,noatime 0 2
echo 1>/sys/module/rcupdate/parameters/rcu_cpu_stall_suppress
這個(gè)參數在 rc.local 文件中,解決云主機空閑時(shí)頻繁刷日志導致業(yè)務(wù)卡頓的問(wèn)題。
若未加該參數,隔幾秒便會(huì )打印內核日志。
CentOS 6.6 系統安裝 x-window 后 networkmanager 服務(wù)自動(dòng)啟動(dòng),會(huì )導致云主機重啟后無(wú)法獲取 IP 地址和 DNS 地址,可通過(guò)關(guān)閉該服務(wù)解決此問(wèn)題。
通過(guò)控制臺VNC連接主機:
臨時(shí)關(guān)閉:
service NetworkManager stop
永久關(guān)閉:
chkconfig NetworkManager off
若 /home 分區較大,可讓不依賴(lài) /home 分區的服務(wù)先啟動(dòng),把下面的參數添加到 /etc/fstab 文件中 /home 項目的參數部分。
noauto,x-systemd.automount
這樣 /home 分區只有需要訪(fǎng)問(wèn)時(shí)才會(huì )被掛載。內核會(huì )緩存所有的文件操作,直到 /home 分區準備完成。這樣會(huì )使 /home 的文件系統類(lèi)型被識別為 autofs, mlocate 查詢(xún)時(shí)忽略該目錄。
掛載遠程文件系統同理。另外,可以設置 x-systemd.device-timeout=#
參數,設置超時(shí)時(shí)間,以防止網(wǎng)絡(luò )資源不能訪(fǎng)問(wèn)的時(shí)候浪費時(shí)間。 如果您的加密文件系統需要密鑰,則需要添加 noauto 參數到 /etc/crypttab 文件中的對應位置。
/etc/crypttabdata /dev/md0 /root/key noauto
如果交換分區沒(méi)有 UUID,可手動(dòng)加入。
如果使用 lsblk -f
命令沒(méi)有列出交換分區的 UUID 就說(shuō)明發(fā)生了這種情況。為交換分區指定 UUID 的步驟:
# swapon -s 確定交換分區
# swapoff /dev/sda7 禁用交換分區
# mkswap -U random /dev/sda7 用新 UUID 重新創(chuàng )建交換分區
# swapon /dev/sda7 激活交換分區`
如果掛載的路徑中有空格,可以使用 “\040” 轉義字符來(lái)表示空格(以三位八進(jìn)制數來(lái)進(jìn)行表示)。
/etc/fstab
UUID=47FA-4071 /home//Camera\040Pictures vfat defaults,noatime 0 2
/dev/sda7 /media/100\040GB\040(Storage) ext4 defaults,noatime,user 0 0
外部設備在插入時(shí)掛載,在未插入時(shí)忽略。這需要 nofail ,啟動(dòng)時(shí)設備不存在直接忽略它、不報錯.
/etc/fstab /dev/sdg1 /media/backup jfs defaults,nofail 0 2
FAT32必須修改 /etc/fstab
文件才能取得對 FAT32 分區的寫(xiě)權限。
/etc/fstab/dev/sdxY /mnt/some_folder vfat user,rw,umask=000 0 0
比如您的 FAT32 分區在 /dev/sda9,想將其掛載到 /mnt/fat32,可輸入并運行如下命令:
/etc/fstab/dev/sda9 /mnt/fat32 vfat user,rw,umask=111,dmask=000 0 0