Of course you can. But you can do about as much on them, as you could do with a computer having the same specs. Look up your router’s CPU architecture¹ and speed, the amount of ram, and what kinds of permanent storage (internal flash memory, USB stick, USB HDD) it supports.
Basically, just treat it like any other headless PC.
The only problem is, to get a command-line, so you can install something, you need to boot your own environment. And the boot loaders of those routers usually aren’t configurable.
But they all allow flashing a new firmware².
So the trick is, to either find a pre-made (unofficial/custom) firmware that allows you to ssh³ in and re-flash in case you want to undo everything… or to make/modify your own firmware image*.
As soon as you have any sort of Linux command line, everything else goes as usual. The most elegant path is to install Gentoo. As it, being a meta-distribution, is extremely adaptable. Basically it’s made for exactly these cases. And in the process you learn how to actually use a computer. That is very important. Because if you don’t (like with Windows/OSX/Ubuntu/KDE), and you get to even the tiniest problem that doesn’t have a colorful clickable, you’re stuck.
Make sure you use the same kernel configuration, drivers, etc as that firmware image though.
- You can also make the firmware image with Gentoo, if you decide to create your own. But in that case, you have to find out which hardware your router actually consists of. Which CPU, which buses, which controllers. Apart from already having a command line, the only way to do that, is to open the thing, look at the text on the chips, and find their specs online.
But don’t attempt to flash that, until you are sure you get either a bootable system with SSH or with re-flashing support. Otherwise you can’t go back. Which is kinda bad, if you soft-bricked the device. ;) (Unless you want to unsolder the flash memory chip, solder it into an USB stick with a compatible matching controller, and re-flash it that way, or something like that. ;)
But usually, things lite Tomato⁴ (or its predecessor DD-WRT) save you from this step… provided you have a suitable router.
If you have a Linux kernel running, and a package management system available (every distribution does), all limitations are gone. Just install whatever you like and the hardware can handle. :)