云服务器

Linux常用系统参数设置命令

syslog命令

syslog是Linux系统默认的日志守护进程。syslog 接受来自系统的各种功能的信息,每个信息都包括重要级。/etc/syslog.conf 文件通知 syslogd 如何根据设备和信息重要级别来报告信息。可以使用logger命令通过syslogd记录日志。
要向syslog文件 /var/log/messages 中记录日志信息:

  1. logger this is a test log line

输出:

  1. tail -n 1 messagesJan 5 10:07:03 localhost root: this is a test log line

记录特定的标记(tag)可以使用:

  1. logger -t TAG this is a test log line

输出:

  1. 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服务

    1. systemctl start nfs-server.service
  2. 设置开机自启动

    1. systemctl enable nfs-server.service
  3. 停止开机自启动

    1. systemctl disable nfs-server.service
  4. 查看服务当前状态

    1. systemctl status nfs-server.service
  5. 重新启动某服务

    1. systemctl restart nfs-server.service
  6. 查看所有已启动的服务

    1. systemctl list -units --type=service
  7. 开启防火墙22端口

    1. iptables -I INPUT -p tcp --dport 22 -j accept
  8. 关闭SElinux:
    运行命令getenforce,验证SELinux状态。返回结果若是enforcing,表明SELinux已开启。

    • 临时关闭:
      执行命令setenforce 0
    • 永久关闭:
      1. 运行以下命令,编辑SELinux的config文件:
        1. vi /etc/selinux/config
      2. 找到SELINUX=enforcing,按i键进入编辑模式,将参数修改为SELINUX=disabled
      3. 修改完成后,按下Esc键,执行命令:wq,保存并退出文件。
      4. 重启实例。
        1. shutdown -r now
      5. 重启后,运行命令getenforce,验证SELinux状态为disabled,表明SELinux已关闭。
  9. 彻底关闭防火墙:

    1. 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”中描述的所有文件系统。

参数

设备文件名:指定要加载的文件系统对应的设备名
加载点:指定加载点目录。

示例

  1. mount -t auto /dev/cdrom /mnt/cdrom
  2. mount: mount point /mnt/cdrom does not exist /mnt/cdrom目录不存在,需要先创建。
  3. cd /mnt
  4. -bash: cd: /mnt: No such file or directory
  5. mkdir -p /mnt/cdrom 创建/mnt/cdrom目录
  6. ls
  7. bin dev home lib media mnt proc sbin srv tmp
  8. var
  9. boot etc initrd lost+found misc opt root selinux sys usr
  10. mount -t auto /dev/cdrom /mnt/cdrom 挂载cdrom
  11. mount: block device /dev/cdrom is write-protected, mounting read-only 挂载成功
  12. ll /mnt/cdrom 查看cdrom里面内容
  13. total 859
  14. dr-xr-xr-x 4 root root 2048 Sep 4 2005 CentOS
  15. -r--r--r-- 2 root root 8859 Mar 19 2005 centosdocs-man.css
  16. -r--r--r-- 9 root root 18009 Mar 1 2005 GPL
  17. dr-xr-xr-x 2 root root 241664 May 7 02:32 headers
  18. dr-xr-xr-x 4 root root 2048 May 7 02:23 images
  19. dr-xr-xr-x 2 root root 4096 May 7 02:23 isolinux
  20. dr-xr-xr-x 2 root root 18432 May 2 18:50 NOTES
  21. -r--r--r-- 2 root root 5443 May 7 01:49 RELEASE-NOTES-en.html
  22. dr-xr-xr-x 2 root root 2048 May 7 02:34 repodata
  23. -r--r--r-- 9 root root 1795 Mar 1 2005 rpm-GPG-KEY
  24. -r--r--r-- 2 root root 1795 Mar 1 2005 RPM-GPG-KEY-centos4
  25. -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的文件系统损坏会导致不正常关机,出错时若系统告知某块硬盘的分区有问题,比如/dev/hda2,可用如下命令处理:

  1. fsck -y /dev/hda2

结束后使用reboot命令重启系统。
若不知何处出问题,可以直接输入命令:

  1. fsck

在随后的多个确认对话框中输入:y
结束后同样使用reboot命令重启系统。

ulimit命令

ulimit命令用来限制系统用户对shell资源的访问,它是一种简单并且有效的实现资源限制的方式。
作为临时限制,ulimit 可作用于使用命令登录的 shell 会话,在会话终止时便结束限制。对于长期的固定限制,ulimit 命令语句可被添加到由登录 shell 读取的文件中,作用于特定的 shell 用户。

语法

ulimit (选项)

选项

-a:显示目前资源限制的设定;
-c :设定core文件的最大值,单位为区块;
-d <数据节区大小>:程序数据节区的最大值,单位为KB;
-f <文件大小>:shell所能建立的最大文件,单位为区块;
-H:设定资源的硬性限制,也就是管理员所设下的限制;
-m <内存大小>:指定可使用内存的上限,单位为KB;
-n <文件数目>:指定同一时间最多可开启的文件数;
-p <缓冲区大小>:指定管道缓冲区的大小,单位512字节;
-s <堆叠大小>:指定堆叠的上限,单位为KB;
-S:设定资源的弹性限制;
-t :指定CPU使用时间的上限,单位为秒;
-u <程序数目>:用户最多可开启的程序数目;
-v <虚拟内存大小>:指定可使用的虚拟内存上限,单位为KB。

示例

  1. [root@localhost ~]# ulimit -a
  2. core file size (blocks, -c) 0 #core文件的最大值为100 blocks。
  3. data seg size (kbytes, -d) unlimited #进程的数据段可以任意大。
  4. scheduling priority (-e) 0
  5. file size (blocks, -f) unlimited #文件可以任意大。
  6. pending signals (-i) 98304 #最多有98304个待处理的信号。
  7. max locked memory (kbytes, -l) 32 #一个任务锁住的物理内存的最大值为32KB。
  8. max memory size (kbytes, -m) unlimited #一个任务的常驻物理内存的最大值。
  9. open files (-n) 1024 #一个任务最多可以同时打开1024的文件。
  10. pipe size (512 bytes, -p) 8 #管道的最大空间为4096字节。
  11. POSIX message queues (bytes, -q) 819200 #POSIX的消息队列的最大值为819200字节。
  12. real-time priority (-r) 0
  13. stack size (kbytes, -s) 10240 #进程的栈的最大值为10240字节。
  14. cpu time (seconds, -t) unlimited #进程使用的CPU时间。
  15. max user processes (-u) 98304 #当前用户同时打开的进程(包括线程)的最大个数为98304。
  16. virtual memory (kbytes, -v) unlimited #没有限制进程的最大地址空间。
  17. 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:

  1. sysctl kern.maxproc kern.maxproc: 1044

要设置一个指定的变量,直接用variable=value这样的语法:

  1. sysctl kern.maxfiles=5000kern.maxfiles: 2088 -> 5000

可用sysctl修改系统变量,也可通过编辑sysctl.conf文件来修改系统变量。它用variable=value的形式来设定值。sysctl变量的设置通常是字符串、数字或者布尔型(用 1 来表示’yes’,用 0 来表示’no’)。

  1. sysctl -w kernel.sysrq=0
  2. sysctl -w kernel.core_uses_pid=1
  3. sysctl -w net.ipv4.conf.default.accept_redirects=0
  4. sysctl -w net.ipv4.conf.default.accept_source_route=0
  5. sysctl -w net.ipv4.conf.default.rp_filter=1
  6. sysctl -w net.ipv4.tcp_syncookies=1
  7. sysctl -w net.ipv4.tcp_max_syn_backlog=2048
  8. sysctl -w net.ipv4.tcp_fin_timeout=30
  9. sysctl -w net.ipv4.tcp_synack_retries=2
  10. sysctl -w net.ipv4.tcp_keepalive_time=3600
  11. sysctl -w net.ipv4.tcp_window_scaling=1
  12. sysctl -w net.ipv4.tcp_sack=1

配置sysctl

编辑此文件:/etc/sysctl.conf 如果该文件为空,则输入以下内容,请根据情况自己做调整:

  1. # Controls source route verification
  2. # Default should work for all interfaces
  3. net.ipv4.conf.default.rp_filter = 1
  4. # net.ipv4.conf.all.rp_filter = 1
  5. # net.ipv4.conf.lo.rp_filter = 1
  6. # net.ipv4.conf.eth0.rp_filter = 1
  7. # Disables IP source routing
  8. # Default should work for all interfaces
  9. net.ipv4.conf.default.accept_source_route = 0
  10. # net.ipv4.conf.all.accept_source_route = 0
  11. # net.ipv4.conf.lo.accept_source_route = 0
  12. # net.ipv4.conf.eth0.accept_source_route = 0
  13. # Controls the System Request debugging functionality of the kernel
  14. kernel.sysrq = 0
  15. # Controls whether core dumps will append the PID to the core filename.
  16. # Useful for debugging multi-threaded applications.
  17. kernel.core_uses_pid = 1
  18. # Increase maximum amount of memory allocated to shm
  19. # Only uncomment if needed!
  20. # kernel.shmmax = 67108864
  21. # Disable ICMP Redirect Acceptance
  22. # Default should work for all interfaces
  23. net.ipv4.conf.default.accept_redirects = 0
  24. # net.ipv4.conf.all.accept_redirects = 0
  25. # net.ipv4.conf.lo.accept_redirects = 0
  26. # net.ipv4.conf.eth0.accept_redirects = 0
  27. # enable Log Spoofed Packets, Source Routed Packets, Redirect Packets
  28. # Default should work for all interfaces
  29. net.ipv4.conf.default.log_martians = 1
  30. # net.ipv4.conf.all.log_martians = 1
  31. # net.ipv4.conf.lo.log_martians = 1
  32. # net.ipv4.conf.eth0.log_martians = 1
  33. # Decrease the time default value for tcp_fin_timeout connection
  34. net.ipv4.tcp_fin_timeout = 25
  35. # Decrease the time default value for tcp_keepalive_time connection
  36. net.ipv4.tcp_keepalive_time = 1200
  37. # Turn on the tcp_window_scaling
  38. net.ipv4.tcp_window_scaling = 1
  39. # Turn on the tcp_sack
  40. net.ipv4.tcp_sack = 1
  41. # tcp_fack should be on because of sack
  42. net.ipv4.tcp_fack = 1
  43. # Turn on the tcp_timestamps
  44. net.ipv4.tcp_timestamps = 1
  45. # Enable TCP SYN Cookie Protection
  46. net.ipv4.tcp_syncookies = 1
  47. # Enable ignoring broadcasts request
  48. net.ipv4.icmp_echo_ignore_broadcasts = 1
  49. # Enable bad error message Protection
  50. net.ipv4.icmp_ignore_bogus_error_responses = 1
  51. # make more local ports available
  52. # net.ipv4.ip_local_port_range = 1024 65000
  53. # set TCP Re-Ordering value in kernel to ‘5′
  54. net.ipv4.tcp_reordering = 5
  55. # Lower syn retry rates
  56. net.ipv4.tcp_synack_retries = 2
  57. net.ipv4.tcp_syn_retries = 3
  58. # Set Max SYN Backlog to ‘2048′
  59. net.ipv4.tcp_max_syn_backlog = 2048
  60. # Various Settings
  61. net.core.netdev_max_backlog = 1024
  62. # Increase the maximum number of skb-heads to be cached
  63. net.core.hot_list_length = 256
  64. # Increase the tcp-time-wait buckets pool size
  65. net.ipv4.tcp_max_tw_buckets = 360000
  66. # This will increase the amount of memory available for socket input/output queues
  67. net.core.rmem_default = 65535
  68. net.core.rmem_max = 8388608
  69. net.ipv4.tcp_rmem = 4096 87380 8388608
  70. net.core.wmem_default = 65535
  71. net.core.wmem_max = 8388608
  72. net.ipv4.tcp_wmem = 4096 65535 8388608
  73. net.ipv4.tcp_mem = 8388608 8388608 8388608
  74. net.core.optmem_max = 40960

fstab

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

文件格式

  1. <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 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 检查。

示例

使用内核名称标识磁盘:

  1. /etc/fstab
  2. # <file system> <dir> <type> <options> <dump> <pass>
  3. tmpfs /tmp tmpfs nodev,nosuid 0 0
  4. /dev/sda1 / ext4 defaults,noatime 0 1
  5. /dev/sda2 none swap defaults 0 0
  6. /dev/sda3 /home ext4 defaults,noatime 0 2

禁止内核在空闲时频繁打印日志

  1. echo 1>/sys/module/rcupdate/parameters/rcu_cpu_stall_suppress

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

关闭networkmanager服务

CentOS 6.6 系统安装 x-window 后 networkmanager 服务自动启动,会导致云主机重启后无法获取 IP 地址和 DNS 地址,可通过关闭该服务解决此问题。

通过控制台VNC连接主机:

临时关闭:

  1. service NetworkManager stop

永久关闭:

  1. chkconfig NetworkManager off

小技巧

自动挂载

若 /home 分区较大,可让不依赖 /home 分区的服务先启动,把下面的参数添加到 /etc/fstab 文件中 /home 项目的参数部分。

  1. noauto,x-systemd.automount

这样 /home 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 /home 分区准备完成。这样会使 /home 的文件系统类型被识别为 autofs, mlocate 查询时忽略该目录。
挂载远程文件系统同理。另外,可以设置 x-systemd.device-timeout=#参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。 如果您的加密文件系统需要密钥,则需要添加 noauto 参数到 /etc/crypttab 文件中的对应位置。

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

交换分区UUID

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

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

路径名有空格

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

  1. /etc/fstab
  2. UUID=47FA-4071 /home/username/Camera\040Pictures vfat defaults,noatime 0 2
  3. /dev/sda7 /media/100\040GB\040(Storage) ext4 defaults,noatime,user 0 0

外部设备

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

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

普通用户读写

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

  1. /etc/fstab/dev/sdxY /mnt/some_folder vfat user,rw,umask=000 0 0

比如您的 FAT32 分区在 /dev/sda9,想将其挂载到 /mnt/fat32,可输入并运行如下命令:

  1. /etc/fstab/dev/sda9 /mnt/fat32 vfat user,rw,umask=111,dmask=000 0 0