I have Linux Mint 18.

How can I run this command after login with main user (no root user):

sudo echo "nameserver" | sudo tee /etc/resolv.conf > /dev/null

I have read so many steps to run a command after login, but they don't work for me (or for other people too).

I need to run this command, because I run it every time to fix a network error.


You can put this line in $HOME/.bash_profile. All commands there are run on login.

But I guess in your case it would be better to fix source of problem. If you want to use constantly, then you'd better add it to your network interface config.

If is usually placed in /etc/network/interfaces. You'll find there list of your interfaces and their configuration parameters.

Then you'll need to add line dns-nameservers to section with interface you are using.

  • The part about ~/.bash_profile is not entirely correct. It doe not run on login shells and it does not run when you enter a graphical environment (~/.xsession does that, but it is not respected by all graphical managers). But yeah, adding to the interfaces is the correct path. – grochmal Aug 3 '16 at 20:37
  • when i open "/etc/network/interfaces" file, no show a interface list: s31.postimg.org/ffz3ot29n/… – Linux Mint Newbie User Aug 3 '16 at 21:00
  • OMG,i can fix this!! 6 months with this problem!! every time run this command omg.I tell you what i do: i insert the command in the "rc.local" file, changing the commando to: (sleep 60 echo "my_root_password" | sudo -S echo "nameserver" | sudo tee /etc/resolv.conf > /dev/null) & I add the password parameter because the "sudo" command, and add the sleep command, because without sleep don't work. I think that is because script was replace for network configuration generated after script run. With sleep, the script run after network system configuration is set. (sorry for my english) – Linux Mint Newbie User Aug 3 '16 at 23:13
  • Well, if this way satisfies your needs, then congratulations to you :) With time you'll figure how things done in linux. In this particular case you can dig into setting up network connections in Ubuntu (from which Mint is derived) and maybe some tricks with visudo so you'll won't need your password hardcoded in plain text somewhere. Good luck! – Pavel Kazhevets Aug 4 '16 at 0:44

Add the commands to the end of your bashrc file

  vi ~/.bashrc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.