Search code examples
c++cremote-debuggingfreebsdclion

CLion Full Remote Mode with FreeBSD as the remote host


Currently, the Full Remote Mode of CLion only supports Linux as a remote host OS. Is it possible to have a FreeBSD remote host?


Solution

  • Yes, you can!

    However, note that I'm recalling these steps retrospectively, so probably I have missed one step or two. Should you encounter any problem, please feel free to leave a comment below.

    1. Rent a FreeBSD server, of course :)

    2. Update your system to the latest release. Otherwise, you may get weird errors like "libdl.so.1" not found when installing packages. The one I'm using is FreeBSD 12.0-RELEASE-p3.

    3. Create a user account. Don't forget to make it a member of wheel, and uncomment the %wheel ALL=(ALL) ALL line in /usr/local/etc/sudoers.

    4. Set up SSH. This step is especially tricky, because we need to use both public-key and password authentication.

      1. Due to a known bug, in some cases, the remote host must use password authentication, or you'll get an error when setting up the toolchain. You can enable password authentication by setting PasswordAuthentication yes in /etc/ssh/sshd_config, followed by a sudo /etc/rc.d/sshd restart.
      2. It appears that CLion synchronizes files between the local and remote host with rsync and SSH. For some reasons I cannot explain, this process will hang forever if the host server doesn't support passphrase-less SSH key login. Follow this answer to create an SSH key as an additional way of authentication.
    5. CLion assumes the remote host OS to be Linux, so we must fix some incompatibilities between GNU/Linux and FreeBSD.

      1. Install GNU utilities with sudo pkg install coreutils.
      2. Rename the BSD utility stat with sudo mv /usr/bin/stat /usr/bin/_stat.
      3. Create a "new" file /usr/bin/stat with the content in Snippet 1. This hack exploits the fact that CLion sets the environment variable JETBRAINS_REMOTE_RUN to 1 before running commands on the remote server.
      4. Do sudo chmod a+x /usr/bin/stat to make it executable.
      5. Again, rename the BSD utility ls with sudo mv /bin/ls /bin/_ls.
      6. Create a "new" file /bin/ls with the content in Snippet 2, like before.
      7. Lastly, sudo chmod a+x /bin/ls.
    6. Install the dependencies with sudo pkg install rsync cmake gcc gdb gmake.

    7. Now you can follow the official instructions, and connect to your shiny FreeBSD host!

    enter image description here


    Snippet 1

    #!/bin/sh
    if [ -z "$JETBRAINS_REMOTE_RUN" ]
    then
            exec "/usr/bin/_stat" "$@"
    else
            exec "/usr/local/bin/gnustat" "$@"
    fi
    

    Snippet 2

    #!/bin/sh
    if [ -z "$JETBRAINS_REMOTE_RUN" ]
    then
            exec "/bin/_ls" "$@"
    else
            exec "/usr/local/bin/gls" "$@"
    fi