Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am using /etc/host to map localhost to a web domain. I would like a fast way of doing this rather than searching and replacing every time. I put this in my .bashrc file.

alias hostchange='
nowdir=$PWD;
cd /etc;
mv hosts hoststempname;
mv hostssecondary hosts;
mv hoststempname hostssecondary;
cd $nowdir'

I am getting this error and it won't progress regardless y or n.

mv: rename hosts to hoststempname: Permission denied
override rw-r--r--  root/wheel for hosts? (y/n [n]) 

I got it to work by adding sudo.

alias hostchange='
nowdir=$PWD;
cd /etc;
sudo mv hosts hoststempname;
sudo mv hostssecondary hosts;
sudo mv hoststempname hostssecondary;
cd $nowdir'

Is this legitimate, I'm taking a shot in the dark here?

share|improve this question
    
I'll try to be as little offensive as I can - sorry if it is still too much. In order to get an answer that can be useful to you, please write what basic information about linux/unix you posses. Specifically, in the areas of user accounts, file ownership and file permissions. –  rozcietrzewiacz Oct 10 '11 at 11:46
    
Gotcha, I'll be sure to include this info next time: (Mac 10.6.5 (unix | bash) / Admin) Does that help? –  ThomasReggi Oct 11 '11 at 8:26
    
Uhm... No, I simply can't believe the Admin part. What admin does not understand the bare minimum of file ownerships and permissions? –  rozcietrzewiacz Oct 11 '11 at 9:38
    
What are you getting at? Its my computer and I'm the only user, what does file ownership & permissions have to do with anything??? –  ThomasReggi Oct 11 '11 at 22:00
    
Ok. I see. You just have no idea about the concept of *nix users. I'll try to get over it somehow... Just please, don't call yourself admin. –  rozcietrzewiacz Oct 12 '11 at 6:38
show 1 more comment

2 Answers

up vote 3 down vote accepted

You are on the right track! A couple comments about this. It is usually better practice to leave multi-line actions like this to functions. I'd probably write it like this:

change_etc_hosts_file() {
  set -e # stop running if we encounter an error
  sudo \mv -f /etc/hosts /etc/hoststempname
  sudo \mv -f /etc/hostssecondary /etc/hosts
  sudo \mv -f /etc/hoststempname /etc/hostssecondary
  set +e
}
alias changehosts=change_etc_hosts_file

You'll notice I also used absolute paths instead of changing directory. This is usually a better idea (to use absolute paths). If you do want to keep using relatives then it is usually better to do that in a sub-shell so you don't have to handling changing directory back to $PWD (which if you abort will leave you in a weird state). To do it as a sub-shell it would look like this:

change_etc_hosts_file() {
  ( # use subshell
    cd /etc
    set -e # stop running if we encounter an error
    sudo \rm -f hoststempname # the \ escapes aliases which might cause prompting
    sudo \cp -f hosts hoststempname
    sudo \cp -f hostssecondary hosts
    sudo \cp -f hoststempname hostssecondary
  )
}

The cd happens inside the () which is a new process so it won't affect your current working directory.

share|improve this answer
    
+1 for the '\' alias escapes and the sub-shell. –  Jason Mock Oct 11 '11 at 15:12
add comment

The fact of you having doubts about the sudo command shows that somehow you survived so far in the Unix world without possessing the basic knowledge about the concept of unix users (root especially). Although this situation is incomprehensible to me, your question along with the comments clearly indicate that is the case.

Yes, what you achieved with sudo is just what is expected. This is because:

  • On Unix systems, there is a number of system users - you can call them 'virtual' (these are not to be mistaken with human computer users).
  • Each file and directory has it's owner, which is always one of the (virtual) users.
  • The root user, known as superuser is the special system account every Unix has to have. He can do everything - has full power over the system.
  • sudo is a command used to temporarily leverage "normal" user's privileges to root so that some special tasks can be performed.

Do have a further read about root, sudo and file permissions on these example sites: 1, 2, 3, 4, 5, 6

share|improve this answer
    
I understand what you are getting at, however what confused me was how sudo would run within an alias and how unix would handle the password request process. I have used sudo before. I had no experience with how sudo would behave within an alias. Moreover that when sudo was run more than once within a command it wouldn't break the command and ask for the password before it finished the command. Thanks for your through clarification on sudo and expressing your concerns. –  ThomasReggi Oct 12 '11 at 19:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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