Alpine Linux share the terminal over the web (ttyd).png
Alpine Linux share the terminal over the web (ttyd).png

What is ttyd?
ttyd is a sim­ple com­mand-line pro­gram that al­lows you to share a ter­mi­nal ses­sion over the web. It pro­vides a web-based ter­mi­nal in­ter­face for in­ter­act­ing with a Linux sys­tem, mak­ing it pos­si­ble to ac­cess the sys­tem re­motely from a web browser.

Install ttyd in Alpine Linux

Open the ter­mi­nal and type the fol­low­ing com­mand to up­date the pack­ages list:

apk update

In­stall the ttyd pack­age by typ­ing the fol­low­ing com­mand:

apk add ttyd

Once the in­stal­la­tion is com­plete, you can use the ttyd com­mand to start the ttyd server.

For ex­am­ple, to start the ttyd server on port 7681, use the fol­low­ing com­mand:

ttyd -p 7681 /bin/sh

This com­mand will start the ttyd server and run the /bin/sh shell in it.

You can now ac­cess the ttyd server by open­ing a web browser and nav­i­gat­ing to http://<ip_address>:7681.

Re­place <ip_address> with the IP ad­dress of the server where the ttyd server is run­ning.

That's it! You have now in­stalled and started the ttyd server on Alpine Linux.


Make ttyd start-up on boot

Cre­ate a new file in the /etc/init.d/ di­rec­tory us­ing your pre­ferred text ed­i­tor. For ex­am­ple, you can use the fol­low­ing com­mand to cre­ate a file named ttyd:

vi /etc/init.d/ttyd

Add the fol­low­ing con­tent to the ttyd file:

#!/sbin/openrc-run

description="ttyd service"
command="/usr/bin/ttyd -p 7681 login &"
command_args=""
pidfile="/run/ttyd.pid"

depend() {
    after network
}

start_pre() {
    mkdir -p /run/ttyd
}
OptionDescription
/usr/bin/ttydSpecifies the path to the ttyd binary.
-p 7681Specifies the port number that ttyd should listen on for incoming connections. In this case, it is set to 7681 (default).
loginSpecifies the command to run after a user connects to ttyd. In this case, it is set to login, which starts a new login session.
&Runs the command in the background, allowing the terminal to be used for other commands while ttyd continues to run.

Cre­ates a new ser­vice de­f­i­n­i­tion for ttyd, defin­ing the com­mand to run and the PID file lo­ca­tion.

Ad­just the command line to use your de­sired op­tions for ttyd.

Save and exit the file.

Make the ttyd script ex­e­cutable us­ing the fol­low­ing com­mand:

chmod +x /etc/init.d/ttyd

Add the ttyd ser­vice to the startup list us­ing the fol­low­ing com­mand:

rc-update add ttyd
  • service ttyd added to runlevel default

This com­mand adds the ttyd ser­vice to the de­fault run­level, so it will start up au­to­mat­i­cally on boot.

That's it! You have now con­fig­ured ttyd to start up on boot in Alpine Linux.


Conclusion

ttyd, which al­lows users to in­ter­act with the shell ses­sion in real-time us­ing a web-based ter­mi­nal in­ter­face.
ttyd also is a use­ful tool for re­mote ad­min­is­tra­tion and trou­bleshoot­ing of Linux sys­tems.


Related