LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-18-2019, 07:16 AM   #1
raymond0517
Member
 
Registered: Mar 2019
Posts: 37

Rep: Reputation: Disabled
fail to execute "echo 'my password' | sudo -S su" in command line


I tried to run the command

echo 'my password' | sudo -S su

hopefully it can automatically goes into a child process as root,
however the command executes with no error but exit the child process
and back to my existing user privilege.

How can I remain at the child process as 'root' without 'exit'ing from the
process.
 
Old 03-18-2019, 07:30 AM   #2
dc.901
Senior Member
 
Registered: Aug 2018
Location: Atlanta, GA - USA
Distribution: CentOS/RHEL, openSuSE/SLES, Ubuntu
Posts: 1,005

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
Originally Posted by raymond0517 View Post
I tried to run the command

echo 'my password' | sudo -S su

hopefully it can automatically goes into a child process as root,
however the command executes with no error but exit the child process
and back to my existing user privilege.

How can I remain at the child process as 'root' without 'exit'ing from the
process.
You could use NOPASSWD flag in sudoers:

Code:
username ALL=(ALL) NOPASSWD: ALL
Then, you can simply run the sudo without specifying the password.
Hopefully this is a trusted user; otherwise anything like this is dangerous.


Another option is to only allow certain commands; for example:

Code:
username ALL=(ALL) NOPASSWD: /full/path/command
This will only allow "username" to run "/full/path/command" with sudo without specifying password.
 
1 members found this post helpful.
Old 03-18-2019, 08:33 AM   #3
raymond0517
Member
 
Registered: Mar 2019
Posts: 37

Original Poster
Rep: Reputation: Disabled
thanks dc.901 for the reply

I added a line at /etc/sudoers, at # User privilege specification section,


root ALL=(ALL:ALL) ALL
b ALL=(ALL) NOPASSWD:ALL <--- added this line

b is my user name

however, upon reboot,
at b user command window prompt,
I still can't execute commands that require root privilege.
 
Old 03-18-2019, 08:37 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Are you using sudo?

Adding your name to the file does not mean you can just run all commands, it means you can use sudo before commands to indicate you wish to run them with root privileges using no password.

Last edited by rtmistler; 03-18-2019 at 08:39 AM.
 
Old 03-18-2019, 08:52 AM   #5
raymond0517
Member
 
Registered: Mar 2019
Posts: 37

Original Poster
Rep: Reputation: Disabled
I tried running command 'sudo su' at my username command prompt

it still prompt for password

it seems that the added line to /etc/sudoers has no effect
 
Old 03-18-2019, 08:54 AM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
How can I remain at the child process as 'root' without 'exit'ing from the
process.
Enter su (or su -to switch to root's environment) if you want to become root.
you'll be prompted for the root password.

It's not a "child process"...it's a new instance of the shell as if you'd logged in with root to begin with.

sudo is not required or involved to become root.
 
1 members found this post helpful.
Old 03-18-2019, 08:57 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
To add to what scasey has said.

sudo is not intended to be used to login as root, but to grant you root privileges for one command.

For instance, editing /etc/profile. This file is owned by root and does not have write privileges for other users. Therefore to change it you would need root privileges. To do that you would use the sudo command prefix as part of the call to edit the file:
Code:
$ sudo gedit /etc/profile
 
Old 03-18-2019, 09:00 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Quote:
Originally Posted by rtmistler View Post
For instance, editing /etc/profile. This file is owned by root and does not have write privileges for other users. Therefore to change it you would need root privileges. [/code]
Close.

The program sudoedit will invoke your favorite editor but run as a normal user and work from a temporary copy of the file. Only after the temporary file is saved and the editor exited will it then replace the original file. That's much safer.

Code:
sudoedit /etc/profile;
 
Old 03-18-2019, 09:01 AM   #9
raymond0517
Member
 
Registered: Mar 2019
Posts: 37

Original Poster
Rep: Reputation: Disabled
I tried command 'su',
it prompts for password, but this password is different from my username password, and I am not sure what it is,
I tried with guess it could be 'toor' but failed to authentical.
 
Old 03-18-2019, 09:02 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
raymond0517, what problem are you trying to solve as root?
 
1 members found this post helpful.
Old 03-18-2019, 09:07 AM   #11
raymond0517
Member
 
Registered: Mar 2019
Posts: 37

Original Poster
Rep: Reputation: Disabled
I merely want to run commands with root privileges without having to key in password at every session,
if cannot be done, then I would like to run a simple script to automatically enter the sudo password
 
Old 03-18-2019, 09:09 AM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Then run "sudo passwd", once, and supply a password for the root account. Then connect to it with "su -l"
 
1 members found this post helpful.
Old 03-18-2019, 09:09 AM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Please look at the manual page for the su command. The full name of it is "Switch User". The default user to switch to is root.

You would not use the sudo command to perform the su command. You would just type su and then after that you would be asked for the root password. After successful entry of the root password, your terminal session would then be logged in as root.

Sudo allows you to USE root privileges for the duration of the command you are entering. One exception to this is that you do not use the sudo command modifier for the default su command.
 
Old 03-18-2019, 09:10 AM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by raymond0517 View Post
I merely want to run commands with root privileges without having to key in password at every session,
if cannot be done, then I would like to run a simple script to automatically enter the sudo password
An example of what exact commands?
 
Old 03-18-2019, 09:25 AM   #15
raymond0517
Member
 
Registered: Mar 2019
Posts: 37

Original Poster
Rep: Reputation: Disabled
I managed to change root password using 'sudo passwd'
thanks.

However, when I type su, it again prompt for password.

So I tried "echo 'rootpasswd' | su"

it returns "su: must be run from a terminal" error message.

I am trying to create an automatic login to root, but not allowed
 
  


Reply

Tags
bash function



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] if [[ -n "$1" ]]; then FAIL FAIL FAIL rbees Programming 7 03-25-2015 02:39 PM
[SOLVED] Would like to "echo" a line, "read" reply, then execute the reply: How in bash? UnixAgain Linux - Newbie 6 02-03-2015 08:27 AM
echo 0:$(echo 8*35*37*47 | bc) | xxd -r && echo $(id -un) Linuxanity LinuxQuestions.org Member Intro 1 08-15-2012 06:30 PM
echo $USER/ sudo echo $USER jamescondron Programming 6 06-03-2009 11:58 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration