全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

Linux常用系统参数设置

最近更新时间:2020-06-09 19:53:31

syslog命令

syslog是Linux系统默认的日志守护进程。syslog 接受来自系统的各种功能的信息,每个信息都包括重要级。/etc/syslog.conf 文件通知 syslogd 如何根据设备和信息重要级别来报告信息。可以使用logger命令通过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命令

systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。

任务 旧指令 新指令
使某服务自动启动 chkconfig --level 3 httpd on systemctl enable httpd.service
使某服务不自动启动 chkconfig --level 3 httpd off systemctl disable httpd.service
检查服务状态 service httpd status systemctl status httpd.service (服务详细信息) systemctl is-active httpd.service (仅显示是否 Active)
显示所有已启动的服务 chkconfig --list systemctl list-units --type=service
启动某服务 service httpd start systemctl start httpd.service
停止某服务 service httpd stop systemctl stop httpd.service
重启某服务 service httpd restart systemctl restart httpd.service

实例

1.启动nfs服务

systemctl start nfs-server.service

2.设置开机自启动

systemctl enable nfs-server.service

3.停止开机自启动

systemctl disable nfs-server.service

4.查看服务当前状态

systemctl status nfs-server.service

5.重新启动某服务

systemctl restart nfs-server.service

6.查看所有已启动的服务

systemctl list -units --type=service

开启防火墙22端口

iptables -I INPUT -p tcp --dport 22 -j accept

关闭SElinux:
修改/etc/selinux/config文件中的SELINUX=””为disabled,然后重启。
彻底关闭防火墙:

sudo systemctl status firewalld.servicesudo systemctl stop firewalld.servicesudo systemctl disable firewalld.service

crontab命令

crontab命令被用来提交和管理用户的需要周期性执行的任务,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行。

语法

crontab(选项)(参数)

选项

-e:编辑该用户的计时器设置;
-l:列出该用户的计时器设置;
-r:删除该用户的计时器设置;
-u<用户名称>:指定要设定计时器的用户名称。

参数

crontab文件:指定包含待执行任务的crontab文件。

mount命令

mount命令用于加载文件系统到指定的加载点。此命令的最常用于挂载cdrom,使我们可以访问cdrom中的数据,因为你将光盘插入cdrom中,Linux并不会自动挂载,必须使用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.xml

fsck命令

fsck命令被用于检查并且试图修复文件系统中的错误。当文件系统发生错误时,可用fsck指令尝试加以修复。

语法

fsck(选项)(参数)

选项

-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命令

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命令

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=5000kern.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

配置sysctl

编辑此文件:/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

fstab

这个文件描述系统中各种文件系统的信息。文件/etc/fstab包含了静态文件系统信息,定义了存储设备和分区整合到整个系统的方式。mount 命令会读取这个文件,确定设备和分区的挂载选项。

文件格式

<file system> <dir> <type> <options> <dump> <pass>

通过空格或 Tab 分隔。
<file systems> - 要挂载的分区或存储设备.
<dir> - <file systems>的挂载位置。
<type> - 要挂载设备或是分区的文件系统类型,支持许多种不同的文件系统
<options> - 挂载时使用的参数,注意有些mount 参数是特定文件系统才有的。一些比较常用的参数有:

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 access 记录。只有在记录中的访问时间早于当前访问才会被更新。
 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 所检查。

实例

使用内核名称标识磁盘:

/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

这个参数在rc.local文件中,解决云主机空闲时频繁刷日志导致业务卡顿的问题。
不加这个参数 ,隔几秒会打印内核日志。

关闭networkmanager服务

centos6.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=#参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。 如果你的加密文件系统需要密钥,则需要添加 noauto 参数到 /etc/crypttab 文件中的对应位置。

/etc/crypttabdata /dev/md0 /root/key noauto

交换分区UUID

如果交换分区没有 UUID,可手动加入。
如果使用 lsblk -f 命令没有列出交换分区的 UUID 就说明发生了这种情况。为交换分区指定 UUID 的步骤:

# swapon -s 确定交换分区  
# swapoff /dev/sda7 禁用交换分区  
# mkswap -U random /dev/sda7 用新 UUID 重新创建交换分区  
# swapon /dev/sda7 激活交换分区

路径名有空格

如果挂载的路径中有空格,可以使用 “040” 转义字符来表示空格(以三位八进制数来进行表示)

/etc/fstab
UUID=47FA-4071 /home/username/Camera040Pictures vfat defaults,noatime 0 2
/dev/sda7 /media/100040GB040(Storage) ext4 defaults,noatime,user 0 0

外部设备

外部设备在插入时挂载,在未插入时忽略。这需要 nofail ,启动时设备不存在直接忽略它不报错.

/etc/fstab /dev/sdg1 /media/backup jfs defaults,nofail 0 2

普通用户读写

FAT32必须修改/etc/fstab文件才能取得对 FAT32 分区的写权限。

/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
文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈