Is there a way to prevent all commands from being defined as an alias?
For example a user shouldn't be able to define rm
or any other command (Ubuntu default commands) as an alias name.
Is there a way to prevent all commands from being defined as an alias?
For example a user shouldn't be able to define rm
or any other command (Ubuntu default commands) as an alias name.
There is no way you can prevent a user from defining whatever aliases they prefer. Consider:
/etc/bash.bashrc
. They enable it wherever they chose to./etc/bash.bashrc
, and all~/.bashrc
and~/.bash_aliases
via a script. They put their aliases in another file and source it.PROMPT_COMMAND
that disables certain aliases. They redefine or undefinePROMPT_COMMAND
.DEBUG
and undefine aliases. They remove the trap.unset
,builtin
andenable
; makealias
a function;declare -rf alias
to prevent users from modifying the function; and export the function. They run/bin/bash
with--rcfile
and--init-file
to start a new shell, where said builtins are now enabled.You could disable aliases at compile time, then it would be up to you to keep bash up-to-date and make sure you aren't affected by the next Shellshock. Of course, the users could build their own bash.
TL;DR
The only way to prevent a user from creating aliases is to provide them with a shell that doesn't support aliasing. This is generally an X/Y problem, where X is really a threat model that should be solved with appropriate controls or architectures rather than trying to solve the problem post-facto after a user is given a shell on a shared system.
Below, I provide a technically-correct answer, as well as some guidance on what problems this will and won't solve. I also provide some additional guidance on alternative controls.
Using the Bash Restricted Shell
You can use Bash's restricted shell by assigning rbash as the user's login shell. For example:
You must then disable the alias built-in for the user, preferably without breaking everyone else's shell too. As an example, you could add the following to a file such as /etc/profile.d/rbash.sh:
Caveats
Unless you've placed the user into a chroot jail or provided them with a modified PATH that doesn't include access to other shells, there's nothing stopping the user from simply typing
bash
at the prompt and getting an unrestricted shell.In addition, by design the restricted shell prevents many common activities such as changing directories:
but doesn't prevent other scripts or programs in the PATH from doing so. This means you have to carefully craft the user's environment, and specifically that you need to prevent them from being able to modify the PATH in their startup files, because even though rbash makes PATH read-only it does so after initialization.
Better Choices
Even if you use rbash, you need to do so as part of a broader set of controls. Some examples may include:
Prevent non-technical users from accidentally invoking dangerous commands by providing default aliases such as
rm -i
,mv -i
, andcp -i
in the /etc/bash.bashrc file.enable -n alias
if you like.Traditional Unix permissions or POSIX ACLs to protect files and directories.
Logins that perform a single non-interactive command. For example:
Use per-key SSH forced commands. For example:
Use the OpenSSH ForceCommand option with a conditional Match block.
Use special tools like gitolite or scponly designed for your specific use case.
Use a chroot jail.
Use virtualization such as Xen, OpenVZ, LXC, VMware, VirtualBox, or other technologies to provide a segregated environment.
Once you have accurately defined your threat model, you can identify the most appropriate controls for your use case. Without a more meaningful understanding of why you want to prevent aliasing (e.g. what real-world problem does it solve?) you can't select the most appropriate controls.
This is quite a pointless endeavor, as muru's answer shows. But there are some options, but they are not perfect.
According to
bash
manual, functions always take precedence over aliases, thus we could do the following:You could place function definition into the systemwide
.bashrc
, however as muru pointed out, smart users will find way to get aliases by sourcing a differentbashrc
file for example.Another idea I've played with is
enable
built-in.alias
is a shell built in, andbash
has niceenable
command that allows enabling or disabling builtins. For instance, here's me disablingalias
.Again, using systemwide
bashrc
is an option here.You could define (in
/etc/profile
) a function calledalias
which does the validation you want (probably usingtype -p
) (after all "Ubuntu default commands" are "executables in$PATH
") before calling the builtinalias
, BUT, as others have pointed out, your users could get around that. Why not get a different set of users, or educate them ("Defining analias
that overrides a command is a Very Good Way of Shooting Oneself in the Foot, and causing confusion (e.g, Why doesls
prompt for my password?))?