Ttyd is a simple tool that allows you to share a Linux terminal over a web browser. It’s cross-platform and uses the libuv and WebGL2 Javascript APIs for performance and real-time connection between the browser and Linux terminal. The SSL support is offered by OpenSSL.
It supports multiple clients at once, allowing multiple clients to connect to the same Linux terminal from different browsers (or tabs). No additional package is required on the client-side, only the correct web address is needed.
You can use this tool for remote system administration, troubleshooting, assisting a friend with any specific task, debugging or recovering a process, and much more.
In this article, I’ll show you how to install ttyd on Linux and then use it to access your Linux terminal from a web browser.
Tutorial Details
Description | Ttyd: Share Your Linux Terminal Over Web Browser |
Difficulty Level | Low |
Root or Sudo Privileges | Yes (for installation) |
OS Compatibility | Ubuntu, Fedora, Windows, macOS, etc. |
Prerequisites | – |
Internet Required | Yes |
How to Install Ttyd on Linux
Most Linux distributions allow for quick installation using their default package manager. Therefore, if you’re on Debian, Ubuntu, Fedora, Arch, Manjaro, etc., you can use one of the following commands to install ttyd based on your Linux distribution.
# On Debian, Ubuntu, Mint, Pop!_OS, etc.
$ sudo apt install ttyd
# On RHEL, CentOS, Fedora, Rocky Linux, AlmaLinux, etc.
$ sudo dnf install ttyd
# On Arch, Manjaro, EndeavourOS, Garuda, etc.
$ sudo pacman -S ttyd
If you prefer using sandbox technology like Snap or a universal package manager like HomeBrew (which works for macOS too), you can use one of the appropriate commands for installation.
# Via Snap
$ sudo snap install ttyd --classic
# Via Brew (for Linux and macOS)
$ brew install ttyd
Finally, for installing ttyd on Debian/Ubuntu Linux via source compilation, you can use the following series of commands:
# Update the package information and install necessary development tools.
$ sudo apt update
$ sudo apt install build-essential cmake git libjson-c-dev libwebsockets-dev
# Clone the git repository and create a separate build directory.
$ git clone https://github.com/tsl0922/ttyd.git
$ cd ttyd && mkdir build && cd build
# Generate CMakefiles in the directory and compile ttyd from source.
$ cmake ..
$ sudo make && sudo make install
Once the installation is complete, the “ttyd
” command becomes accessible.
How to Use Ttyd to Share Linux Terminal Over the Web
To start sharing your Linux terminal, you can use the “ttyd
” command along with the “-p
” option, which accepts web ports as a parameter to listen for incoming connections (in this case, “8080“).
$ ttyd -p 8080 bash
Output:
If you encounter the following error message when sharing the Linux terminal via ttyd, either opt for an alternative web port or find and terminate the process linked to the specified port (or use the Killport tool).
[2024/05/13 14:11:02:9211] N: ttyd 1.6.3 (libwebsockets 4.0.20)
[2024/05/13 14:11:02:9212] N: tty configuration:
[2024/05/13 14:11:02:9212] N: start command: bash
[2024/05/13 14:11:02:9212] N: close signal: SIGHUP (1)
[2024/05/13 14:11:02:9212] N: terminal type: xterm-256color
[2024/05/13 14:11:02:9213] N: Using foreign event loop...
[2024/05/13 14:11:02:9214] E: ERROR on binding fd 11 to port 8080 (-1 98)
[2024/05/13 14:11:02:9215] E: init server failed
[2024/05/13 14:11:02:9215] E: libwebsockets vhost creation failed
Next, you can head over to your web browser and input the IP address or hostname of the system whose Linux terminal you’re sharing. If accessing from a different network, you may need to set up port forwarding for the listening port (in our case, “8080“) from the router page.
However, if you’re accessing within the same network, you can use the local IP or the localhost with the listening port number to access the Linux terminal in the browser.
http://localhost:8080/
Output:
Manage Ttyd Systemd Service on Linux
When you install ttyd via the package manager, it will create a systemd service, simplifying service management tasks like starting, stopping, and auto-starting the service at system boot.
The ttyd service is typically enabled, so each time your system boots up, ttyd services automatically start, sharing your Linux terminal with a random web port. To check if it’s enabled and running in the background on your system, use the following systemctl command:
# Verify if the ttyd service is configured to automatically start with system boot.
$ sudo systemctl is-enabled ttyd
# Verify whether the ttyd service is currently running in the background.
$ sudo systemctl status ttyd
Output:
If the ttyd service is running in the background and you intend to keep it that way but are unable to locate the listening port, you can use the following ps command, along with grep, to find out the web port ttyd is using to listen for incoming connections.
$ ps -aux | grep -v "grep" | grep ttyd
Output:
If you haven’t installed ttyd via the package manager and can’t find a ttyd systemd service but want to create one for easier management, open your terminal, use your preferred text editor, either Vim or Nano, and create the systemd service file.
$ sudo nano /lib/systemd/system/ttyd.service
# OR
$ sudo vim /lib/systemd/system/ttyd.service
Add the following lines:
[Unit]
Description=ttyd_linuxtldr.com
After=network.target systemd-tmpfiles-clean.service
[Service]
Type=simple
ExecStart=/bin/bash -c "ttyd login"
KillMode=process
LimitNOFILE=512
LimitMEMLOCK=infinity
LimitSTACK=infinity
SyslogIdentifier=ttyd
[Install]
WantedBy=multi-user.target
Save and close the file, then execute the following series of commands to configure the new changes to the systemd services.
# Reload the systemd.
$ sudo systemctl daemon-reload
# Stop any existing ttyd process.
$ sudo killall -9 ttyd
# Enable and restart the ttyd service.
$ sudo systemctl enable ttyd
$ sudo systemctl restart ttyd
That’s it,
Secure Ttyd by Enabling HTTPS SSL Certificate
To securely access your Linux terminal via ttyd using the HTTPS protocol, you’ll need to first obtain a free Let’s Encrypt SSL certificate for your domain. To do this, you can install the certbot tool and request a certificate for your domain directly from your Linux terminal.
# Run the command corresponding to your Linux distribution to install certbot.
$ sudo apt install certbot
$ sudo dnf install certbot
$ sudo pacman -S certbot
# Request the free Let’s Encrypt SSL certificate for your domain.
$ certbot certonly -d <your domain>
You’ll now be able to run ttyd on HTTPS:
$ ttyd --ssl --ssl-cert /etc/letsencrypt/live/<your domain>/fullchain.pem --ssl-key /etc/letsencrypt/live/<your domain>/privkey.pem <command>
Final Word
In this article, you learned how to install ttyd on Linux, manage ttyd systemd services, find out the ttyd default port, create a ttyd systemd service, and access the Linux terminal via the HTTPS protocol.
Now, if you have any questions or queries related to the topic, then do let us know in the comment section.
Till then, peace!
Good article.
╰(*°▽°*)╯
The ttyd package in Fedora became orphaned and dropped with Fedora 34. It is not packaged in RHEL/clones nor EPEL… so unfortunately, one can not use the instructions shown in this article to install on Red Hat-based systems.
Thanks for bringing this to our attention and will investigate, updating the article soon.
I am having some issues running ttyd as a daemon under Linux Mint. When I start it as a daemon, it listens to port 7681. Fine with this port. However, when I point my browser to localhost:7681 (7681 being the port picked up by ttyd), I get an error on my browser.
However, when I manually start it with explicitly specidying the port as 8080, it works just fine. I do not have anything else on port 7681. What can be the problem ? How can I change the listening port in the config file when I want to start it as a service ?
Use the “
sudo ss -tulpn | grep 7681
” command to check the associated service with the port.