Linux alias for user

How can I preset aliases for all users?

I have Ubuntu 14.04.2. I want to make it so all users automatically have a specific set of aliases. I have my aliases set in my personal .bashrc, but I don’t want to have to manually copy them into the other users. Ideally it should automatically set these for newly created users as well.

By «preset,» do you mean you want all new users to be created with certain aliases, which they can then easily reconfigure? Or do you mean you want to create global aliases, such that all users will have them all the time (and be able to redefine/undefine them. but not see them when they look through their own configuration files, which they might find befuddling)? Or something else? Related: Undertanding .bashrc and .bash_profile (in spite of its odd title, I believe that question and this one are actually quite similar).

4 Answers 4

You can create a script in /etc/profile.d/ to make aliases for all users:

    Create a file called 00-aliases.sh (or any other fancy name) in /etc/profile.d :

gksu gedit /etc/profile.d/00-aliases.sh 
alias foo='bar --baz' alias baz='foo --bar' 
  • /etc/profile is a global file that gets run before ~/.profile .
  • /etc/profile.d/ is a folder that contains scripts called by /etc/profile
  • When /etc/profile is called (when you start/login a shell), it searches for any files ending in .sh in /etc/profile.d/ and runs them with one of these commands:

source /etc/profile.d/myfile.sh 

This explains it very well. The clarification helps me to develop other uses as well. Thanks for all the help!

The method in this answer should not be used. Aliases in .sh files in /etc/profile.d/ (or /etc/profile ) will be defined only for login shells and they will not work in interactive non-login shells. Unlike environment variables, bash can’t export aliases to child processes, not even child bash shells. This method may seem to work properly if it’s only tested in login shells, such as the original shell obtained by logging on in a virtual console or via SSH, but it fails in their child shells and also fails in shells started by GUI terminal windows.

@Helio My answer to Undertanding .bashrc and .bash_profile gives two other methods, depending on what the goal is, along with detailed explanation. I’m unsure if either this or that question can be considered a duplicate. I’ve commented on this question to get clarification from the OP about their needs. This question is more narrowly scoped than that one so I suppose it’d be OK for me to post a short answer here covering some of the material in my long one there. (For now, I’m hoping to get a reply by the OP to my comment.)

Источник

How to Create and Use Alias Command in Linux

Linux users often need to use one command over and over again. Typing or copying the same command again and again reduces your productivity and distracts you from what you are actually doing.

You can save yourself some time by creating aliases for your most used commands. Aliases are like custom shortcuts used to represent a command (or set of commands) executed with or without custom options. Chances are you are already using aliases on your Linux system.

List Currently Defined Aliases in Linux

You can see a list of defined aliases on your profile by simply executing alias command.

Here you can see the default aliases defined for your user in Ubuntu 18.04.

List Aliases in Linux

You can create an alias with a single character that will be equivalent to a command of your choice.

How to Create Aliases in Linux

Creating aliases is relatively easy and quick process. You can create two types of aliasestemporary ones and permanent. We will review both types.

Creating Temporary Aliases

What you need to do is type the word alias then use the name you wish to use to execute a command followed by » your custom command here»

Here is an actual example:

You can then use «wr» shortcut to go to the webroot directory. The problem with that alias is that it will only be available for your current terminal session.

If you open new terminal session, the alias will no longer be available. If you wish to save your aliases across sessions you will need a permanent alias.

Creating Permanent Aliases

To keep aliases between sessions, you can save them in your user’s shell configuration profile file. This can be:

The syntax you should use is practically the same as creating a temporary alias. The only difference comes from the fact that you will be saving it in a file this time. So for example, in bash, you can open .bashrc file with your favorite editor like this:

Find a place in the file, where you want to keep the aliases. For example, you can add them in the end of the file. For organizations purposes you can leave a comment before your aliases something like this:

#My custom aliases alias home=”ssh -i ~/.ssh/mykep.pem [email protected]” alias ll="ls -alF" 

Save the file. The file will be automatically loaded in your next session. If you want to use the newly defined alias in the current session, issue the following command:

To remove an alias added via the command line can be unaliased using unalias command.

$ unalias alias_name $ unalias -a [remove all alias]
Conclusion

This was a short example on how to create your own alias and execute frequently used commands without having to type each command again and again. Now you can think about the commands you use the most and create shortcuts for them in your shell.

Источник

How to create alias for user in linux

😀 Note that you can also use tilda to switch to directories of many users in your system as follows: Solution 3: As icarus suggested in the comments, one simple way is to create an alias that does However if you really want to behave this way, you can alias a function to that handles checking if the directory is : However this also causes tab completion for to break Solution 1: records your command in its history list prior to expanding aliases, so you can use history expansion to access arguments to the alias when it is used. Alternatively, define an alias to a command that works: Solution 2: There are many ways: You can make a variable for and/or as a shortcut for it.

How to create an alias for a user name on Linux?

You can’t, reliably. Not all auth mechanisms allow for this, either natively or hacked-in.

Now if you don’t mind it being an email alias, then just add a line to /etc/aliases and rebuild the alias DB.

What you looking for is an email alias, not an alias for the user name. Each user in Linux has one and only one name. However, setting up an email server is an entirely different matter. You can set up as many email accounts, with as many aliases to those accounts, as you like. Specifics of doing that are dependent on what your email package is.

Add john.smith user to /etc/passwd and set the same home and UID.

How to create an alias for a user name on Linux?, What you looking for is an email alias, not an alias for the user name. Each user in Linux has one and only one name. However, setting up an email server is an entirely different matter. You can set up as many email accounts, with as many aliases to those accounts, as you like. Specifics of doing that are …

Create alias for desktop directory

alias desktop='/home/bob-ubuntu/Desktop' cd desktop 

An alias is for a command name. A parameter to the cd command is not a command name. The alias is not used in this context.

If you type just desktop , this invokes the alias. But by default you’ll get an error

bash: /home/bob-ubuntu/Desktop: Is a directory 

Add the line shopt -s autocd to your ~/.bashrc so that typing a directory name in command position performs cd to that directory. This way you can change to the directory ~/Desktop by typing just ~/Desktop (instead of cd ~/Desktop ) or, with your alias, desktop .

Alternatively, define an alias to a command that works:

alias desktop='cd /home/bob-ubuntu/Desktop' 
  • You can make a variable for $desktop and/or $D as a shortcut for it.
  • You can alias desktop=’cd /home/bob-ubuntu/Desktop’
  • You can use $USER/Desktop
  • You can use $XDG_DESKTOP_DIR if XDG user directories is set.
  • You can add /home/bob-ubuntu to CDPATH environment variable of cd command

But you are really better off just using:

Note that you can also use tilda to switch to $HOME directories of many users in your system as follows:

cd ~root ls ~ftp echo ~nobody 

As icarus suggested in the comments, one simple way is to create an alias that does cd ~/Desktop

However if you really want cd to behave this way, you can alias a function to cd that handles checking if the directory is Desktop :

However this also causes tab completion for cd to break

Command — How to Create an alias in Linux?, Sorted by: 5. Edit your ~/.bashrc, adding the following line to the bottom: alias srm=»rm -i». Share. answered Jun 2, 2015 at 1:36. svart. 108 6. you can use doskey command to set an alias if you are a windows user like this doskey ls=dir then running ls will run dir command.

Alias with input. Unix

csh records your command in its history list prior to expanding aliases, so you can use history expansion to access arguments to the alias when it is used.

% alias myGrep grep -rn \!:1 . --color 

When you use myGrep foo , that two-word command is recorded in history, then it is expanded to grep -rn !:1 . —color . In that command, !:1 refers to the first argument of the previous command ( myGrep foo ), resulting in grep -rn foo . —color , which is actually executed.

The proper way to make an alias that takes a parameter is with a function. In Bash functions the arguments are assessed as $1 , $2 and so forth. Strung concatenation is implicit. Consider:

I is simply called like this:

$ hello Eduard Hello Eduard 

Bash — Set aliases globally for all users, If you want to make those changes for all users you can put that in /etc/bashrc or /etc/bash.bashrc whichever is present on your system and then make /etc/profile to source it. For example my machine is running ubuntu and it’s /etc/profile entry is this way. So if i put alias in /etc/bash.bashrc then they will be …

Creating multiple temporary aliases

generate all you session alias in a file, for instance alias.txt

alias x='cd /parent/child' alias y='cd /a/b/c' alias z='tail -0f some.log' 

You sould have all you alias in alias list, for this single session.

In former case, content is read ‘as if typed’, while the latter case will define alias in a shell that will exit.

How do I Create an Alias in Bash?, Create and launch the .bashrc using the nano command. If it is not available, an empty document would be opened. $ nano ~ / .bashrc. File .bashrc will be opened. Add the below line to the file to make aliases for an update of the system. alias update = » sudo apt update && sudo apt upgrade –y». Save the file and close it.

Источник

Читайте также:  Linux games in virtualbox
Оцените статью
Adblock
detector