目录
❀My学习Linux命令小记录(11)❀
41.insmod指令
功能说明:将给定的模块加载到内核中。
(ps.insmod命令用于将给定的模块加载到内核中。Linux有许多功能是通过模块的方式,在需要时才载入kernel。如此可使kernel较为精简,进而提高效率,以及保有较大的弹性。这类可载入的模块,通常是设备驱动程序。)
基本语法:insmod(选项)(参数)
选项:
-f:不检查目前kernel版本与模块编译时的kernel版本是否一致,强制将模块载入;
-k:将模块设置为自动卸除;
-m:输出模块的载入信息;
-o<模块名称>:指定模块的名称,可使用模块文件的文件名;
-p:测试模块是否能正确地载入kernel;
-s:将所有信息记录在系统记录文件中;
-v:执行时显示详细的信息;
-x:不要汇出模块的外部符号;
-X:汇出模块所有的外部符号,此为预设置。
实例:
# insmod led.o #向内核加载模块
# insmod /lib/modules/2.6. #加载RAID1阵列级别模块
18-8.el5/kernel/drivers/md/raid1.ko
42.lsmod指令
功能说明:显示已载入系统的模块。
(ps.lsmod命令用于显示已经加载到内核中的模块的状态信息。执行lsmod命令后会列出所有已载入系统的模块。Linux操作系统的核心具有模块化的特性,应此在编译核心时,务须把全部的功能都放入核心。您可以将这些功能编译成一个个单独的模块,待需要时再分别载入。)
基本语法:lsmod
实例:
# lsmod | grep raid1 #查看加载RAID1阵列级别的模块
raid1 25153 0
从以上显示结果可知,RAID1模块已加载成功。只是在使用insmod命令加载模块时,需要使用绝对路径方能加载,且加载时无法自动解决依赖关系。
# lsmod
Module Size Used by
ipv6 272801 15
xfrm_nalgo 13381 1 ipv6
crypto_api 12609 1 xfrm_nalgo
ip_conntrack_ftp 11569 0
xt_limit 6721 2
xt_state 6209 2
ip_conntrack 53665 2 ip_conntrack_ftp,xt_state
nfnetlink 10713 1 ip_conntrack
xt_tcpudp 7105 6
xt_multiport 7233 1
iptable_filter 7105 1
ip_tables 17029 1 iptable_filter
x_tables 17349 5 xt_limit,xt_state,xt_tcpudp,xt_multiport,ip_tables
dm_mirror 24393 0
dm_multipath 27213 0
scsi_dh 12481 1 dm_multipath
第1列Module:表示模块的名称。
第2列Size:表示模块的大小。
第3列Used:表示依赖模块的个数。
第4列by:表示依赖模块的内容。
通常在使用lsmod命令时,都会采用类似lsmod | grep -i ext3这样的命令来查询当前系统是否加载了某些模块。
43.sysctl指令
功能说明:在内核运行时动态地修改内核的运行参数。
(ps.sysctl命令被用于在内核运行时动态地修改内核的运行参数,可用的内核参数在目录/proc/sys中。它包含一些TCP/ip堆栈和虚拟内存系统的高级选项, 这可以让有经验的管理员提高引人注目的系统性能。用sysctl可以读取设置超过五百个系统变量。)
基本语法: sysctl(选项)(参数)
选项:
-n:打印值时不打印关键字;
-e:忽略未知关键字错误;
-N:仅打印名称;
-w:当改变sysctl设置时使用此项;
-p:从配置文件“/etc/sysctl.conf”加载内核参数设置;
-a:打印当前所有可用的内核参数变量和值;
-A:以表格方式打印当前所有可用的内核参数变量和值。
实例:
#sysctl -a #查看所有可读变量
sysctl: reading key “net.ipv6.conf.all.stable_secret”
sysctl: reading key “net.ipv6.conf.default.stable_secret”
abi.vsyscall32 = 1
crypto.fips_enabled = 0
debug.exception-trace = 1
debug.kprobes-optimization = 1
…
vm.stat_interval = 1
vm.swappiness = 30
vm.user_reserve_kbytes = 118516
vm.vfs_cache_pressure = 100
# sysctl -n net.ipv4.ip_forward #查看某变量的值
1
#sysctl kern.maxproc kern.maxproc: 1044 #读一个指定的变量,例如kern.maxproc
#sysctl kern.maxfiles=5000 #要设置一个指定的变量,直接用variable=value
kern.maxfiles: 2088 -> 5000
可以使用sysctl修改系统变量,也可以通过编辑sysctl.conf文件来修改系统变量。sysctl.conf看起来很像rc.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
如果该文件为空,则输入以下内容,否则请根据情况自己做调整:
如果希望屏蔽别人 ping 你的主机,则加入以下代码:
# Disable ping requests
net.ipv4.icmp_echo_ignore_all = 1
编辑完成后,请执行以下命令使变动立即生效:
# /sbin/sysctl -p
# /sbin/sysctl -w net.ipv4.route.flush=1
44.systemctl指令
功能说明:系统服务管理器指令。
(ps.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
语法:
systemctl start nfs-server.service . # 启动nfs服务
systemctl enable nfs-server.service # 设置开机自启动
systemctl disable nfs-server.service # 停止开机自启动
systemctl status nfs-server.service # 查看服务当前状态
systemctl restart nfs-server.service # 重新启动某服务
systemctl list-units --type=service # 查看所有已启动的服务
实例:
# systemctl #查看系统状态,这会显示当前系统的状态,包括运行中的服务和其状态。
# systemctl list-units #显示所有单元(units)的状态,这将列出所有加载的单元(服务、套接字、设备等)以及它们的状态。
# sudo systemctl start serviceName #启动服务,这会启动指定的服务。例如,sudo systemctl start apache2 会启动 Apache web 服务器。
# sudo systemctl stop serviceName #停止服务,这会停止指定的服务。
# sudo systemctl restart serviceName #重启服务,这会重启指定的服务。
# systemctl status serviceName #查看服务状态,这将显示指定服务的详细信息,包括是否正在运行以及最近的日志。
# sudo systemctl enable serviceName #启用服务开机自启动,这会配置指定的服务在系统启动时自动启动。
# sudo systemctl disable serviceName #禁用服务开机自启动,这会配置指定的服务在系统启动时不自动启动。
# journalctl -u serviceName #查看服务的所有日志,这会显示指定服务的所有相关日志。
# journalctl -b #查看系统引导日志,这会显示当前引导的系统日志。
这只是 systemctl 的一些基本用法。systemctl 提供了许多其他选项,可以用于更复杂的系统管理任务。要了解更多信息,你可以查阅 systemctl 的手册页(man systemctl)或系统文档。
45.exec指令
功能说明:调用并执行指定的命令。
(ps.exec命令用于调用并执行指令的命令。exec命令通常用在shell脚本程序中,可以调用其他的命令。如果在当前终端中使用命令,则当指定的命令执行完毕后会立即退出终端。)
语法:exec(选项)(参数)
选项:
-c:在空环境中执行指定的命令。
实例:
# echo Linux C++ # 输出指定信息
Linux C++
# exec -c echo Linux C++ #然后再使用exec命令调用echo命令输出同样的信息,并且对输出的信息进行对比,输入指令如下所示:
Linux C++ # 使用指定指令输出信息
通过比较两者执行后的结果来看,所实现的功能是相同的,即使用exec命令调用echo命令成功。
❀My学习Linux命令小记录(11)❀over~