I know that its possible to chat in linux terminal using netcat. I want to know whether it is possible to encrypt the netcat communications.

I did chat by listening on PC-1

nc -l 1234

And connecting to my IP on the other machine.

nc $IP 1234
  • You could try something like echo message | zip -e | nc $IP 1234 on one side, and nc -l 1234 | unzip at the other end. Don't know if that will work though.
    – Jos
    Mar 12, 2015 at 7:55
  • 1
    possible duplicate of Secure Chat Tunnel Mar 12, 2015 at 8:58
  • I dont want to ssh to my remote machine.Some other method was my preference. Mar 12, 2015 at 9:28

2 Answers 2


It's possible - however I don't think nc does this itself: echo "Words" | gpg -e will produce an encrypted version on stdout; you can specify a receiving user as per usual.
If you pipe this to another copy of gpg as gpg -d then it asks for a passphrase - this will be remembered for a period, so enabling a conversation.

Therefore, echo "words" | gpg -e | nc target 4321 will send, and nc -l 4321 | gpg -d will listen.

Also, see this question which is similar.

  • Netcat is also 2 way. So would an interactive session be gpg -e | nc target 4321 | gpg -d? Mar 24, 2016 at 5:08
  • @CMCDragonkai It makes sense, however gpg seems not suitable for interactive because it seems waiting for the whole input to finish before writing the output.
    – fikr4n
    Sep 14, 2016 at 14:36
  • Using gpg -c is probably more convenient for one-time transfers (it encrypts message with symmetric cipher using given passphrase). The SSH tunnel solution is great too but connecting through SSH is sometimes not possible/desirable.
    – Tey'
    Apr 28, 2020 at 0:23

By itself netcat doesn't have encryption or authentication controls so while the traffic could be encrypted via OpenSSL or GnuPG with some clever piping and a bit if loops on the listening side, if you're on an untrustworthy network you might lose the race-condition to read using it and netcat doesn't allow multiple clients without clever scripting... but I'm not here to bash a tool but instead notify you of a Bash tool ;-)

Note it's very experimental and you should really check out the Travis-CI build log to find out exactly what it does, hint open a second window/tab and follow along with the travis.yml enabled scripts to see every working/tested feature so far developed.

Second hint, the following command examples are better in my experience with encrypting random strings.

Var_gpg_opts="--armor --batch --no-tty --recipient user@host.domain --encrypt"
## Output to terminal
cat <<<"${Var_input}" | gpg ${Var_gpg_opts}
## Save output to file
cat <<<"${Var_input}" | gpg ${Var_gpg_opts} >> "${Var_log_file}"

However, decryption of multi-armored output file is a bit trickier... so here's the link to the script written for that because it's a bit too long to post here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.