1

I would like to create an alias of the below command with sudo like below.

alias /usr/bin/vmstat='sudo /usr/bin/vmstat'

is it possible to create something like that for a user?

muru
  • 197,895
  • 55
  • 485
  • 740
  • That will work in zsh. Which shell are you using? – muru Sep 07 '23 at 13:01
  • 1
    What exactly is the problem you are trying to solve here? According to man vmstat vmstat does not require special permissions – steeldriver Sep 07 '23 at 16:01
  • I have deployed a monitoring system and that runs a command /usr/bin/vmstat 1 3 to get CPU usage but that command returns out with error of /proc not mounted but when I run sudo /usr/bin/vmstat 1 3 i return the result correctly. – Mirza Sajawal Baig Sep 08 '23 at 05:30
  • @MirzaSajawalBaig please edit your question to include that information, and provide the exact error message as well. Which monitoring system is it? – muru Sep 12 '23 at 02:58

1 Answers1

1

In Bash, an alias is just a name, but not a path (since aliases are stored in memory).

Here are some examples from my own system:

alias cte='crontab -e'
alias scte='sudo crontab -e'
# if I wanted a separate alias for 'sudo htop' I would have called the following alias shtop
alias htop='sudo htop'

In your example, for Bash you would need to do:

alias vmstat='sudo /usr/bin/vmstat'

Then the vmstat command would run with sudo by default.

You just need to decide which alias name you want to allocate to which command - with or without sudo.

Artur Meinild
  • 26,018