LPI 102-400 – 105.1 Customize and use the shell environment
So we’re going to talk about paths in Linux. The dollar sign here defines the dent-my-space. PA commands. she searches pa-h for commands. It directories unseparated list of directories commands. the shell looks for commands. And Jiu were gild “an example, ifhome”rand “code,” chowouldlar sign home and directory. I’ll show me my hokey? directory.
There’s a file where you can set all of these paths permanently. And the file is called a “dot profile.” It is located by default under every user’s home directory. And this is what the file looks like. It’s a very small file, and these are currently the settings for the path. And if you want to change it and include a custom path, then you can just basically type “o” and add the path that you want. Assume I want to include export path equals dollar sign path bin. So bin will be added to the end of this path after my home directory. Bin will also be part of my path once I save it. But I’m not going to save it; I’ll just get out of it. I just wanted to show you how to do it if you want to do it permanently.
Hello students, and welcome to this first section of the LPIC exam preparation. So in this section, we’re going to discuss COVID shells and shell scripting. And the first subsection in this section is on customizing and using the shell environment. The weight of this section means that this subsection will have four questions on your exam. The description says the candidate should be able to customise shell environments to meet users’ needs.
Candidates should be able to modify global and user profiles. The key knowledge areas are setting environment variables, for example, paths at login or when spawning a new shell. Write Bash functions for frequently used sequences of commands. Maintain skeleton directories for new user accounts. Set command search paths to the proper directory. And this is a partial list of the used files, terms, and utilities in this subsection. The Etsy profile is the first: file environment or end export set unset Bash underscore profile. Bash highlight login profile bash underscore bash underscore logout, function, and alias.
So one of the common elements in shell environments is the dot. In shell, the use of dot or dot means a lot. And we are going to try to understand this right now. So what the dot means is the current directory; the dot means the parent directory. And let’s try to understand that by giving examples. So I’ve done an ls, and these are the directories that I have currently under this user’s home directory. And what I’m going to do is go to scripts, which is one of my subdirectories, and do an LS there. And as you can see, I have a shell script there.
And let’s say I want to run it and do a first sh, but the shell doesn’t find it. The reason it doesn’t find it is because it’s not going to run from the current directory, which is not in the path. If I do an echo dollar sign path, you can see that the home student scripts directory is not in the path. You do see a home student’s script, but there is no script directory in there. And that’s why if I need to run my script, I need to type to let the script know or let the system know that the script resides in the current directory. And as soon as I do this, I’m able to run my script, which is a Hello World program. So how do you avoid this extra step and create a path where scripts are part of my path and I don’t have to type a dot slash, if that is your choice? A lot of people like to run scripts with the dot slash, and that’s fine with them. But if that’s a script that you run quite often, or suppose you’re running it in a cron job, and you want to run it without the dot slash, then you have to place it in the path. How do you do that? You type in “export path equals dollar sign, dollar sign path, column, home student scripts, end of code.” Okay? And once you have done this, then you can source it.
So that way, the change will take effect right away. profile and source; basharc is acceptable in some cases. Or you can source both of them. And now when we run it, as you can see, we can run it without the dot slash. I just did it. And now it recognises that this script directory is part of my path. And how can I check it? Follow the path of the dollar sign. And, as you can see, this line extended all the way to here. Now it has appended this part as well. So now this is part of my path as well. And whatever I place in here and try to run as a script, it will run without the dot slash. So in this lecture, we have learned what the significance of a dot is and how you can place something in the path. So you don’t have to use the dot slash if you want to go to your previous directory. Remember, I have a parent directory. So we’re currently working on home student scripts. If I do a CD, then I am a home student. so it moves up one directory. I’ll be fine if I do another CD-D and a PWD now. If I do a CD one more time, then I’m in the slash directory. And what will happen? If I do CD only, I’m back to my default home directory for students.
Okay, to see what the current environment is that I have configured, I can just type in ENT, and it’ll show me all the different variables that are set up for my current environment, including my path, my log name, my shell, and everything else. And suppose I want to run the current session without all these variables. I can do so by typing envy Bash just for this session. And if I type envy now to see what my environment looked like, look at how much it has shrunk. So now it has provided me with just the basics I need to run the bash command.
Suppose if I were to run a simple command like “Clear,” I could have done so very easily previously, but now it says no, it’s not set in your variable or in your PAP, so you can’t run it. And suppose I move and want to return to my regular session with the entire environment set up. I just typed “exit,” followed by “EMV,” and my environment has returned. And now, if I run clear, it will let me. Let’s talk about another very important variable, which is set. And what does the set command do? It creates a local variable and sets it to any value that we can customize it to. And then we can use unset to remove the local variable. So let’s start. We’re going to do VAR-1 for variable-one equals and then single coding. Let’s type Linux is fun; press enter. And if I were to echo the dollar sign, it says Linux is fun. So this is a temporary set command to set a variable. Now I want to unset it, unset VAR 1, and let’s echo what my current variable looks like.
And now it’s gone because we’ve just done the unsetting. Another way of creating a local variable is by using the export command. The local variable created will be available for the current session only. To unset the variable, we’ll simply set its value to blank. So let’s see how that’s done. So exporting VAR to Linux is easy. Okay, echoing the dollar VAR, Linux is easy. Now I want to unset it as VAR equals, and then I’m going to give it no value. And if I were to do an echo dollar VAR, it’s been unsettled already because I didn’t assign it any value. Now, let’s look at how to configure user-wide environment variables in Linux. So currently, we are in the student directory. If I were to do an ls, it only shows directories, but if I do an ls el, it shows me the hidden files as well. Hidden files are filenames that start with a dot. And one of the files in here, as you can see, is bashrc. So Bash RC is going to be automatically created in your home directory. So what we’re going to do is edit this file and insert an export statement in there.
So we’re going to VI RC. And what I’m going to do is, just at the top of the file with this blank line, I’m going to press I for insert and I’m going to type in “export CD” for change directory, and it’s all caps instead of lowercase like we usually do. And I’m going to type in “have a nice day” and “end of single code.” And in order to make that change take effect right away, I’m going to do a source RC, and now I’m going to play an echodollar sign CD, and it says, “Have a nice day.” So now I have customised my Bash RC file and added this extra statement in there. And, of course, this is a meaningless statement, but if you want to use something more meaningful where you’re changing the path or something more meaningful, you can include it. And how do you remove this? Of course, you go back into the Bash RC file and either comment it out just like the other files or other lines, or you can simply type DD and it’s gone, and save and exit. And then, in order to make sure the change takes effect, we’re going to do a source RC that has not been saved.
to add a variable that will be available for remote login sessions only. That is, when you SSH to the user from a remote system, we modify a file called the Bash underscore profile. So I’ll use VI Bash underscore profile. And I have a line in here already. And you can type something, similar or more meaningful, that you actually have a use for if you remotely log into this machine and want this message to appear. So I’m just going to exit out of it, although I’ve already sourced it. But it’ll be a good idea to source underscore profiles and then do an echo of dollar sign VAR. And that’s my line that I have in that file. And what I’m going to do is SSH to this computer that I am on right now. So the IP is 10 dot, 0 dot, 2 dot, 11 dot. I’m logged in right now.
And if I were to do an echo hereof dollar sign VAR, I’m going to see the Bash profile file or the modification that I made. I see it. So just think of it as me logging in from a remote machine right now. I used the same computer to log in. However, if you were on a remote machine and did an echo dollar sign of the variable, you should be able to see what kind of change you made in that. Okay, what if I want to set an unset system wide environment variable on Linux to add a system-wide no login variable that is available for all users when any of them opens a new terminal, but not when any user of the machine is remotely accessed? It must be added to direct or file. First of all, we have to become a super user.
Now I’m a super user. I need to navigate to the slashed directory and then to the etc. directory. I have to view this file, Bash Bashar. And in here, I’m going to add a line just to show you what kind of change this is going to reflect. So the line I’m going to add is export VAR equals. This is a system wide variable. It’s just a text line that I have added in here, and I’m going to remove it after we are done digging. And as usual, I’m going to save and quit.
And also, as usual, I’m going to source it. So source easy bash. bashers is source now, and that variable will not be available for every user when he opens a new terminal shell. As a result, I’m now logged in as my non-root user. And if I echo “dollar bar” and it appears as a system by variable, this is the line I added to the system by profile. Suppose if you want any environment variable to be available when any of the users on your machine are remotely logged in but not on opening any new terminal on a local machine, then you need to edit the Etsy profile.
Let’s do that. Anytime you are going to change something in Etsy and a subdirectory, you have to be a super user. The device is not going to give you permission to change anything underneath, etc. So I’m going to source the Etsy profile, and if I were to SSH to this machine now, it would be 100 211, and if I do an echo VAR one, you’ll see this variable. This is a system wide variable that I have just set up for remote users only. So right now I’m remotely logged into the machine.
In your home directory, you’re going to find another very important file that controls the behavior of logout when you’re logging out from the terminal. So the file is called Bash logout. And if I do more on this, it’s a very small file, but it tells you that this controls the behavior. You can change it while leaving the console. Clear the screen to increase privacy. And this is what the script does. Does the script clear the console before you exit or close this console? So that way, there will be no history left on whatever you have run so far. On some machines or in some environments You will also see a file called Bash underscore login.
Similarly, the login behavior that is controlled out of that one will be replicated in that file. It will look very similar to this file. Another important file is the dot-profile file. And if I do an LS off-profile, you’ll see this file here, and then I’ll do an on-profile. And this tells you what the home directory is going to be. And the BSHR brash, RC will be in your home directory. what the path is going to be. Keep in mind that this is for the user only. for the currently logged-in users, not global as it is not system-wide. It’s known as a “dot profile,” and it’s installed by default in any new user. So in Bash, you can define aliases for commands. You use aliases to provide an alternative name for a command. to provide default parameters for the command or a more complex command. You set aliases or list aliases with the aliases command and remove them with the unalias command.
If you were to do an alias LS on ll, you are going to see these two default ones. And if I want to add one for a long listing in reverse kernel logical order, I can do something like this: alias LLRT is the same as LS LRT color; auto enter Okay, so that is set. If I do LSRT of slash Bin, you can see that the shortened command or the alias that we just created, LLRT, is not a command in general. LS-space LRT is a command. So we were able to run it exactly like llrtBin and get this. And this is how you set up aliases. What if I want it unaliased? Well, I just gave you the clue. I will unleash it as LLRT. And now if I type LLRT, it’s not going to work because I have just removed it.
Okay, in this lecture, we’re going to discuss the scale directory. The scale is driven by the skeleton, and it’s used to initiate the home directory when a user is first created. If you want to see what this directory looked like, check out lair, t, and easy scale. And this is how a generic Etsy scale file appeared. And if we do more on the Etsy default user ad, which defines the scale directory, you can change the default location to Etsy scale, of course, or to any other location. And here you can see the details of how the files are going to get created in this directory.
And the group is going to start at 100. And the default group for users and if you remember, we talked about the default shell. So I’m going to make a user and add a default file. And what that will do is ensure that any time you create a new user, they’re going to have the profile that I am going to describe in that file. And the way I do it is with a VI ETSC default user ad. This file is already there, and what I’m going to do is uncomment parts of it. So the first thing I’m going to do is make the shell. So right now the shell says “bin sh.” I want to change it to “bin bash.” So BA, I’m going to add it in there.
They’ll make it bin-bash. And the next thing I want to do is change the home directory. Actually, I can leave the home directory as it is. So the home group will start at home group. I want to start at, let’s say instead of 100, I’m going to start at 1000, right? Okay. You can also leave inactive the number of days after a password expires until the account is permanently disabled as the default. and I’ve uncommented that also. So it’ll be inactive after it away expire. There’s no expiration. So I’ll just leave that blank. That means there is no expiration. And the scale directory I want to keep defaults to Etsy scale.
And the last one is “create mail spool.” And I’ll agree to make mail spools. so I’ll uncomment that as well. And there we go. We have just created a user ad. And from now on, whenever we create a user, they’re going to have this profile that we have just set up. Okay? Now we’re just going to create a user, and we’ll see if it turns out that the profile that we’ve set means that user is automatically getting it or not. Because we have set up a profile in the Etsy skilled user ad directory. So add a user. Let’s call him Jose. Add user. Jose. Okay, password again. Don’t worry about the rest of the information. Everything is correct. And now I am going to look at the Etsy password file. And this is our user, Jose. He’s right here. And if you see my profile, it’s supposed to be in the thousands. So of course it’s got 1010 because my previous one was 1009. From there, the user ID is also added. The previous one The home directory is supposed to be slashhome, and the shell is supposed to be bash. So our profile worked, and the new user took over all of the new profiles.
So in this lecture, we’re going to talk about functions. Functions are basically a grouping of commands. Assume I want to know the uptime of a specific machine, who the users who are currently logged in are, and what the host name of this machine is. Then I can group all of these commands together in one functional block lock and just run that command or run that function, which will give me all this information. So let’s see how to do that.
We’ll go to our Bash RC file and do it somewhere convenient, let’s say over here. So I’ve inserted it and I’m going to start typing. Let’s name our function first. I’m going to call it “info,” and you start off with the parentheses and then the curly brackets. And then we’re going to echo “e” and start a new line. You don’t have to do this, but I just want to make it kind of clear. And let’s call it hostname, end the code with a colon, and then give the actual command that’s going to run to give us that information. So you name is the command, and you name a, and then I’ll do a second function or part of a function e. And again for the new line logged in, followed by users, a colon, the end of code, and a semicolon. And what is the command for that? Who. If you just type in who is on the command line, it will give you the information.
So that’s what I want this function to do. And then echo “e” and “nuptime,” end of codes, a semicolon, and then the command “uptime.” Okay, so this is the end of our function. And then I want to type in my curly bracket just to end that. Let me remove this extra space. It is probably a good idea to leave the space there. Okay, so that’s our function and WQ, and we quit. And then I’m going to call our function Info. And as you can see, this is actually being typed out; actually, that was the wrong name to give to let me do a Bash RC because information itself is a command. So I should not have used that. And I’m going to change the name of our function. How about that? One thing I forgot to mention is that you have to source the Bash RC file. So what is that source? And then I’m going to type “host info,” which is our function, and it’ll give you all the information that we asked for. Provide you with the host name, currently logged-in user, and uptime. So this is how you create a function in Linux.
Popular posts
Recent Posts