"Too many levels of symbolic links": find the loop, and recreate the link properly
Fix "Too many levels of symbolic links" on Linux: find the loop with namei and find -L, recreate the link with a correct path, and handle Docker mounts.
by OhSudo ··
cat: /etc/nginx/sites-enabled/mysite: Too many levels of symbolic linksA symbolic link points to a path, and the system follows it, and follows the next link if there is one. It gives up after about 40 links, with this error. In practice, it almost never means a long chain. It means a loop: the link points to itself, or two links point to each other.
And nearly always, the link was created with a relative path that didn’t mean what you thought.
- sites-enabled/mysitea symlinkfollows(fails)
- Its targetpoints back to itself(breaks here)until
- Too many levelsabout 40 links followed
TL;DR
namei -l /etc/nginx/sites-enabled/mysitesudo rm /etc/nginx/sites-enabled/mysitesudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysitenamei shows each step of the path, so you can see the loop. Then remove the link, and create it again with a full path.
1. See the loop
You can follow along in the terminal, where a site was enabled this way:
lrwxrwxrwx 1 root root 6 Nov 12 10:00 /etc/nginx/sites-enabled/mysite -> mysiteA link called mysite that points to mysite, in the same folder, is pointing at itself. namei follows every link on the way, and makes the problem plain:
namei -l /etc/nginx/sites-enabled/mysite2. Why this happens: relative targets
ln -s TARGET LINK stores the target exactly as you type it. When it’s used, a relative target is read from the folder that holds the link, not from where you were when you created it.
So this, run from /etc/nginx/sites-available, creates a link that points to itself:
cd /etc/nginx/sites-availablesudo ln -s mysite /etc/nginx/sites-enabled/mysiteThe link sits in sites-enabled, and says mysite, which means sites-enabled/mysite: itself. The fix is to give the full path:
sudo rm /etc/nginx/sites-enabled/mysitesudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysitecat /etc/nginx/sites-enabled/mysiteOr a relative path that’s correct from the link’s folder: ../sites-available/mysite. With GNU ln, ln -sr works it out for you.
Removing a link: use rm on the link itself, with no slash at the end. rm -r link/ on a link to a folder can delete what’s inside the target.
3. Two links pointing at each other
a -> bb -> aThis happens with deployment scripts that switch a current link between releases, and get it wrong. ls -l on both shows it. Remove one, and point it at a real file or folder.
4. Find every loop under a folder
List more folders after the first one to search them too. With -L, find follows links, so any that are still links at the end are broken. Loops show up with this message:
find: '/etc/nginx/sites-enabled/mysite': Too many levels of symbolic links5. In Docker
A bind mount of a path that’s a symlink can loop inside the container, where the target path means something else, or doesn’t exist. Mount the real folder rather than the link:
readlink -f ./configdocker run -v "$(readlink -f ./config)":/app/config myimageA COPY in a Dockerfile copies a link as a link, so a link pointing outside the build folder ends up broken in the image. Copy the real files into the build folder instead.
On NFS and autofs mounts, this message can also appear for a share that’s misconfigured or a stale mount, with no loop in sight. Check mount | grep -E 'nfs|autofs', and remount it.
Warning
Before removing anything, make sure it’s a link: ls -l shows an l at the start, and ->. Running rm -rf on a path you think is a link, and isn’t, deletes real data. Also check what points at the link before you change it, as services may depend on it.
Shell, we have a problem!
Similar, but different: No such file or directory on a link that exists means a broken link, whose target is missing. nginx -t often reports this error when a site link is wrong, as a failed open() of the file. Not a directory means a part of the path is a file. And Permission denied means the path works, and you’re not allowed in.
$ ./practice.sh
Practice this in the terminal
Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.