How to run sudo commands with tty in Execute and Bash Resources

Hi Folks,

How can we use tty in execute and bash resource. Command with sudo keep on fails.

Are you using sudo because you are running chef-client as a non root user? If so does the user have passwordless sudo set up?

The question sounds like you might be trying to fit a square peg into a round hole. What are you trying to accomplish?

Generally speaking, the chef client would run as a daemon, not attached to a terminal, so the tty command wouldn’t return anything meaningful; you’ll probably just get “not a tty” or something like that. Of course you can manually start chef-client from your command line, but that would usually defeat the purpose.

Also, chef-client is a system administration tool that should generally run as user root, so you won’t need sudo. People have made it run as another user, but you will run into all kinds of issues at every turn.

Kevin Keane
Whom the IT Pros Call
The NetTech
http://www.4nettech.com
Our values: Privacy, Liberty, Justice
See https://www.4nettech.com/corp/the-nettech-values.html

Yup i am running chef-client as non root user and it has password less setup…Here the thing is i want to run a command with sudo in recipe. When i use execute or bash resource with sudo command it is failing saying please provide “tty”.

Hi,

Can you run the sudo command “manually” as the user the chef-client is running as? If not then you actually don’t have a Chef problem, but more a general setup/system problem.
Did you read the man page of sudo and try various command switches that might help? So switches that might come in useful are:

  • -i: simulate initial login
  • -E: Preserve environmet

If the problem still exists, could you provide more info like the command you are running and the exact error output?

79: bash ‘Running sudo test sleep command’ do
80: user 'root’
81: cwd '/tmp’
82: code <<-EOH
83: sudo sleep 1000
84: EOH
85: end

Output

:
Mixlib::ShellOut::ShellCommandFailed

Expected process to exit with [0], but received ‘1’
---- Begin output of “bash” “/tmp/chef-script20150813-3835-3kj758” ----
STDOUT:
STDERR: sudo: sorry, you must have a tty to run sudo
---- End output of “bash” “/tmp/chef-script20150813-3835-3kj758” ----
Ran “bash” “/tmp/chef-script20150813-3835-3kj758” returned 1

When run manually it’s working fine.

Do you have requiretty set in your /etc/sudoers or any /etc/sudoers.d/*.conf? If so, sudo will never work without a tty.

Yup …by default this is enabled in sudoers file…When i commented it, works fine. Is this the only solution?

This is for sure the correct and clean solution. Other solutions (like "sudo bash -c “sleep 1000”) might work depending on your system and setup but there is no guarantee.

Thank you so much Joerg.