Articles I've written for customers on IT issues.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.3 KiB

  1. \documentclass[11pt]{article}
  2. %Gummi|065|=)
  3. \title{\textbf{AutoSSH - a Reverse Proxy Alternative}}
  4. \usepackage{xcolor}
  5. \usepackage[vcentering,dvips]{geometry}
  6. \geometry{papersize={6in,9in},total={4.5in,6.8in}}
  7. \usepackage{graphicx}
  8. \usepackage{caption }
  9. \author{Steak Electronics}
  10. \date{06/4/19}
  11. \begin{document}
  12. %\maketitle
  13. \textbf{AutoSSH - a Reverse Proxy Alternative}
  14. \vspace{0.2in}
  15. This document is best read printed out on paper.
  16. %\textbf{Todo}
  17. \textcolor{green!60!blue!70}{
  18. \section{Overview}}
  19. I recently added another apache server to an existing infrastructure, and I wanted it to be accessible under a similar IP as another server. Due to the complexity of the website, it was not possible to simply do a reverse proxy without knowing the correct settings (e.g. X-Forwarded for). Instead, AutoSSH was used.
  20. \textcolor{green!60!blue!70}{
  21. \section{Work Log}}
  22. Ok, I'm going to get right to the configs that I used. You want the tool, you don't need to know all the details.
  23. \textcolor{green!60!blue!70}{
  24. \subsection{Crontab}}
  25. Here is the crontab script I used. I put this in /etc/crontab, so it has root after the times. I only use /etc/crontab, as it's easier to manage.
  26. \begin{verbatim}
  27. * * * * * root pgrep autossh > /dev/null || \
  28. /usr/local/bin/autosshzm/autosshzm.sh
  29. \end{verbatim}
  30. A few notes about this. Pgrep will search for autossh. If it doesn't find it, then it will try the next command. (|| is an OR). Put the bash script wherever you want.
  31. \textcolor{green!60!blue!70}{
  32. \subsection{Bash Script}}
  33. This script is obviously what the crontab calls.
  34. \begin{verbatim}
  35. #!/bin/bash
  36. logger " /usr/local/bin/autosshzm script started."
  37. #source $HOME/.bash_profile #not needed.
  38. source $HOME/.keychain/$HOSTNAME-sh
  39. logger " /usr/local/bin/autosshzm sourced."
  40. autossh -L 0.0.0.0:2:localhost:80 -f user@ipaddress sleep 31536000
  41. &> /var/log/autosshzm/autosshzm.log
  42. #autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3"
  43. -L 0.0.0.0:2:localhost:80 user@ipaddress &>
  44. /var/log/autosshzm/autosshzm.log
  45. logger "auto ssh ran"
  46. \end{verbatim}
  47. Note that the second autossh does not work, as it's missing the sleep and the -f command. \footnote{Figuring this kind of stuff out can take about an hour.} In order for this to work, you'll also need the following commands:
  48. \begin{verbatim}
  49. apt-get install keychain autossh
  50. \end{verbatim}
  51. There were some more setup steps required for keychain...
  52. From stackexchange:
  53. \begin{verbatim}
  54. 25
  55. keychain
  56. solves this in a painless way. It's in the repos for Debian/Ubuntu:
  57. sudo apt-get install keychain
  58. and perhaps for many other distros (it looks like it originated
  59. from Gentoo).
  60. This program will start an ssh-agent if none is running, and
  61. provide shell scripts that can be sourced and connect the current
  62. shell to this particular ssh-agent.
  63. For bash, with a private key named id_rsa, add the following to
  64. your .profile:
  65. keychain --nogui id_rsa
  66. This will start an ssh-agent and add the id_rsa key on the first
  67. login after reboot. If the key is passphrase-protected, it will
  68. also ask for the passphrase. No need to use unprotected keys
  69. anymore! For subsequent logins, it will recognize the agent
  70. and not ask for a passphrase again.
  71. Also, add the following as a last line of your .bashrc:
  72. . ~/.keychain/$HOSTNAME-sh
  73. This will let the shell know where to reach the SSH agent managed
  74. by keychain. Make sure that .bashrc is sourced from .profile.
  75. However, it seems that cron jobs still don't see this. As a
  76. remedy, include the line above in the crontab, just before
  77. your actual command:
  78. * * * * * . ~/.keychain/$HOSTNAME-sh; your-actual-command
  79. \end{verbatim}
  80. The only thing that I needed to do here was
  81. keychain --nogui id\_rsa
  82. The rest of it (notes about crontab) was not required.
  83. \textcolor{green!60!blue!70}{
  84. \section{What Did NOT Work}}
  85. Here's some things I tried that did not work.
  86. \begin{itemize}
  87. \item https://github.com/obfusk/autossh-init - This init script, didn't do much for me. Remember, I'm stuck with systemd in Ubuntu 19.04...\footnote{The scourge of deleting software history. Keep backwards compatibility at ALL COSTS, developers.}
  88. \item Reverse proxy with Apache - As I said, my website \footnote{Some people might call it a web application. I will not.} was too complex, and I didn't want to go down that rabbit hole.
  89. \item Starting AutoSSH in rc.local. Didn't work.
  90. \end{itemize}
  91. \end{document}