user70388: I have started bitcoin daemon with -regtest flag. The output was 

Bitcoin server starting

But, when I run
bitcoin-cli -regtest generate 101

command, it shows 

error: couldn't connect to server: unknown (code -1) (make sure server
  is running and you are connecting to the correct RPC port)

How this happens?

user80262: Try changing port number in bitcoin.conf.
open file
nano ~/.bitcoin/bitcoin.conf

add new line

rpcport=18332;

save the file and restart bitcoind

user65182: Adding rpcport=18332 temporarily solve your problem. If you want to run mainnet or testnet at the same time when regtest is running, you wont be able to use them, it is because when you assign rpcport=xyz all  bitcoin instances will try to interact through same rpcport when daemon is started but at one time only single daemon can access single port number.But in your case all three daemons tries to take rpcport as 18332.
Infact what you can do is, try to get connected to default rpc port for regtest by typing command:

bitcoind -regtest -daemon -rpcport=18443

Mainnet:8332 
Testnet:18332 
Regtest:18443`

Note:For bitcore v15 rpc port is same for testnet and mainnet where as in v16 rpc port is diffrent for all of them

70388: <p>I have started bitcoin daemon with -regtest flag. The output was </p>

<blockquote>
  <p>Bitcoin server starting</p>
</blockquote>

<p>But, when I run</p>

<pre><code>bitcoin-cli -regtest generate 101
</code></pre>

<p>command, it shows </p>

<blockquote>
  <p>error: couldn't connect to server: unknown (code -1) (make sure server
  is running and you are connecting to the correct RPC port)</p>
</blockquote>

<p>How this happens?</p>
 80262: <p>Try changing port number in bitcoin.conf.
open file</p>

<pre><code>nano ~/.bitcoin/bitcoin.conf
</code></pre>

<p>add new line</p>

<blockquote>
  <p>rpcport=18332;</p>
</blockquote>

<p>save the file and restart bitcoind</p>
 65182: <p>Adding <em>rpcport=18332</em> <strong>temporarily</strong> solve your problem. If you want to run <strong>mainnet</strong> or <strong>testnet</strong> at the same time when <strong>regtest</strong> is running, you <em>wont be able to use them</em>, it is because when you assign <code>rpcport=xyz</code> all  bitcoin instances will try to interact through same rpcport when daemon is started but at one time only single daemon can access single port number.But in your case all three daemons tries to take rpcport as 18332.</p>

<p>Infact what you can do is, try to get connected to default rpc port for regtest by typing command:</p>

<blockquote>
  <p>bitcoind -regtest -daemon -rpcport=18443</p>
</blockquote>

<pre><code>Mainnet:8332 
Testnet:18332 
Regtest:18443`
</code></pre>

<p><em>Note:For bitcore v15 rpc port is same for testnet and mainnet where as in v16 rpc port is diffrent for all of them</em></p>
