Lab 12.2 - Further explanation required. Thank you
I do not understand the second part of the solution of lab 12.2
Question states:
but answer types ls /usr
Why is /usr included in the answer part?
Thanks in advance.
Answers
-
The point of the $PATH environment variable is which "ls" command is found and executed first. The system will use your $PATH environment variable contents to look for a command called "ls" in the order (from left to right) that it finds in your $PATH variable. So, we have two "ls" commands represented as files. One, the standard Linux system version, is found in the /bin directory (/bin/ls). The other, in this example, is found in /tmp (as in /tmp/ls--NOTE, this is not standard Linux!). So, the point of this exercise is to show you that the $PATH variable represents an ordered list of directories to search for the commands that you type in. In this case, the "ls" command. In the first case, you put "/tmp" at the end of the list of directories to search for commands. By default, "/bin" is earlier in the standard list of directories that are searched. So, /bin/ls (the stock and standard Linux "ls" command) is found first and is the program executed. We are using "/usr" simply as the argument to the "ls" command. We could just as easily used "ls ~" (your $HOME or login directory) or "ls /" (the root directory of the filesystem). We could have just used "ls" without an argument since it would default to listing the contents of our current working director (the directory where we currently are located within the filesystem tree). Again, the main point is that we are using the "/bin/ls" command rather than the "/tmp/ls" command.
In the second case, you put "/tmp" at the beginning of our search path in the $PATH variable. This means that "/tmp" will be searched first. If an "ls" executable file is found in "/tmp" (it is found there), it will be the one executed (instead of the standard Linux "/bin/ls" command!).
The point of all this is kind of subtle. Have you ever heard of the concept of a Trojan Horse? Yes, it comes from Greek Mythology, but in computer terms, it is hiding something that usually is malicious so that you use it instead of the real thing that you wanted to use. If you add a publicly writable ("/tmp" is writable by all users, so all users could put anything in that directory!) to your executable $PATH near the front of the path, you may end up executing a malicious program (that does bad things to you!) instead of the program that you intended to execute. For this reason, it is advisable that you only have Linux system directories at the beginning of your executable path and add less secure directories at the end of your executable path, and then only directories that you can trust.
1 -
@KevinCSmallwood thanks for your time and detailed answer. I really appreciate it.
Now I see I was focusing on a minor detail of the lab ( “/usr” part of the answer), and this prevented me from understanding the main point of the lab.
From your answer I understand that this lab presents 2 tricky parts, the first one is the name of the program “ls” and the second one is the location “/tmp”.
I can see that the first answer (putting the “/tmp” at the end of the PATH) does not solve the problem, since “/bin” directory has precedence over the “/tmp” one, and the second answer solves the problem (placing “/tmp” before the directory) since it forces “/tmp” to be read before the standard PATH. Conversely, the latter answer causes a security concern, since “/tmp” is publicly writable file by all users.
If it were to keep the program named “ls”, I can infer then that the problem would lie on the “/tmp” location where the newly created program “ls” has been placed. Allow me to kindly request you where, the new "ls" program should be placed (in which directory) to avoid the security problem that having it located on “/tmp” represents to find a solution.
Thanks in advance for your time and support.
Josep Maria
0 -
The simple answer is try not to name your commands after system commands if they have a different function. On the other hand, let's say you want to have your own "ls" command that lists the contents of directories (although I'll point out, if you read the manual page (i.e., "man ls"), you will find that the "ls" command has a lot of options and can do a lot of things that you may not be able to duplicate with your own program--don't reinvent the wheel), general convention says to put that in your "~/bin" directory. As I alluded, it would be better to call it something like "myls" and put it in your "~/bin" directory so that you don't have the name conflict. The convention "bin" stands for "binary" and is where you usually find executable programs (some may be compiled programs, and some may be executable shell scripts). You can create (if it doesn't already exist) and place your own executables in that "bin" directory. Let's go back to your other question about aliases. In this case, it would be better to call your "ls" executable something like "myls," put it in "~/bin/myls" (usually, but not always, "~/bin" may appear in your $PATH environment variable, but you know how to add it!), and if you really want "ls" to always execute your "myls" executable, then do this:
$ alias ls myls
As you now know, type "ls" at the command line prompt will then have the system execute the "myls" command. As long as that name is unique in the search path, it will find it and execute "myls". It is very common for some people to have aliases for various "ls" commands and options. For example:
$ alias lls 'ls -l'
That makes the "lls" command automatically execute an 'ls -l'. So, with this alias, you could do something like:
$ alias lls 'ls -l' $ lls /usr # NOTE that "ls -l" will be substituted for "lls" in this example! <the result of an "ls -l /usr" will be shown> $
Keep working through the course. I've been working with UNIX and then Linux since 1980 and still learn new things! As I like to say, you never stop learning until you die, and then, maybe you learn something new! ;-)
1 -
@KevinCSmallwood thank you for taking the time to provide this accurate answers. I really appreciate it. Thanks to your answers I now understand better many Linux concepts. I shall continue studying through the course.
1 -
You are most welcome, and I'm glad it was useful!
0
Categories
- All Categories
- 167 LFX Mentorship
- 219 LFX Mentorship: Linux Kernel
- 795 Linux Foundation IT Professional Programs
- 355 Cloud Engineer IT Professional Program
- 179 Advanced Cloud Engineer IT Professional Program
- 82 DevOps Engineer IT Professional Program
- 127 Cloud Native Developer IT Professional Program
- 112 Express Training Courses
- 112 Express Courses - Discussion Forum
- 6.2K Training Courses
- 48 LFC110 Class Forum - Discontinued
- 17 LFC131 Class Forum
- 35 LFD102 Class Forum
- 227 LFD103 Class Forum
- 14 LFD110 Class Forum
- 39 LFD121 Class Forum
- 15 LFD133 Class Forum
- 7 LFD134 Class Forum
- 17 LFD137 Class Forum
- 63 LFD201 Class Forum
- 3 LFD210 Class Forum
- 5 LFD210-CN Class Forum
- 2 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum - Discontinued
- 1 LFD233 Class Forum
- 2 LFD237 Class Forum
- 23 LFD254 Class Forum
- 697 LFD259 Class Forum
- 109 LFD272 Class Forum
- 3 LFD272-JP クラス フォーラム
- 10 LFD273 Class Forum
- 152 LFS101 Class Forum
- 1 LFS111 Class Forum
- 1 LFS112 Class Forum
- 1 LFS116 Class Forum
- 1 LFS118 Class Forum
- LFS120 Class Forum
- 7 LFS142 Class Forum
- 7 LFS144 Class Forum
- 3 LFS145 Class Forum
- 1 LFS146 Class Forum
- 3 LFS147 Class Forum
- 1 LFS148 Class Forum
- 15 LFS151 Class Forum
- 1 LFS157 Class Forum
- 33 LFS158 Class Forum
- 8 LFS162 Class Forum
- 1 LFS166 Class Forum
- 1 LFS167 Class Forum
- 3 LFS170 Class Forum
- 2 LFS171 Class Forum
- 1 LFS178 Class Forum
- 1 LFS180 Class Forum
- 1 LFS182 Class Forum
- 1 LFS183 Class Forum
- 29 LFS200 Class Forum
- 736 LFS201 Class Forum - Discontinued
- 2 LFS201-JP クラス フォーラム
- 14 LFS203 Class Forum
- 102 LFS207 Class Forum
- 1 LFS207-DE-Klassenforum
- 1 LFS207-JP クラス フォーラム
- 301 LFS211 Class Forum
- 55 LFS216 Class Forum
- 48 LFS241 Class Forum
- 42 LFS242 Class Forum
- 37 LFS243 Class Forum
- 15 LFS244 Class Forum
- LFS245 Class Forum
- LFS246 Class Forum
- 50 LFS250 Class Forum
- 1 LFS250-JP クラス フォーラム
- LFS251 Class Forum
- 154 LFS253 Class Forum
- LFS254 Class Forum
- LFS255 Class Forum
- 5 LFS256 Class Forum
- 1 LFS257 Class Forum
- 1.3K LFS258 Class Forum
- 10 LFS258-JP クラス フォーラム
- 111 LFS260 Class Forum
- 159 LFS261 Class Forum
- 41 LFS262 Class Forum
- 82 LFS263 Class Forum - Discontinued
- 15 LFS264 Class Forum - Discontinued
- 11 LFS266 Class Forum - Discontinued
- 20 LFS267 Class Forum
- 24 LFS268 Class Forum
- 29 LFS269 Class Forum
- 1 LFS270 Class Forum
- 199 LFS272 Class Forum
- 1 LFS272-JP クラス フォーラム
- LFS274 Class Forum
- 3 LFS281 Class Forum
- 9 LFW111 Class Forum
- 260 LFW211 Class Forum
- 182 LFW212 Class Forum
- 13 SKF100 Class Forum
- 1 SKF200 Class Forum
- 1 SKF201 Class Forum
- 782 Hardware
- 198 Drivers
- 68 I/O Devices
- 37 Monitors
- 96 Multimedia
- 174 Networking
- 91 Printers & Scanners
- 83 Storage
- 743 Linux Distributions
- 80 Debian
- 67 Fedora
- 15 Linux Mint
- 13 Mageia
- 23 openSUSE
- 143 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 348 Ubuntu
- 461 Linux System Administration
- 39 Cloud Computing
- 70 Command Line/Scripting
- Github systems admin projects
- 90 Linux Security
- 77 Network Management
- 101 System Management
- 46 Web Management
- 64 Mobile Computing
- 17 Android
- 34 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 371 Off Topic
- 114 Introductions
- 174 Small Talk
- 19 Study Material
- 507 Programming and Development
- 285 Kernel Development
- 204 Software Development
- 1.8K Software
- 211 Applications
- 180 Command Line
- 3 Compiling/Installing
- 405 Games
- 309 Installation
- 97 All In Program
- 97 All In Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)