Zsh is a default shell on the Kali Linux system that allows you to run commands and perform your required tasks with ease. However, while working on Zsh in Kali Linux, you may encounter “zsh: permission denied: /etc/apt/sources.list” error while adding a repository to the system repository list. This type of error occurs when you try to modify the system sources.list file without sufficient privileges. It prevents you from adding the required repository, thus you won’t be able to install the required application or package on your system.
Worry not, read this guide for finding how to fix the “zsh: permission denied: /etc/apt/sources.list” error on Kali Linux system.
Quick Outline
- Why “zsh: permission denied: /etc/apt/sources.list” Error Occurs
- How to Fix “zsh: permission denied: /etc/apt/sources.list” Error on Kali Linux
- Conclusion
Why “zsh: permission denied: /etc/apt/sources.list” Error Occurs
When you add a repository with echo command followed by repository URL into the source repository with sudo privileges, you will encounter the following error:
This error occurs because users don’t have required permission to add the repository even with the sudo privileges. It is because the sources.list file can be modified only by the root user. You can use the ls command provide below to check for the necessary permission for sources.list file:
When you run the sudo command with echo mode, the echo command will run as a root but the command to append the repository to sources.list file is run as a normal user. Thus, due to conflict and requiring permission issues, you will experience that kind of error.
How to Fix “zsh: permission denied: /etc/apt/sources.list” Error on Kali Linux
To fix the “zsh: permission denied: /etc/apt/sources.list” error on Kali Linux, you can:
- Run tee Command with sudo Privileges
- Manually Add Repository Inside the sources.list File
- Create a Separate File
- Add a Repository as a Root User
- Create a Script
1: Run tee Command with sudo Privileges
The most straightforward approach to fix “zsh: permission denied: /etc/apt/sources.list” error is to run the echo command as a root user, and giving the sudo privileges to tee command, as shown below:
2: Manually Add Repository Inside the sources.list File
Another approach to fix the “zsh: permission denied: /etc/apt/sources.list” error is to open the sources.list file with root privileges. This allows you to edit the sources.list file, such as adding a repository according to your choice. You can open sources.list file on Kali Linux via nano editor from the provided command:
After the changes, you can save the file using CTRL+X, approve the changes using Y and close the file using the Enter button. Once done, update the repository on Kali Linux using the following command:
3: Create a Separate File
Besides editing the main source file, you can also fix the “zsh: permission denied: /etc/apt/sources.list” error by creating a separate file on Kali Linux. It can be done from the below-given touch command that creates a repository.list file inside the sources.list.d location:
Then adding the repository to Kali Linux source repository location by running echo as a root user and tee command with the sudo privileges:
By doing this process, you are leaving the main sources.list file alone, however, it will add the sources through an included file that will handle the added repository.
4: Add a Repository as a Root User
Besides adding the repository inside the sources.list file with root privileges, you can manually enter the system as a root user using the following command:
Then add the required repository using the echo command without giving sudo privileges:
Further, you can also manually add the repository as root user by using the sudo su command followed by the -c argument:
5: Create a Script File
You can also create a script file in Kali Linux, add the repository command, and then run the script to add the required repository to the source repository location. The complete process can be followed using the following steps:
Step 1: First, create a script file in Kali Linux with .sh extension using nano editor from the below command:
Inside the script file, first add the bash line:
Then add the repository with the echo command:
Once done, save the script file using CTRL+X, then add Y for approving the changes and press Enter to close the file:
Step 2: Then give execute permission to the script file you created in the previous steps, it can be done using the chmod command:
Step 3: Now, run the script file with sudo privileges to successfully add the repository to the system:
In this way, you can fix the “zsh: permission denied: /etc/apt/sources.list” error on Kali Linux system.
Conclusion
“zsh: permission denied: /etc/apt/sources.list” error occurs in a system because a normal user doesn’t have permission to make changes to the source repository file. If you are using the Kali Linux system and encounter this error, you can fix it by running the tee command with sudo privileges. Besides that, you can manually add a repository or consider a separate file for adding repository. Alternatively, you can also add the repository by login as a root user or create a script, add the repository and execute the script to successfully add the repository to the Kali Linux system. A detailed description of all five methods are presented in the above-section of this guide.
By day, I am an engineer and researcher, building bridges and figuring out how the universe works. But when the clock strikes five, I turn into a wordsmith!
I write about my adventures on my blog, sharing tips and tricks to help others join the tech fun. I have worked on Raspberry Pi, Ubuntu, Debian, and Laptops. Right now, I have accepted the challenge of working on the Kali Linux system, and with my vast expertise in Linux systems, I will help users overcome the challenges through my blogs.