最近更新时间:2026-08-18 21:09:45
syslog 是 Linux 系统默认的日志守护进程,接受来自系统各种功能的信息,每条信息都包含重要级别。/etc/syslog.conf 文件通知 syslogd 如何根据设备和信息重要级别来报告信息。可以使用 logger 命令通过 syslogd 记录日志。
向 syslog 文件 /var/log/messages 中记录日志信息:
logger this is a test log line
输出:
```bash
tail -n 1 messages
Jan 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 messages
Jan 5 10:37:14 localhost TAG: this is a test log linesystemctl 命令是系统服务管理器指令,将 service 和 chkconfig 两个命令组合到一起。
任务 | 旧指令 | 新指令 |
|---|---|---|
使某服务自动启动 |
|
|
使某服务不自动启动 |
|
|
检查服务状态 |
|
|
显示所有已启动的服务 |
|
|
启动某服务 |
|
|
停止某服务 |
|
|
重启某服务 |
|
|
启动 nfs 服务
systemctl start nfs-server.service设置开机自启动
systemctl enable nfs-server.service停止开机自启动
systemctl disable nfs-server.service查看服务当前状态
systemctl status nfs-server.service重新启动某服务
systemctl restart nfs-server.service查看所有已启动的服务
systemctl list-units --type=serviceiptables -I INPUT -p tcp --dport 22 -j ACCEPT修改 /etc/selinux/config 文件中的 SELINUX= 为 disabled,然后重启。
sudo systemctl status firewalld.service
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.servicecrontab 命令用于提交和管理用户需要周期性执行的任务。安装完成操作系统后,默认会安装此服务工具并自动启动 crond 进程,crond 进程每分钟会定期检查是否有要执行的任务并自动执行。
crontab (选项) (参数)-e:编辑该用户的计时器设置
-l:列出该用户的计时器设置
-r:删除该用户的计时器设置
-u <用户名称>:指定要设定计时器的用户名称
crontab文件:指定包含待执行任务的 crontab 文件
mount 命令用于加载文件系统到指定的加载点。此命令常用于挂载 cdrom,使我们可以访问 cdrom 中的数据。将光盘插入 cdrom 后,Linux 不会自动挂载,必须使用 mount 命令手动完成挂载。
mount (选项) (参数)-V:显示程序版本
-l:显示已加载的文件系统列表
-h:显示帮助信息并退出
-v:冗长模式,输出指令执行的详细信息
-n:加载没有写入文件 /etc/mtab 中的文件系统
-r:将文件系统加载为只读模式
-a:加载文件 /etc/fstab 中描述的所有文件系统
设备文件名:指定要加载的文件系统对应的设备名
加载点:指定加载点目录
mount -t auto /dev/cdrom /mnt/cdrom
mount: mount point /mnt/cdrom does not exist /mnt/cdrom目录不存在,需要先创建。
cd /mnt
-bash: cd: /mnt: No such file or directory
mkdir -p /mnt/cdrom 创建/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.xmlfsck命令用于检查并且试图修复文件系统中的错误。当文件系统发生错误时,可用fsck指令尝试加以修复。
fsck(选项)(参数)-y:自动修复所有发现的问题
-a:自动修复文件系统,不询问任何问题
-A:依照/etc/fstab配置文件的内容,检查文件内所列的全部文件系统
-N:不执行指令,仅列出实际执行会进行的动作
-P:当搭配“-A”参数使用时,则会同时检查所有的文件系统
-r:采用互动模式,在执行修复时询问问题,让用户得以确认并决定处理方式
-R:当搭配“-A”参数使用时,则会略过/目录的文件系统不予检查
-s:依序执行检查作业,而非同时执行
-t <文件系统类型>:指定要检查的文件系统类型
-T:执行fsck指令时,不显示标题信息
-V:显示指令执行过程
文件系统:指定要查看信息的文件系统
linux的文件系统损坏会导致linux不正常关机,出错的时候如果系统告诉您是哪一块硬盘的分区有问题,比如是/dev/hda2,接着用如下的命令去处理它:
fsck -y /dev/hda2结束后使用reboot命令重启系统,如果不知道是哪个地方出了问题,可以直接:
fsck在随后的多个确认对话框中输入y。结束后同样使用reboot命令重启系统。
ulimit命令用来限制系统用户对shell资源的访问,它是一种简单并且有效的实现资源限制的方式。作为临时限制,ulimit可作用于通过使用命令登录的shell会话,在会话终止时便结束限制。对于长期的固定限制,ulimit命令语句可被添加到由登录shell读取的文件中,作用于特定的shell用户。
ulimit(选项)-a:显示目前资源限制的设��
-c <core文件上限>:设定core文件的最大值,单位为区块
-d <数据节区大小>:程序数据节区的最大值,单位为KB
-f <文件大小>:shell所能建立的最大文件,单位为区块
-H:设定资源的硬性限制,也就是管理员所设下的限制
-m <内存大小>:指定可使用内存的上限,单位为KB
-n <文件数目>:指定同一时间最多可开启的文件数
-p <缓冲区大小>:指定管道缓冲区的大小,单位512字节
-s <堆叠大小>:指定堆叠的上限,单位为KB
-S:设定资源的弹性限制
-t <CPU时间>:指定CPU使用时间的上限,单位为秒
-u <程序数目>:用户最多可开启的程序数目
-v <虚拟内存大小>:指定可使用的虚拟内存上限,单位为KB
[root@localhost ~]# ulimit -a
core file size (blocks, -c) 0 #core文件的最大值为100 blocks。
data seg size (kbytes, -d) unlimited #进程的数据段可以任意大。
scheduling priority (-e) 0
file size (blocks, -f) unlimited #文件可以任意大。
pending signals (-i) 98304 #最多有98304个待处理的信号。
max locked memory (kbytes, -l) 32 #一个任务锁住的物理内存的最大值为32KB。
max memory size (kbytes, -m) unlimited #一个任务的常驻物理内存的最大值。
open files (-n) 1024 #一个任务最多可以同时打开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 #进程的栈的最大值为10240字节。
cpu time (seconds, -t) unlimited #进程使用的CPU时间。
max user processes (-u) 98304 #当前用户同时打开的进程(包括线程)的最大个数为98304。
virtual memory (kbytes, -v) unlimited #没有限制进程的最大地址空间。
file locks (-x) unlimited #所能锁住的文件的最大个数没有限制。sysctl命令用于在内核运行时动态地修改内核的运行参数,可用的内核参数在目录/proc/sys中。它包含一些TCP/IP堆栈和虚拟内存系统的高级选项,用sysctl可以读取设置超过五百个系统变量。
sysctl(选项)(参数)-n:打印值时不打印关键字
-e:忽略未知关键字错误
-N:仅打印名称
-w:当改变sysctl设置时使用此项
-p:从配置文件/etc/sysctl.conf加载内核参数设置
-a:打印当前所有可用的内核参数变量和值
-A:以表格方式打印当前所有可用的内核参数变量和值
变量=值:设置内核参数对应的变量值
sysctl -a打印当前所有可用的内核参数变量和值,例如查看kern.maxproc:
sysctl kern.maxproc
kern.maxproc: 1044要设置一个指定的变量,直接用variable=value这样的语法:
sysctl kern.maxfiles=5000
kern.maxfiles: 2088 -> 5000可用sysctl修改系统变量,也可通过编辑sysctl.conf文件来修改系统变量。它用variable=value的形式来设定值。sysctl变量的设置通常是字符串、数字或者布尔型。(布尔型用1来表示’yes’,用0来表示’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该文件描述系统中各种文件系统的信息。文件 /etc/fstab 包含了静态文件系统信息,定义了存储设备和分区整合到整个系统的方式。mount 命令会读取这个文件,确定设备和分区的挂载选项。
<file system> <dir> <type> <options> <dump> <pass>通过空格或 Tab 分隔。
<file systems>:要挂载的分区或存储设备。
<dir>:<file systems> 的挂载位置。
<type>:要挂载设备或分区的文件系统类型,支持多种不同的文件系统。
<options>:挂载时使用的参数,部分参数为特定文件系统所有。常用参数包括:
auto:在启动时或执行 mount -a 命令时自动挂载。
noauto:只在您的命令下被挂载。
exec:允许执行此分区的二进制文件。
noexec:不允许执行此文件系统上的二进制文件。
ro:以只读模式挂载文件系统。
rw:以读写模式挂载文件系统。
user:允许任意用户挂载此文件系统,若无显示定义,隐含启用 noexec、nosuid、nodev 参数。
users:允许所有 users 组中的用户挂载文件系统。
nouser:只能被 root 挂载。
owner:允许设备所有者挂载。
sync:I/O 同步进行。
async:I/O 异步进行。
dev:解析文件系统上的块特殊设备。
nodev:不解析文件系统上的块特殊设备。
suid:允许 suid 操作和设定 sgid 位。
nosuid:禁止 suid 操作和设定 sgid 位。
noatime:不更新文件系统上 inode 访问记录,可以提升性能(参见 atime 参数)。
nodiratime:不更新文件系统上的目录 inode 访问记录,可以��升性能(参见 atime 参数)。
relatime:实时更新 inode 访问记录,仅在记录的访问时间早于当前访问时才会更新。
flush:vfat 的选项,更频繁地刷新数据,复制对话框或进度条在全部数据都写入后才消失。
defaults:使用文件系统的默认挂载参数,例如 ext4 的默认参数为:rw、suid、dev、exec、auto、nouser、async。
<dump>:dump 工具通过它决定何时做备份。dump 会检查其内容,并用数字来决定是否对这个文件系统进行备份。允许的数字为 0 和 1。0 表示忽略,1 则进行备份。
<pass>:fsck 读取 <pass> 的数值来决定需要检查的文件系统的检查顺序。允许的数字为 0、1 和 2。根目录应当获得最高的优先权 1,其它所有需要被检查的设备设置为 2。0 表示设备不会被 fsck 所检查。
使用内核名称标识磁盘:
# <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为避免云服务器在空闲时频繁刷内核日志导致业务卡顿,可将以下参数写入 rc.local 文件:
echo 1 > /sys/module/rcupdate/parameters/rcu_cpu_stall_suppress未添加该参数时,系统会每隔几秒打印内核日志。
在 CentOS 6.6 系统中,安装 X Window 后 NetworkManager 服务可能会自动启动,导致云服务器重启后无法获取 IP 地址和 DNS 地址。通过关闭该服务可以解决此问题。
通过控制台 VNC 连接服务器后,执行以下操作:
临时关闭:
service NetworkManager stop永久关闭:
chkconfig NetworkManager off如果 /home 分区较大,可以让不依赖 /home 分区的服务先启动。将以下参数添加到 /etc/fstab 文件中 /home 条目的参数部分:
noauto,x-systemd.automount这样 /home 分区只有在需要访问时才会被挂载。内核会缓存所有的文件操作,直到 /home 分区准备完成。这会使 /home 的文件系统类型被识别为 autofs,mlocate 查询时会忽略该目录。
挂载远程文件系统同理。另外,还可以设置 x-systemd.device-timeout=# 参数。
纯净模式
