Imran: Creating users or global aliases.
Now we have learned how to create aliases right
on your own session, but you might not know one thing,
that as soon as you log out of your console
or PuTTY session where you have created your aliases,
that aliases, they will go away.
There is no way to save them.
Now how can you save aliases
so every time you log in, they remain for your session?
Well, there are basically two type
of aliases that you could create in a user environment.
One is user that applies only to a specific user profile.
And then the next one is the global alias.
Global alias applies
to everyone who has an account on your system.
So if you create one aliases, every user who logs
into the system will be able to access that alias.
If you wanted to set up an alias specific to only one user,
then you have to log into that user
and then go into the user home directory.
Inside of the home directory
there's a file called .bashrc.
Dot files, anytime there's a dot in front of a file,
it means that file is a hidden file.
So you would have to actually specify .bashrc.
For global aliases, if you want to set up a global aliases,
you have to specify that in the etc configuration file,
and that is etc/bashrc.
So let's take an example of an aliases.
Let's say if I wanted to set up an alias
that will look like this, I want it to run hh
and I want it to give me the host name.
Instead of me typing the entire host name,
I just want it to do hh.
So let's go into my Linux machine.
I am logged into my Linux machine right here.
And if you notice, when I run hh it says command not found.
So I need to create an alias for it.
So when I type alias hh equal host name,
now if I type just aliases, you'll see
that this aliases has been added to my environment.
Okay, perfect, now, if I just type hh,
it will give me the host name, right?
That's perfect, now remember this setup
of aliases stays here as long
as I'm logged into this console session.
As soon as I'm out of this
or I am logged in from another PuTTY session,
let's say, I won't be able to access this alias
because this alias is tied to this particular session.
So as an example, I could show you right here
or if you've noticed I have this PuTTY session,
I'm logged into the same Linux machine, I will become myself
because in the other session right here, that's myself too.
Who am I?
That is myself, so I'm gonna become myself.
And now when I type, who am I?
You'll see I'm logged in as myself.
When I type hh, you'll see command not found.
See, same user, but different session.
But this hh, it's actually tied
to this session or this terminal.
So if I wanted to make that global, meaning,
not sorry, global, if I wanted to make that tied
to my user account, then what I have to do
is I would VI my .bashrc file,
which is right here in my home directory.
If I do LS minus LA,
to A is to view the hidden files as well,
you're gonna notice that there's a file called bashrc.
So go ahead and VI .bashrc file.
Go to the end of it and you could put a comment,
personal aliases, and then you put an alias, the name
of the alias equal and the command name, save the file.
And now if you go in and put in another session,
so let's say this, it's not gonna show up right away
on this session because this session
is still holding the old environment.
So you have to create a new session.
So go click on this icon and click a duplicate session.
It will open up another PuTTY session,
log in with your username, and now if you type hh,
you'll see your host name is there.
So now that's how you could tie up your alias
with a user account.
Now what if you wanted to tie up that alias
to everyone who logs into the system, which is called what?
Global alias, so in that case, let's say if I become,
let me do a duplicate session again.
If I become root and log in as root and I type hh,
you see root doesn't have access to that alias.
So I wanted to make that alias available to everyone.
So for that, first of all, you have to be root
because every time you need
to modify the etc configuration file, you have to be root.
So in root, oh, sorry, while you're root,
make sure you're root, type, who am I?
Then go into VI the file, etc/bashrc file.
In this file, go to the end of it by hitting Shift G
and then put your comment as global aliases.
And here you could type alias hh equal host name
and save this file.
Now again, if you type hh right now,
it's not gonna work because this session,
the hh is tied up to the user accounts globally.
So you have to log in again.
So I'm gonna go ahead and close this session.
I'm gonna go ahead and close this session
and I'm gonna use the same session.
I'm gonna start as a duplicate session.
So the first one I'm gonna try iafzal.
And I'm gonna see that as iafzal,
I have access to the alias.
But now you're probably thinking, Hey,
I added that to the user profile.
That's what you do.
Okay, that's fine, now let me log into the root account.
So I'm gonna go in and I'm gonna type root
and I'm gonna log in as root, who am I?
I am root, and now if I type in hh
and you'll see the aliases right there.
You can also type aliases
to see what is inside of the environment,
what is set as an aliases, you see my alias
is set as this one as well.
If you have more users in this machine
you could try to log into that user.
So if I do cat etc password to see which other users I have
I have created some of the users, but then I deleted it.
So let's say if I create a new user,
user add, I create a new user called James.
James is created, let me set up a password for James.
That's fine, okay, password is set up.
Let's clear the screen
and let's actually exit out of the screen.
Let's exit out of this screen too.
Now I'm gonna log in.
Let's do double duplicate session.
Let's log in as James now,
and let's see if that hh alias
is also accessible by James, so hh, sorry, not J.
And you see that alias that we set it up in etc/.bashrc
is now also accessible to James as well.
So that's how you set up user aliases and global aliases.
It's something that you should remember when you log out
and you don't add the aliases to these files,
it will go away.