[Solved] For which raison sudo could not work

Archived topics about LMDE 1 and LMDE 2
Locked
Karakoram

[Solved] For which raison sudo could not work

Post by Karakoram »

I try to write a file in etc/apt/source.list.d

For that i do

Code: Select all

sudo echo 'what i want to right in the file' > /etc/apt/source.list.d/my_file_name
and the terminal told me : you dont have permission.

Somebody could explain to me why the super user is not working ?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
jimallyn
Level 19
Level 19
Posts: 9075
Joined: Thu Jun 05, 2014 7:34 pm
Location: Wenatchee, WA USA

Re: For which raison sudo could not work

Post by jimallyn »

Are you trying to add additional repositories? Why not use Software Sources from the Admin menu?
“If the government were coming for your TVs and cars, then you'd be upset. But, as it is, they're only coming for your sons.” - Daniel Berrigan
Karakoram

Re: For which raison sudo could not work

Post by Karakoram »

Because the install instruction was just like that.

Also i want to learn.
User avatar
Fred Barclay
Level 12
Level 12
Posts: 4185
Joined: Sat Sep 13, 2014 11:12 am
Location: USA primarily

Re: For which raison sudo could not work

Post by Fred Barclay »

I don't know the reasoning behind it, but sudo just doesn't work for this case. This used to trip me up - as it did you - when adding repos. I can't find any other "limitations" of sudo so I'm not sure if this is an unintentional bug or a misunderstanding on my part as to a feature of sudo.

Anyways, there are two other ways you can accomplish the same thing:
1. Switch user to root (don't stay as root any longer than you have to - in this case, only long enough to add the repo)

Code: Select all

sudo su
echo 'what i want to right in the file' > /etc/apt/sources.list.d/my_file_name
2. Run the original code through sh

Code: Select all

sudo sh -c "echo 'what i want to right in the file' > /etc/apt/sources.list.d/my_file_name"
Now you mention wanting to learn, so a couple of things to point out here: :)
Most importantly, don't just run random code in your terminal, especially as root (with sudo or su, or any variants like gksudo). The terminal is one of, if not the, most powerful tools in desktop Linux. With it you can make clean, surgical incisions and stitch your system back together elegantly, or you can bludgeon it beyond all recognition. Be careful, know what you type, and always think twice!

Close behind it - don't add random repositories to Mint/LMDE. You have a nice clean system, free of malware and nasties. By adding a repo, you add the potential for abuse. So think carefully and don't add the repository if you don't trust the provider!

Also, even if you know the code you are typing in your terminal, don't do it as root (with sudo/su/gksudo/gksu/kdesudo...) unless absolutely necessary! If you are not running as super user, then you can't damage your system nearly as badly if you do slip up and make a serious mistake in terminal. Example: running rm -rf /* will only erase everything in your home directory and a few other places, but sudo rm -rf /* will wreck your system beyond any hope of recovery. Note: do not try these commands! I mention them only as an example of the dangers of using "sudo" unthinkingly. They will delete your files irrevocably!

Make sure when you add a real repository in /etc/apt/sources.list.d/, that the file name ends in ".list". So not "my_file_name" but "my_file_name.list".

Cheers and happy learning!
Fred

EDIT: corrected "source.list.d" to "sources.list.d". Thanks Flemur!
Last edited by Fred Barclay on Sun Nov 27, 2016 12:38 am, edited 1 time in total.
Image
"Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy."
- Albert Einstein
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: For which raison sudo could not work

Post by Flemur »

/etc/apt/source.list.d/my_file_name
Also, that should probably be sources.list.d
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
killer de bug

Re: [Solved] For which raison sudo could not work

Post by killer de bug »

sudo doesn't work in a bash script with this kind of echo stuff. My understanding is that sudo only execute the first command (echo) with the superuser rights and then everything else is just normal user rights.

A solution is to move to su - if you have a root account.
Another solution is to call a new shell in the sudo command. This new shell can execute the remaining part of the command with elevated privileges.
For example:
doesn't work: ~$ sudo echo 2 > /proc/acpi/thermal_zone/ATF0/polling_frequency
should work: -$ sudo sh -c 'echo 2 > /proc/acpi/thermal_zone/ATF0/polling_frequency'


Therefore, I expect the OP command to be:
sudo sh -c 'echo something > /etc/apt/sources.list.d/my_file_name'
Locked

Return to “LMDE Archive”