title: Mosquitto increase max connections
body: <p>I install mosquitto, and have changed a lot of setting in </p>
  
  <pre><code>/etc/security/limits.conf
  /etc/sysctl.cnof
  /etc/pam.d/common-session
  /etc/pam.d/common-session-noninteractive
  </code></pre>
  
  <p>Detail changed:</p>
  
  <pre><code>$ sudo vim /etc/sysctl.conf
  
  fs.file-max=2097152
  fs.nr_open=2097152
  net.core.somaxconn=32768
  net.ipv4.tcp_max_syn_backlog=16384
  net.core.netdev_max_backlog=16384
  net.ipv4.ip_local_port_range='1025 65535'
  net.core.rmem_default=262144
  net.core.wmem_default=262144
  net.core.rmem_max=16777216
  net.core.wmem_max=16777216
  net.core.optmem_max=16777216
  net.ipv4.tcp_rmem='1024 4096 16777216'
  net.ipv4.tcp_wmem='1024 4096 16777216'
  net.ipv4.tcp_max_tw_buckets=1048576
  net.ipv4.tcp_fin_timeout=15
  
  $ sudo vim /etc/security/limits.conf
  
  *       hard    nofile  1048576
  *       soft    nofile  1048576
  *       hard    nproc   1048576
  *       soft    nproc   1048676
  
  $ sudo vim /etc/pam.d/common-session
  $ sudo vim /etc/pam.d/common-session-noninteractive
  
  session required pam_limits.so
  </code></pre>
  
  <p>But after done this, mosquitto can still support 1024 connections.</p>
  
  <p>So I use <code>cat /etc/{pid}/limits</code> to check the mosquitto's limits</p>
  
  <p>Then I found that mosquitto's limit is still the default</p>
  
  <pre><code>Max processes             7968                 7968      processes
  Max open files            1024                 4096      files
  </code></pre>
  
  <p>I figured out the mosquitto is nologin user, and cannot trigger the PAM limit configuration. And if I use <code>prlimit</code> command to increase the soft openfile limit, the mosquitto can indeed increase the max connections, but is limited by hard limit (4096).</p>
  
  <p>Seems the debian still cannot do this (<a href="https://superuser.com/questions/454465/make-ulimits-work-with-start-stop-daemon">Reference</a>), so a possible solution is to use <code>/etc/init/mosquitto.conf</code></p>
  
  <p>I do changed the <code>/etc/init/mosquitto.conf</code> but it seems not working</p>
  
  <pre><code>$ sudo vim /etc/init/mosquitto.conf    
  limit nofile 1048576 1048576
  limit nproc  1048576 1048576
  ulimit -n 1048576 # I do both command, but neither do they works
  ulimit -u 1048576
  </code></pre>
  
  <p>Is anyone have any idea to make mosquitto support large amount of connections?</p>
  
  <p>Thanks</p>
  
  <p>PS. test tool: <a href="https://github.com/emqtt/emqtt_benchmark" rel="nofollow noreferrer">emqtt benchmark</a></p>