Skip to content
ohsudo, Home
Linux

"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.

Beginner3 min readApplies to Linux · macOS · Docker · any Unix

by OhSudo ··

cat: /etc/nginx/sites-enabled/mysite: Too many levels of symbolic links

A 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.

  1. sites-enabled/mysitea symlink
  2. Its targetpoints back to itself(breaks here)
  3. Too many levelsabout 40 links followed
The system follows a link, then the next one, and gives up after about 40. That nearly always means a loop, usually from a relative path that didn't mean what you thought.

TL;DR

Terminal window
namei -l /etc/nginx/sites-enabled/mysite
sudo rm /etc/nginx/sites-enabled/mysite
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite

namei 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:

Terminal window
ls -l /etc/nginx/sites-enabled/mysite
readlink /etc/nginx/sites-enabled/mysite
lrwxrwxrwx 1 root root 6 Nov 12 10:00 /etc/nginx/sites-enabled/mysite -> mysite

A 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:

Terminal window
namei -l /etc/nginx/sites-enabled/mysite

2. 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:

Terminal window
cd /etc/nginx/sites-available
sudo ln -s mysite /etc/nginx/sites-enabled/mysite

The link sits in sites-enabled, and says mysite, which means sites-enabled/mysite: itself. The fix is to give the full path:

Terminal window
sudo rm /etc/nginx/sites-enabled/mysite
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite
cat /etc/nginx/sites-enabled/mysite

Or 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.

a -> b
b -> a

This 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

Terminal window
find -L /etc/nginx -type l

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 links

5. 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:

Terminal window
readlink -f ./config
docker run -v "$(readlink -f ./config)":/app/config myimage

A 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.

Open the terminal →

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles