@Linux
用alias命令避免虚拟机关机
下班时候关树莓派,poweroff命令一敲,才发现把SSH远程的虚拟机给关了。给管理员发邮件才重新开机。想了一下,应该把虚拟机上的halt、poweroff、shutdown命令都给禁用掉,只留一个reboot。
但是我一直用的是root用户,貌似也没法禁用某个命令。
要不用alias把关机的命令改成别的?
halt命令可以用halt -w来代替,不执行halt命令,也就写个记录。
[root@test ~]# halt --help
halt [OPTIONS...]
Halt the system.
--help Show this help
--halt Halt the machine
-p --poweroff Switch off the machine
--reboot Reboot the machine
-f --force Force immediate halt/power-off/reboot
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/reboot
poweroff命令也一样,可以用poweroff -w来代替。
[root@test ~]# poweroff --help
poweroff [OPTIONS...]
Power off the system.
--help Show this help
--halt Halt the machine
-p --poweroff Switch off the machine
--reboot Reboot the machine
-f --force Force immediate halt/power-off/reboot
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/reboot
但是shutdown命令只能用shutdown -k来代替,还是会出提示,有点吓人。
[root@test ~]# shutdown --help
shutdown [OPTIONS...] [TIME] [WALL...]
Shut down the system.
--help Show this help
-H --halt Halt the machine
-P --poweroff Power-off the machine
-r --reboot Reboot the machine
-h Equivalent to --poweroff, overridden by --halt
-k Don't halt/power-off/reboot, just send warnings
--no-wall Don't send wall message before halt/power-off/reboot
-c Cancel a pending shutdown
还是改成echo命令吧,嘿嘿!编辑.bashrc文件,alias添加几行:
alias halt='echo "Are you OK??"'
alias poweroff='echo "Are you OK??"'
alias shutdown='echo "Are you OK??"'
修改之后要source一下才能生效。居然报错?
[root@test ~]# source .bashrc
if: Expression Syntax.
then: Command not found.
root@test ~]# source /root/.bashrc
if: Expression Syntax.
then: Command not found.
百度一下,原来是shell的问题,修改一下默认shell即可。
[root@test ~]# echo $SHELL
/bin/tcsh
[root@test ~]# chsh -s /bin/bash
Changing shell for root.
Shell changed.
[root@test ~]# echo $SHELL
/bin/bash
[root@test ~]# source .bashrc
[root@test ~]#
输入命令之后返回Are you OK?,完全无害。
[root@test ~]# poweroff
Are you OK??