DevOps Zone
DevOps Zone is brought to you in partnership with:
spacer spacer Mark Needham
  • Bio
  • Website
  • @markhneedham
  • spacer
  • spacer

Mark Needham is a software developer and consultant at ThoughtWorks. I have a keen interest in developer testing and object oriented design of systems. Mark is a DZone MVB and is not an employee of DZone and has posted 250 posts at DZone. You can read more from them at their website. View Full User Profile

Puppet: sudo, sudo -i and sudo su

07.06.2012
spacer Email
Views: 2981
  • Tweet
  • spacer
The DevOps Zone is presented by DZone with partners including ThoughtWorks Studios and UrbanCode to bring you the most interesting and relevant content on the DevOps movement.  See today's top DevOps content and be sure to check out ThoughtWorks Studio's Continuous Delivery Video Series and UrbanCode's Webinars.

Recommended Links

Deployment Automation: The Basics

DevOps Deployment: Applying Agile and Lean

Continuous Delivery: Learn From The Inventors!

Share, Learn, Discuss Workflow

FREE DevOps, Build Pipeline Tool: "Go"

Like this piece? Share it with your friends:

| More

On the project I’m currently working on we’re doing quite a bit of puppet and although we’re using the puppet master approach in production & test environments it’s still useful to be able to run puppet headless to test changes locally.

Since several of the commands require having write access to ‘root’ folders we need to run ‘puppet apply’ as a super user using sudo. We also need to run it in the context of some environment variables which the root user has.

One way to do this would be to run the following command:

sudo su

That runs the ‘su’ command as root which means that we can run root’s shell as the root user without needing to know the root password.

The cool thing about using ‘su’ like this is that it leaves us in the same directory that we were in before we ran the command.

mneedham@ubuntu:/home/mneedham/puppet$ sudo su
root@ubuntu:/home/mneedham/puppet#

If instead we want to be positioned in the root home directory we could use the following:

mneedham@ubuntu:/home/mneedham/puppet$ sudo su -
root@ubuntu:~# pwd
/root

We can achieve a similar outcome using ‘sudo -i’, a flag I didn’t know about until my colleague Phil Potter showed me:

mneedham@ubuntu:/home/mneedham/puppet$ sudo -i
root@ubuntu:~# pwd
/root

-i [command]

The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the target user as a login shell. This means that
login-specific resource files such as .profile or .login will be read by the shell.

If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed.

sudo attempts to change to that user’s home directory before running the shell. It also initializes the environment, leaving DISPLAY and TERM unchanged, setting HOME, SHELL, USER, LOGNAME, and PATH, as well as the contents of /etc/environment on Linux and AIX systems. All other environment variables are removed.

Interestingly with ‘sudo -i’ we can pass a command which will be executed in the root context which would be useful in this situation as we wouldn’t need to keep switching to the root shell to run puppet.

One of the things that changes if we’re in the root shell is the value of $HOME so I started with that to check I was passing the command correctly:

mneedham@ubuntu:/home/mneedham/puppet$ sudo -i echo $HOME
/home/mneedham

Unfortunately the echo statement is getting evaluated in the context of the current user’s shell rather than the root shell and my colleague Rob Hunter showed me the ‘set -x’ tool/flag so that I could figure out what was going on with the escaping in the shell:

mneedham@ubuntu:/home/mneedham/puppet$ sudo -i echo $HOME
+ sudo -i echo /home/mneedham
/home/mneedham

As we can see, the $HOME value is expanded too early so we need to escape it like so:

mneedham@ubuntu:/home/mneedham/puppet$ sudo -i echo \$HOME
+ sudo -i echo '$HOME'
/root

If we disable that flag:

mneedham@ubuntu:/home/mneedham/puppet$ set +x
+ set +x
mneedham@ubuntu:/home/mneedham/puppet$ sudo -i echo \$HOME
/root

We can see that $HOME is being evaluated in the root shell context and we can also call our ‘puppet apply’ script in a similar vein.

 

Published at DZone with permission of Mark Needham, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:
  • DevOps
  • Puppet
  • Tool
ThoughtWorks Studios and UrbanCode, the sponsors of the DevOps Zone, are champions of the DevOps movement.  Their deployment tooling solutions focus on the entire software development lifecycle, involving all parts of an organization, which helps facilitate a migration to the DevOps philosophy.
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.