You are not logged in.

#1 2019-11-14 20:40:40

BLizgreat!
Resident Babbler - vll!
Registered: 2015-10-03
Posts: 1,217

How to poweroff/reboot gnu/Linux like a pro !!!

Ok people welcome to another babble how-to. We're going to cover what the title says, errr that's why I used that title. Ummm getting on with it, being able to poweroff or reboot a gnu/Linux operating system from terminal is fun. It can also impress your friends too, you'll feel like a super tech assassin, now let's go over how it's done.

With systemd having taken over as the default init am going to focus on it. It's the wave of the future. The commands to poweroff(shutdown), reboot, suspend and hibernate which someone would use in terminal are as follows.

Poweroff:

sudo systemctl poweroff

Reboot:

sudo systemctl reboot

Suspend:

sudo systemctl suspend

Hibernate:

sudo systemctl hibernate

Which is what these commands will do. Though we aren't stopping here, because for one thing, that's a lot of typing. For another it means we have to enter the password every time we run a sudo command and in this case that's uncool and finally, that's a lot of typing. So surely ... there has to be a way to make this work better, right ? There is, enter the .bash_aliases file. A bash alias is just whatever someone sets in the above file, to run a command, once one is defined, when that alias is entered into a terminal that's what bash will interpret it as and will run whatever has been associated with that alias. It can be a lot of things, a command, a script etc but in this case it's going to be a command. Open your file-manager (which in Bunsenlabs is Thunar but any FM you have installed will do.) Let's check, as there should already be a .bash_aliases file in your users /home directory. While in Thunar press the Ctrl key + hit the H key to unhide/show hidden files and folders. If you already have a .bash_aliases file here you're good to go(open that file in your text editor of choice), if not create one. Right click, select create document or whatever and then name the thing .bash_aliases.

Once we open the .bash_aliases file ( note: You do not need sudo-etc and shouldn't use such for stuff in your users /home. Those directories and files are already owned by your user.) With the file open, we're now going to add some bash aliases to it. Make it look as follows.

alias poweroff="sudo systemctl poweroff"
alias reboot="sudo systemctl reboot"
alias suspend="sudo systemctl suspend"
alias hibernate="sudo systemctl hibernate"

Save the file and then logout your user and log back in to reload the config files. At this point test, pop open a terminal and type reboot or suspend or whichever and after entering your passwd that's what it'll do. We're getting there, bet you're already starting to feel like a command-line commando ... right ? smile What about having to enter a password though. Kinda involved eh, so let's deal with that next. To do so, in steps creating a file in the /etc/sudoers.d directory, in this file a person can add privileged commands and junk which a user/group is authorized to run on the OS without being bothered with entering a password.

Warning: Until such time as someone knows what they're doing don't overly abuse sudoers.d. Doing so can cause issues and you've been warned.

Now with that out of the way time to make a file in the location we need and then edit that puppy with with nano (a command-line text editor.) If nano isn't installed, install it now, then enter in terminal,

sudo touch /etc/sudoers.d/myfile

Which creates a file at that location named myfile. So time to edit the thing with nano, enter ...

sudo nano /etc/sudoers.d/myfile

The file opens up in nano and then make it as follows.

# Adding some commands I don't have to enter password to use.
yourusername ALL=(ALL) NOPASSWD: /bin/systemctl

Save the file, which to do when using nano you press the following keys, hold down the Ctrl key + press the X key, then press the Y key and finally the Enter/Return key on your keyboard. Log your user out/in and whamo ! Test the stuff out. smile

COOL BONUS BABBLE: Someone can also use these in menu.xml (your right click menu) or add custom keyboard shortcuts in rc.xml, which will run these commands and do the poweroff/reboot/suspend-etc and being able to hit a key-combo and do this kind of thing is certainly fun, more convenient and uber-l33t. smile

CLARIFICATION: When it comes to that above, don't mean use the bash alias for them, use the actual cmds to do whatever ie: "sudo systemctl poweroff" etc.

Last edited by BLizgreat! (2019-11-15 01:42:37)

Offline

#2 2019-11-15 04:07:41

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,088
Website

Re: How to poweroff/reboot gnu/Linux like a pro !!!

But... you don't need sudo, or editing sudoers, if you install policykit-1-gnome (no longer used by Gnome smile ) and run

/usr/lib/policykit-1-gnome/polkit-gnome-authentication-agent-1 &

in your autostart.

On BL you can run 'systemctl poweroff' and friends just like that, as a normal user.


John
--------------------
( a boring Japan blog, idle Twitterings and GitStuff )
In case you forget, the rules.

Offline

#3 2019-11-15 06:29:03

BLizgreat!
Resident Babbler - vll!
Registered: 2015-10-03
Posts: 1,217

Re: How to poweroff/reboot gnu/Linux like a pro !!!

Wasn't the point John-san. A cool look at a bunch of different things gnu/Nix related is. Also potentially attracting a certain type of nixer to the BL community. I don't have BL, will never use anything but Debian myself, also don't like polkit or have such pre-config's and any required packages assoc with such. Ah just going to say it, my install could kick the hell out of your install and your installs momma too. Lmao ! tongue

ps, though yeah it's common place for out-of-box distro's to have it set that way. Ahhhh no worries fellow nixer. It's all good. Each their own way. wink

Last edited by BLizgreat! (2019-11-15 06:41:48)

Offline

#4 2019-11-15 06:44:30

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,088
Website

Re: How to poweroff/reboot gnu/Linux like a pro !!!

BLizgreat! wrote:

my install could kick the hell out of your install

This is surely not the point either.

First thing I do on a new cli install is install policykit-1 so I can shutdown without sudo. (In fact the -gnome part is not needed for cli as I recall...)

But each to their own.
(...as you edited in while I was posting.)

Last edited by johnraff (2019-11-15 06:45:16)


John
--------------------
( a boring Japan blog, idle Twitterings and GitStuff )
In case you forget, the rules.

Offline

#5 2019-11-15 06:51:33

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,088
Website

Re: How to poweroff/reboot gnu/Linux like a pro !!!

BLizgreat! wrote:

I don't have BL, will never use anything but Debian myself...

Just for anybody reading this: BL is Debian.
Nothing that doesn't come straight from the Debian repos, beyond our config stuff which anyone as smart as BLZ can put together themselves.

There are a couple of home-built or backported packages in the backports repo, used to be a couple in the default BL setup (many in CrunchBang), but no more. (Unless I've forgotten one. roll)


John
--------------------
( a boring Japan blog, idle Twitterings and GitStuff )
In case you forget, the rules.

Offline

#6 2019-11-15 06:53:02

ohnonot
...again
Registered: 2015-09-29
Posts: 4,165
Website

Re: How to poweroff/reboot gnu/Linux like a pro !!!

My systemd does not require SU auth for these commands - and I don't use gnome policykit at all. Some PAM magic IIRC.

Also FTFY:

alias off="sudo systemctl poweroff"

Offline

#7 2019-11-15 14:30:02

clusterF
Member
Registered: 2019-05-07
Posts: 316

Re: How to poweroff/reboot gnu/Linux like a pro !!!

non systemd KISS method, as root user...

echo "YOURUSER ALL= NOPASSWD: /sbin/poweroff, /sbin/reboot" > /etc/sudoers.d/user_shutdown

"Common sense is like deodorant, those who need it the most never use it."

git: clusterF

Online

#8 2019-11-17 17:46:06

ohnonot
...again
Registered: 2015-09-29
Posts: 4,165
Website

Re: How to poweroff/reboot gnu/Linux like a pro !!!

^ I seriously recommend checking if that doesn't work without password anyhow, before editing /etc/sudoers.

Offline

#9 2019-11-17 22:00:43

clusterF
Member
Registered: 2019-05-07
Posts: 316

Re: How to poweroff/reboot gnu/Linux like a pro !!!

^ works on my nix ohnonot. Its not actually editing /etc/sudoers (visudo).

Last edited by clusterF (2019-11-17 22:03:59)


"Common sense is like deodorant, those who need it the most never use it."

git: clusterF

Online

#10 2019-11-17 22:21:05

BLizgreat!
Resident Babbler - vll!
Registered: 2015-10-03
Posts: 1,217

Re: How to poweroff/reboot gnu/Linux like a pro !!!

Have a babble thread up about this ... New and improved or approved method is creating files in /etc/sudoers.d for this kind of thing. Doesn't get overwritten in the case of an upgrade. Also just 4 record, actually use the same /sbin/poweroff etc but those are linked to /bin/systemctl already at least on Stretch and again in Buster. From a Stretch 9.7 or 8 OS.

ls -l /sbin/poweroff

Output:

lrwxrwxrwx 1 root root 14 Jun  7 15:51 /sbin/poweroff -> /bin/systemctl

Apparently it's a backwards compat thing, cause many people still use those cmds. However the one's listed in the OP are the correct one's for direct use with systemd, the systemctl things. Just babbling, either way it goes doesn't so much matter but may as well do it right. Agree with Ohnonot, that it's better to avoid fiddling with something like systemctl no passwd. I don't so much care on a single-user, personal system though. Anyone here with access to my old laptop, isn't going to know wth systemctl is/does or even exists and if they did and had that kind of access to this old thing wouldn't really matter. As getting root on it would be fairly easy for someone with the know how and physical access. I don't bother with disk encryption, no bio's passwd set, blahblah.

Last edited by BLizgreat! (2019-11-17 22:25:25)

Offline

#11 2019-11-18 10:44:32

clusterF
Member
Registered: 2019-05-07
Posts: 316

Re: How to poweroff/reboot gnu/Linux like a pro !!!

^ so /sbin/poweroff is linked to systemctl, did not know that. I use mainly voidlinux (runit init) these days but from memory using systemctl poweroff i dont think i needed a password even without messing around with sudoers with nopasswd, would that be correct?


"Common sense is like deodorant, those who need it the most never use it."

git: clusterF

Online

#12 2019-11-18 16:28:01

BLizgreat!
Resident Babbler - vll!
Registered: 2015-10-03
Posts: 1,217

Re: How to poweroff/reboot gnu/Linux like a pro !!!

Yeah it's linked to /bin/systemctl already and don't know much about how Void is config'ed, only ever played with an install of it really briefly, as Ohnonot said, one sure way to find out what's going on w or w/o needing passwd, would just be to try it ( systemctl poweroff/reboot etc) without passwd/sudo and see. See which groups your user is in, in terminal "groups" likely in many a user is automatically added to the relevant group, allowing them to run such w/o passwd. The systemctl poweroff/reboot-etc are the approved cmds on a systemd OS though.

Am still doing it the older way too. With /sbin/poweroff, /sbin/reboot ... It's just how I've settled on it at the moment but really doesn't matter, if it's changed, I'll change too or could get ahead of the curve and just start using the appropriate cmds now. smile Either way does the same, as laid out in this babble tute. When I type an alias, poweroff/reboot/suspend in terminal that's what the OS does.

Last edited by BLizgreat! (2019-11-18 16:34:04)

Offline

Board footer

Powered by FluxBB