DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Puppet: sudo, sudo -i and sudo su

Puppet: sudo, sudo -i and sudo su

Mark Needham user avatar by
Mark Needham
·
Jul. 06, 12 · Interview
Like (0)
Save
Tweet
Share
9.51K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

Sudo shell

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using Swagger for Creating a PingFederate Admin API Java Wrapper
  • Host Hack Attempt Detection Using ELK
  • How To Best Use Java Records as DTOs in Spring Boot 3
  • How to Use Buildpacks to Build Java Containers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: