1、参考链接:
https://blog.csdn.net/huangchonghai/article/details/120735517
https://www.cnblogs.com/jest549/p/14697557.html
ubuntu修改 ulimit -c unlimit - Bigben - 博客园
解决办法:GLib-ERROR **: Creating pipes for GWakeup: Too many open files_柳鲲鹏的博客-CSDN博客
步骤1、修改/etc/security/limits.conf文件,我的修改如下
- # /etc/security/limits.conf
- #
- #Each line describes a limit for a user in the form:
- #
- #
-
- #
- #Where:
- #
can be: - # - a user name
- # - a group name, with @group syntax
- # - the wildcard *, for default entry
- # - the wildcard %, can be also used with %group syntax,
- # for maxlogin limit
- # - NOTE: group and wildcard limits are not applied to root.
- # To apply a limit to the root user,
must be - # the literal username root.
- #
- #
can have the two values: - # - "soft" for enforcing the soft limits
- # - "hard" for enforcing hard limits
- #
- #
- can be one of the following:
- # - core - limits the core file size (KB)
- # - data - max data size (KB)
- # - fsize - maximum filesize (KB)
- # - memlock - max locked-in-memory address space (KB)
- # - nofile - max number of open files
- # - rss - max resident set size (KB)
- # - stack - max stack size (KB)
- # - cpu - max CPU time (MIN)
- # - nproc - max number of processes
- # - as - address space limit (KB)
- # - maxlogins - max number of logins for this user
- # - maxsyslogins - max number of logins on the system
- # - priority - the priority to run user process with
- # - locks - max number of file locks the user can hold
- # - sigpending - max number of pending signals
- # - msgqueue - max memory used by POSIX message queues (bytes)
- # - nice - max nice priority allowed to raise to values: [-20, 19]
- # - rtprio - max realtime priority
- # - chroot - change root to directory (Debian-specific)
- #
- #
-
- #
-
- * hard core unlimited
- * soft core unlimited
-
- * hard nofile unlimited
- * soft nofile unlimited
-
- * hard stack unlimited
- * soft stack unlimited
-
- * hard nproc unlimited
- * soft nproc unlimited
-
-
-
- #* soft core 0
- #root hard core 100000
- #* hard rss 10000
- #@student hard nproc 20
- #@faculty soft nproc 20
- #@faculty hard nproc 50
- #ftp hard nproc 0
- #ftp - chroot /ftp
- #@student - maxlogins 4
-
- # End of file
- #
- #
- zrd hard core unlimited
- zrd soft core unlimited
步骤2、修改/etc/pam.d/su 、/etc/pam.d/common-session、/etc/pam.d/login
关于《limits.conf 生效,与pam_limits.so 文件》,请参考:limits.conf 生效,与pam_limits.so 文件 - 陳聽溪 - 博客园
步骤3、修改/etc/sysctl.conf文件,在文件中(清除文件原始内容(或者在原有的基础上添加,我是这么干的))添加如下行(修改网络内核对TCP连接的有关限制)。
- #
- # /etc/sysctl.conf - Configuration file for setting system variables
- # See /etc/sysctl.d/ for additional system variables.
- # See sysctl.conf (5) for information.
- #
-
- #kernel.domainname = example.com
-
- # Uncomment the following to stop low-level messages on console
- #kernel.printk = 3 4 1 3
-
- ##############################################################3
- # Functions previously found in netbase
- #
-
- # Uncomment the next two lines to enable Spoof protection (reverse-path filter)
- # Turn on Source Address Verification in all interfaces to
- # prevent some spoofing attacks
- #net.ipv4.conf.default.rp_filter=1
- #net.ipv4.conf.all.rp_filter=1
-
- # Uncomment the next line to enable TCP/IP SYN cookies
- # See http://lwn.net/Articles/277146/
- # Note: This may impact IPv6 TCP sessions too
- #net.ipv4.tcp_syncookies=1
-
- # Uncomment the next line to enable packet forwarding for IPv4
- #net.ipv4.ip_forward=1
-
- # Uncomment the next line to enable packet forwarding for IPv6
- # Enabling this option disables Stateless Address Autoconfiguration
- # based on Router Advertisements for this host
- #net.ipv6.conf.all.forwarding=1
-
-
- ###################################################################
- # Additional settings - these settings can improve the network
- # security of the host and prevent against some network attacks
- # including spoofing attacks and man in the middle attacks through
- # redirection. Some network environments, however, require that these
- # settings are disabled so review and enable them as needed.
- #
- # Do not accept ICMP redirects (prevent MITM attacks)
- #net.ipv4.conf.all.accept_redirects = 0
- #net.ipv6.conf.all.accept_redirects = 0
- # _or_
- # Accept ICMP redirects only for gateways listed in our default
- # gateway list (enabled by default)
- # net.ipv4.conf.all.secure_redirects = 1
- #
- # Do not send ICMP redirects (we are not a router)
- #net.ipv4.conf.all.send_redirects = 0
- #
- # Do not accept IP source route packets (we are not a router)
- #net.ipv4.conf.all.accept_source_route = 0
- #net.ipv6.conf.all.accept_source_route = 0
- #
- # Log Martian Packets
- #net.ipv4.conf.all.log_martians = 1
- #
-
- ###################################################################
- # Magic system request Key
- # 0=disable, 1=enable all
- # Debian kernels have this set to 0 (disable the key)
- # See https://www.kernel.org/doc/Documentation/sysrq.txt
- # for what other values do
- #kernel.sysrq=1
-
- ###################################################################
- # Protected links
- #
- # Protects against creating or following links under certain conditions
- # Debian kernels have both set to 1 (restricted)
- # See https://www.kernel.org/doc/Documentation/sysctl/fs.txt
- #fs.protected_hardlinks=0
- #fs.protected_symlinks=0
-
- ###################xiaogongwei###################
-
- net.ipv4.ip_local_port_range = 1024 65535
- net.core.rmem_max=16777216
- net.core.wmem_max=16777216
- net.ipv4.tcp_rmem=4096 87380 16777216
- net.ipv4.tcp_wmem=4096 65536 16777216
- net.ipv4.tcp_fin_timeout = 10
- net.ipv4.tcp_timestamps = 0
- net.ipv4.tcp_window_scaling = 0
- net.ipv4.tcp_sack = 0
- net.core.netdev_max_backlog = 30000
- net.ipv4.tcp_no_metrics_save=1
- net.core.somaxconn = 262144
- net.ipv4.tcp_syncookies = 0
- net.ipv4.tcp_max_orphans = 262144
- net.ipv4.tcp_max_syn_backlog = 262144
- net.ipv4.tcp_synack_retries = 2
- net.ipv4.tcp_syn_retries = 2
步骤4、 执行如下命令(使上述设置生效):
- /sbin/sysctl -p /etc/sysctl.conf
- /sbin/sysctl -w net.ipv4.route.flush=1
步骤5、执行如下命令(linux系统优化完网络必须调高系统允许打开的文件数才能支持大的并发,默认1024是远远不够的):
- echo "ulimit -HSn 65535" >> /etc/rc.local
- echo "ulimit -HSn 65535" >>/root/.bash_profile
- echo "ulimit -SHn 65535" >> /etc/profile
- ulimit -SHn 65535
步骤6、重启机器
通过修改,tcp可以达到65535个连接完全没有问题
通过这个命令 ulimit -n 可以看到值改为65535了,也就是说现在最多支持65536个tcp socket连接了查看当前有多少个TCP连接到当前服务器命令:netstat -antp |grep -i est |wc -l