• 修改Ubuntu对CPU,socket,进程,文件数等的限制


    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文件,我的修改如下

    1. # /etc/security/limits.conf
    2. #
    3. #Each line describes a limit for a user in the form:
    4. #
    5. #
    6. #
    7. #Where:
    8. # can be:
    9. # - a user name
    10. # - a group name, with @group syntax
    11. # - the wildcard *, for default entry
    12. # - the wildcard %, can be also used with %group syntax,
    13. # for maxlogin limit
    14. # - NOTE: group and wildcard limits are not applied to root.
    15. # To apply a limit to the root user, must be
    16. # the literal username root.
    17. #
    18. # can have the two values:
    19. # - "soft" for enforcing the soft limits
    20. # - "hard" for enforcing hard limits
    21. #
    22. # can be one of the following:
    23. # - core - limits the core file size (KB)
    24. # - data - max data size (KB)
    25. # - fsize - maximum filesize (KB)
    26. # - memlock - max locked-in-memory address space (KB)
    27. # - nofile - max number of open files
    28. # - rss - max resident set size (KB)
    29. # - stack - max stack size (KB)
    30. # - cpu - max CPU time (MIN)
    31. # - nproc - max number of processes
    32. # - as - address space limit (KB)
    33. # - maxlogins - max number of logins for this user
    34. # - maxsyslogins - max number of logins on the system
    35. # - priority - the priority to run user process with
    36. # - locks - max number of file locks the user can hold
    37. # - sigpending - max number of pending signals
    38. # - msgqueue - max memory used by POSIX message queues (bytes)
    39. # - nice - max nice priority allowed to raise to values: [-20, 19]
    40. # - rtprio - max realtime priority
    41. # - chroot - change root to directory (Debian-specific)
    42. #
    43. #
    44. #
    45. * hard core unlimited
    46. * soft core unlimited
    47. * hard nofile unlimited
    48. * soft nofile unlimited
    49. * hard stack unlimited
    50. * soft stack unlimited
    51. * hard nproc unlimited
    52. * soft nproc unlimited
    53. #* soft core 0
    54. #root hard core 100000
    55. #* hard rss 10000
    56. #@student hard nproc 20
    57. #@faculty soft nproc 20
    58. #@faculty hard nproc 50
    59. #ftp hard nproc 0
    60. #ftp - chroot /ftp
    61. #@student - maxlogins 4
    62. # End of file
    63. #
    64. #
    65. zrd hard core unlimited
    66. 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连接的有关限制)。

    1. #
    2. # /etc/sysctl.conf - Configuration file for setting system variables
    3. # See /etc/sysctl.d/ for additional system variables.
    4. # See sysctl.conf (5) for information.
    5. #
    6. #kernel.domainname = example.com
    7. # Uncomment the following to stop low-level messages on console
    8. #kernel.printk = 3 4 1 3
    9. ##############################################################3
    10. # Functions previously found in netbase
    11. #
    12. # Uncomment the next two lines to enable Spoof protection (reverse-path filter)
    13. # Turn on Source Address Verification in all interfaces to
    14. # prevent some spoofing attacks
    15. #net.ipv4.conf.default.rp_filter=1
    16. #net.ipv4.conf.all.rp_filter=1
    17. # Uncomment the next line to enable TCP/IP SYN cookies
    18. # See http://lwn.net/Articles/277146/
    19. # Note: This may impact IPv6 TCP sessions too
    20. #net.ipv4.tcp_syncookies=1
    21. # Uncomment the next line to enable packet forwarding for IPv4
    22. #net.ipv4.ip_forward=1
    23. # Uncomment the next line to enable packet forwarding for IPv6
    24. # Enabling this option disables Stateless Address Autoconfiguration
    25. # based on Router Advertisements for this host
    26. #net.ipv6.conf.all.forwarding=1
    27. ###################################################################
    28. # Additional settings - these settings can improve the network
    29. # security of the host and prevent against some network attacks
    30. # including spoofing attacks and man in the middle attacks through
    31. # redirection. Some network environments, however, require that these
    32. # settings are disabled so review and enable them as needed.
    33. #
    34. # Do not accept ICMP redirects (prevent MITM attacks)
    35. #net.ipv4.conf.all.accept_redirects = 0
    36. #net.ipv6.conf.all.accept_redirects = 0
    37. # _or_
    38. # Accept ICMP redirects only for gateways listed in our default
    39. # gateway list (enabled by default)
    40. # net.ipv4.conf.all.secure_redirects = 1
    41. #
    42. # Do not send ICMP redirects (we are not a router)
    43. #net.ipv4.conf.all.send_redirects = 0
    44. #
    45. # Do not accept IP source route packets (we are not a router)
    46. #net.ipv4.conf.all.accept_source_route = 0
    47. #net.ipv6.conf.all.accept_source_route = 0
    48. #
    49. # Log Martian Packets
    50. #net.ipv4.conf.all.log_martians = 1
    51. #
    52. ###################################################################
    53. # Magic system request Key
    54. # 0=disable, 1=enable all
    55. # Debian kernels have this set to 0 (disable the key)
    56. # See https://www.kernel.org/doc/Documentation/sysrq.txt
    57. # for what other values do
    58. #kernel.sysrq=1
    59. ###################################################################
    60. # Protected links
    61. #
    62. # Protects against creating or following links under certain conditions
    63. # Debian kernels have both set to 1 (restricted)
    64. # See https://www.kernel.org/doc/Documentation/sysctl/fs.txt
    65. #fs.protected_hardlinks=0
    66. #fs.protected_symlinks=0
    67. ###################xiaogongwei###################
    68. net.ipv4.ip_local_port_range = 1024 65535
    69. net.core.rmem_max=16777216
    70. net.core.wmem_max=16777216
    71. net.ipv4.tcp_rmem=4096 87380 16777216
    72. net.ipv4.tcp_wmem=4096 65536 16777216
    73. net.ipv4.tcp_fin_timeout = 10
    74. net.ipv4.tcp_timestamps = 0
    75. net.ipv4.tcp_window_scaling = 0
    76. net.ipv4.tcp_sack = 0
    77. net.core.netdev_max_backlog = 30000
    78. net.ipv4.tcp_no_metrics_save=1
    79. net.core.somaxconn = 262144
    80. net.ipv4.tcp_syncookies = 0
    81. net.ipv4.tcp_max_orphans = 262144
    82. net.ipv4.tcp_max_syn_backlog = 262144
    83. net.ipv4.tcp_synack_retries = 2
    84. net.ipv4.tcp_syn_retries = 2

    步骤4、 执行如下命令(使上述设置生效):

    1. /sbin/sysctl -p /etc/sysctl.conf
    2. /sbin/sysctl -w net.ipv4.route.flush=1

    步骤5、执行如下命令(linux系统优化完网络必须调高系统允许打开的文件数才能支持大的并发,默认1024是远远不够的):

    1. echo "ulimit -HSn 65535" >> /etc/rc.local
    2. echo "ulimit -HSn 65535" >>/root/.bash_profile
    3. echo "ulimit -SHn 65535" >> /etc/profile
    4. ulimit -SHn 65535

    步骤6、重启机器

    通过修改,tcp可以达到65535个连接完全没有问题

    通过这个命令 ulimit -n 可以看到值改为65535了,也就是说现在最多支持65536个tcp socket连接了查看当前有多少个TCP连接到当前服务器命令:netstat -antp |grep -i est |wc -l

  • 相关阅读:
    Numpy 笔记——数组创建
    nvm下载安装教程
    Chrome解决“github.com拒绝了我们的访问请求”
    Maven多模块项目管理小结
    Python中的函数式编程是什么?
    java计算机毕业设计springboot+vue村委会管理系统
    Lua 调试库( debug )
    SpringMVC中文乱码(request或response)前后端处理
    第1章、温故而知新
    vite配置多页面应用
  • 原文地址:https://blog.csdn.net/xiaoxiao133/article/details/126575931