freebsd内核编译、优化

简介:
一、内核编译   
   优化内核,去掉不用的组件及设备驱动,以提高系统效率,首先使用uname -a查看本机的内核详细版本,使用dmesg查看
本机所有的硬件信息,并进行相应的记录,后续编辑内核文件时要用到
1.安装CVSUP:
------------------------------
最好在安装时装好cvsup;
freebsd# cd /usr/ports/net/cvsup-without-gui/
freebsd# make install clean

2.升级源码:
------------------------------
freebsd# ee /usr/share/examples/cvsup/stable-supfile
把:
default host=CHANGE_THIS.FreeBSD.org
改为:
default host=cvsup.FreeBSDchina.org
src-all
freebsd# ee /usr/share/examples/cvsup/ports-supfile
把:
default host=CHANGE_THIS.FreeBSD.org
改为:
default host=cvsup.FreeBSDchina.org
freebsd# cvsup -g -L 2 /usr/share/examples/cvsup/stable-supfile
或者  csup -g -L 2 /usr/share/examples/cvsup/stable-supfile
freebsd# cvsup -g -L 2 /usr/share/examples/cvsup/ports-supfile
freebsd# cd /usr/obj
freebsd# chflags -R noschg *
freebsd# rm -rf *

3.重新编译源码和内核
-----------------------------------
freebsd# cd /usr/src/sys/amd64(或i386---32位与64位,分别选择相应的)/conf/
freebsd# mkdir /root/kernels
freebsd# cp GENERIC /root/kernels/MYKERNEL
freebsd# cd /usr/src
freebsd# ln -s /root/kernels/MYKERNEL
freebsd# make buildworld    //编译所有的系统程序
freebsd# make buildkernel KERNCONF=MYKERNEL   //编译新的系统核心
freebsd# reboot
freebsd# make installkernel KERNCONF=MYKERNEL  //安裝新的系统核心 
freebsd# make installworld   //安装新的系统程序
freebsd# reboot
重启系统用uname -a查看编辑后的内核是否是自己定制的内核;
编辑内核文件要注意的地方
device  em    # Broadcom BCM570xx Gigabit Ethernet  //加载网卡,一定要慎重,特别是远程
如果不确定网卡型号,可用dmesg |less 查看
如新内核有问题,可以还的原内核文件
mv /boot/kernel /boot/kernel.bak
mv /boot/kernel.old /boot/kernel
 
4、附上优化后的内核文件:
----------------------------------------------------------------
cpu  I686_CPU
ident  MYKERNE
device          pf
device          pflog
device          pfsync
options         ALTQ
options         ALTQ_CBQ
options         ALTQ_RED
options         ALTQ_RIO
options         ALTQ_HFSC
options         ALTQ_PRIQ
options         ALTQ_NOPCC
options         SC_DISABLE_REBOOT
options         IPFIREWALL                            
options         IPFIREWALL_DEFAULT_TO_ACCEPT          
options         DUMMYNET                              
options         HZ=1000
options   IPSEC        #IP security
device    crypto
options  SCHED_ULE  # ULE scheduler
options  PREEMPTION  # Enable kernel thread preemption
options  INET   # InterNETworking
options  SCTP   # Stream Control Transmission Protocol
options  FFS   # Berkeley Fast Filesystem
options  SOFTUPDATES  # Enable FFS soft updates support
options  UFS_ACL   # Support for access control lists
options  UFS_DIRHASH  # Improve performance on big directories
options  UFS_GJOURNAL  # Enable gjournal-based UFS journaling
options  MD_ROOT   # MD is a potential root device
options  PROCFS   # Process filesystem (requires PSEUDOFS)
options  PSEUDOFS  # Pseudo-filesystem framework
options  COMPAT_43TTY  # BSD 4.3 TTY compat [KEEP THIS!]
options  SCSI_DELAY=5000  # Delay (in ms) before probing SCSI
options  KTRACE   # ktrace(1) support
options  STACK   # stack(9) support
options  SYSVSHM   # SYSV-style shared memory
options  SYSVMSG   # SYSV-style message queues
options  SYSVSEM   # SYSV-style semaphores
options  P1003_1B_SEMAPHORES # POSIX-style semaphores
options  _KPOSIX_PRIORITY_SCHEDULING # POSIX P1003_1B real-time extensions
options  KBD_INSTALL_CDEV # install a CDEV entry in /dev
options  ADAPTIVE_GIANT  # Giant mutex is adaptive.
options  STOP_NMI  # Stop CPUS using NMI instead of IPI
options  AUDIT   # Security event auditing
# To make an SMP kernel, the next two lines are needed
options  SMP   # Symmetric MultiProcessor Kernel
device  apic   # I/O APIC
# CPU frequency control
device  cpufreq
# Bus support.
device  eisa
device  pci
# SCSI Controllers
device  mpt  # LSI-Logic MPT-Fusion
device  scbus  # SCSI bus (required for SCSI)
device  ch  # SCSI media changers
device  da  # Direct Access (disks)
device  pass  # Passthrough device (direct SCSI access)
device  ses  # SCSI Environmental Services (and SAF-TE)
# atkbdc0 controls both the keyboard and the PS/2 mouse
device  atkbdc  # AT keyboard controller
device  atkbd  # AT keyboard
device  psm  # PS/2 mouse
device  vga  # VGA video card driver
device  splash  # Splash screen and screen saver support
# syscons is the default console driver, resembling an SCO console
device  sc
device  agp  # support several AGP chipsets
# Power management support (see NOTES for more options)
#device  apm
# Add suspend/resume support for the i8254.
device  pmtimer
# PCI Ethernet NICs that use the common MII bus controller code.
# NOTE: Be sure to keep the 'device miibus' line in order to use these NICs!
device  miibus  # MII bus support
device  em
device  le  # Broadcom BCM570xx Gigabit Ethernet

# Pseudo devices.
device  loop  # Network loopback
device  random  # Entropy device
device  ether  # Ethernet support
device  tun  # Packet tunnel.
device  pty  # Pseudo-ttys (telnet etc)
device  md  # Memory "disks"
device  gif  # IPv6 and IPv4 tunneling
device  bpf  # Berkeley packet filter
# USB support
device  uhci  # UHCI PCI->USB interface
device  ohci  # OHCI PCI->USB interface
device  ehci  # EHCI PCI->USB interface (USB 2.0)
device  usb  # USB Bus (required)
device  ukbd  # Keyboard
 
二、内核参数优化
      内核编译完后,就要对内核的参数做一些调整,以提高服务器的运行速度

/etc/sysctl.conf
----------------------------------------------------------
#通过源路由,攻击者可以尝试到达内部IP地址 --包括RFC1918中的地址,所以 
不接受源路由信息包可以防止你的内部网络被探测
net.inet.ip.sourceroute=0 
net.inet.ip.accept_sourceroute=0
#安全参数,编译内核的时候加了options TCP_DROP_SYNFIN才可以用,可以阻止某些OS探测
net.inet.tcp.drop_synfin=1
#最大的待发送TCP数据缓冲区空间
net.inet.tcp.sendspace=65536
#最大的接受TCP缓冲区空间
net.inet.tcp.recvspace=65536
#最大的接受UDP缓冲区大小
net.inet.udp.recvspace=49152
#最大的发送UDP数据缓冲区大小
net.inet.udp.maxdgram=24576
#本地套接字连接的数据发送空间
net.local.stream.sendspace=65535
#加快网络性能的协议
net.inet.tcp.rfc1323=1
net.inet.tcp.rfc3042=1
net.inet.tcp.rfc3390=1
#最大的套接字缓冲区
kern.ipc.maxsockbuf=2097152
#系统中允许的最多文件数量
kern.maxfiles=65536
#每个进程能够同时打开的最大文件数量
kern.maxfilesperproc=32768
#该选项设置是否延迟ACK应答数据包
net.inet.tcp.delayed_ack=1
#屏蔽ICMP重定向功能
net.inet.icmp.drop_redirect=1
net.inet.icmp.log_redirect=1
net.inet.ip.redirect=0
net.inet6.ip6.redirect=0
#防止ICMP广播风暴
net.inet.icmp.bmcastecho=0
net.inet.icmp.maskrepl=0
#限制系统发送ICMP速率
net.inet.icmp.icmplim=100
net.inet.icmp.icmplim_output=1
#设置为1会帮助系统清除没有正常断开的TCP连接
net.inet.tcp.always_keepalive=1
#若看到net.inet.ip.intr_queue_drops这个在增加,就要调大net.inet.ip.intr_queue_maxlen,为0最好
net.inet.ip.intr_queue_maxlen=1000
net.inet.ip.intr_queue_drops=0
#防止DOS攻击,默认为30000
net.inet.tcp.msl=2500
#接收到一个已经关闭的端口发来的所有包,直接drop,如果设置为1则是只针对TCP包
net.inet.tcp.blackhole=2
#接收到一个已经关闭的端口发来的所有UDP包直接drop
net.inet.udp.blackhole=1
#为网络数据连接时提供缓冲
net.inet.tcp.inflight.enable=1
#限制 TCP 带宽延迟积和 NetBSD 的 TCP/Vegas 类似。  
#它可以通过将 sysctl 变量 net.inet.tcp.inflight.enable 设置成 1 来启用。  
#系统将尝试计算每一个连接的带宽延迟积,并将排队的数据量限制在恰好能保持最优吞吐量的水平上。 
#这一特性在您的服务器同时向使用普通调制解调器,千兆以太网,乃至更高速度的光与网络连接 (或其他带宽延迟积很
#大的连接) 的时候尤为重要, 
#特别是当您同时使用滑动窗缩放,或使用了大的发送窗口的时候。  
#如果启用了这个选项,您还应该把 net.inet.tcp.inflight.debug 设置为 0 (禁用调试), 
#对于生产环境而言, 将 net.inet.tcp.inflight.min 设置成至少 6144 会很有好处。  
#然而, 需要注意的是,这个值设置过大事实上相当于禁用了连接带宽延迟积限制功能。 
#这个限制特性减少了在路由和交换包队列的堵塞数据数量,也减少了在本地主机接口队列阻塞的数据的数量。 
#在少数的等候队列中、交互式连接,尤其是通过慢速的调制解调器,也能用低的 往返时间操作。 
#但是,注意这只影响到数据发送 (上载/服务端)。对数据接收(下载)没有效果。 
#调整 net.inet.tcp.inflight.stab 是 不 推荐的。 
#这个参数的默认值是 20,表示把 2 个最大包加入到带宽延迟积窗口的计算中。  
#额外的窗口似的算法更为稳定,并改善对于多变网络环境的相应能力,  
#但也会导致慢速连接下的 ping 时间增长 (尽管还是会比没有使用 inflight 算法低许多)。  
#对于这些情形, 您可能会希望把这个参数减少到 15, 10, 或 5;  
#并可能因此而不得不减少 net.inet.tcp.inflight.min (比如说, 3500) 来得到希望的效果。 
#减少这些参数的值, 只应作为最后不得已时的手段来使用。 
net.inet.tcp.inflight.debug=0
net.inet.tcp.inflight.rttthresh=10
net.inet.tcp.inflight.min=6144
net.inet.tcp.inflight.max=1073725440
net.inet.tcp.inflight.stab=20
#如果打开的话每个目标地址一次转发成功以后它的数据都将被记录进路由表和arp数据表,节约路由的计算时间,但会需#
要大量的内核内存空间来保存路由表
net.inet.ip.fastforwarding=1
#默认情况下,ip包的id号是连续的,如果设置成1,则这个id号是随机的
net.inet.ip.random_id=1
#并发连接数,默认为128,推荐在1024-4096之间,数字越大占用内存也越大
kern.ipc.somaxconn=8192
#不允许用户看到其他用户的进程
security.bsd.see_other_uids=0
security.bsd.see_other_gids=0
#设置kernel安全级别
kern.securelevel=0
#记录下任何TCP连接
net.inet.tcp.log_in_vain=0
#记录下任何UDP连接
net.inet.udp.log_in_vain=0
#防止不正确的udp包的攻击
net.inet.udp.checksum=1
#防止DOS攻击
net.inet.tcp.syncookies=1
#仅为线程提供物理内存支持,需要256兆以上内存
kern.ipc.shm_use_phys=1
# 线程可使用的最大共享内存
kern.ipc.shmmax=67108864
# 最大线程数量
kern.ipc.shmall=32768
# 程序崩溃时不记录
kern.coredump=0
#lo本地数据流接收和发送空间
net.local.stream.recvspace=65536
net.local.dgram.maxdgram=16384
net.local.dgram.recvspace=65536
# 本地数据最大数量
net.inet.raw.maxdgram=65536
# 本地数据流接收空间
net.inet.raw.recvspace=65536
#ipfw防火墙动态规则数量,默认为4096,增大该值可以防止某些病毒发送大量TCP连接,导致不能建立正常连接
net.inet.ip.fw.dyn_max=65535
#所有MPSAFE的网络ISR对包做立即响应,提高网卡性能
net.isr.direct=1
#清理apache产生的CLOSE_WAIT状态 
net.inet.ip.rtexpire=3600
net.inet.ip.rtminexpire=2
#TCP的套接字的空闲时间
net.inet.tcp.keepidle=600000
#控制TCP及UDP所使用的port范围
net.inet.ip.portrange.first=8888 
net.inet.ip.portrange.hifirst=8888
#加载linux内核版本
compat.linux.osrelease=2.6.16

/boot/loader.conf
----------------------------------------------------
kern.maxdsiz="536870912"
kern.ipc.maxsockets="16424" 
kern.ipc.nmbclusters="32768" 
kern.ipc.nmbufs="65535"
kern.ipc.nsfbufs="2496"
net.inet.tcp.tcbhashsize="2048"
kern.maxusers="256"
 

本文转自king_819 51CTO博客,原文链接:http://blog.51cto.com/kerry/235943,如需转载请自行联系原作者
相关文章
|
Unix 网络安全 C语言
|
Shell Unix 域名解析
|
数据安全/隐私保护 网络协议 Unix
|
Unix 开发工具 网络安全