Sign up ×
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 always boot up my GNU/Linux laptop in console mode. But sometimes I need to bring up the GUI mode. it always requires entering the root password. So I wrote the following script "gogui.sh" and put it in /usr/bin:

#!/bin/bash
echo "mypassword" | sudo service lightdm start

It is a really stupid idea, as if someone read the file, can easily see my password.

Is the an alternative to this?

share|improve this question
2  
you have an aversion to entering your password at a sudo prompt? what are your real goals? – Jeff Schaller yesterday
    
I am just usurious to see a proper/alternative way – Saeid Yazdani yesterday
5  
Why not use the NOPASSWD syntax detailed in sudoers(5) for password-free calls to that service command, or a suitable wrapper? – thrig yesterday
    
Note that in constructions like these, an unprivileged user doing a process list (with e.g. ps ax) at just the right time might also see your password. It won't happen in this particular case, as echo is a bash builtin command, but the general approach is also dangerous for this reason. – marcelm 7 hours ago

4 Answers 4

up vote 28 down vote accepted

Passing a password to sudo in a script is utterly pointless. Instead, add a sudo rule adding the particular command you want to run with the NOPASSWD tag. Take care that the command-specific NOPASSWD rule must come after any general rule.

saeid ALL = (ALL:ALL) ALL
saeid ALL = (root) NOPASSWD: service lightdm start

But this is probably not useful anyway. lightdm start starts a login prompt, but you only need that if you want to let other users log in graphically. You don't need it if all you want is to start a GUI session. Instead, call startx to start a GUI session from your text mode session. This does not require any extra privilege.

You may need to explicitly specify your window manager or desktop environment, as startx might not pick up the same default session type that lightdm uses.

startx -- gnome-session
share|improve this answer
    
Thanks...thats way way way better :P...I use unity, should I still pass to startx the gnome-seesion argument? – Saeid Yazdani yesterday
    
I just checked with startx, while it does bring up my desktop, but many things are missing like the unity launcher and also I got no sound, if I do the lightdm service everything works fine – Saeid Yazdani yesterday

There are some options, and one of them is to add the specific command using visudo with the NOPASSWD flag:

%wheel ALL=(ALL) NOPASSWD: /path/to/myscript

cat /path/to/myscript
#!/bin/bash
service lightdm start
share|improve this answer

EDIT
Just noticed in your comments you mentioning Unity, so it appears you're running Ubuntu. My original answer was geared toward SysVinit systems, but commands like telinit still exist for compatibility and will work on distros using Systemd or Upstart.

SysV way

    telinit 5

On modern Systemd systems, telinit is redirected to systemctl.

Systemd way

    systemctl isolate runlevel5.target

or

    systemctl isolate graphical.target

Original answer:
On older non-Systemd distros (RHEL/CentOS 6 and older, for example) the easiest way to switch from console mode to GUI is with the telinit command. On the RHEL/Centos distros, for example, multi-user text-based mode is run level 3 and the multi-user GUI mode is run level 5. Switching from console to GUI mode would be done like this: By default, telinit requires root privileges. To run this as a normal user without a password, you'll need a sudoers entry or set the setuid flag set on the telinit executable. The sudo method is the preferred approach as it can be restricted to just your account.

share|improve this answer

Since you're specifically referring to sudo, stick to Gilles' answer. However, since your question title merely was about getting a password to a program's stdin and I had a similar situation recently:

echo $(read -sp "Password: " password; echo $password) | yourcommand

I'll probably earn some bashing for that, but it worked.

Speaking of bash, there you can probably also use

yourcommand <$(read -sp "Password: " password; echo $password)
share|improve this answer

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.