If you ever need to delete users, use the userdel command, as follows:
userdel username
You need to have superuser permissions. Make sure that no processes from the user you are trying to delete are running, or otherwise userdel will fail to execute. (“How to Delete/Remove Users in Linux (userdel Command),” n.d.)
If you ever need to add a user to your computer (I had to once), then use adduser. It must be run as the superuser, so be sure to prefix it with sudo. (“How to Add and Delete Users on Ubuntu 18.04,” n.d.) Its usage is as follows:
adduser username
You can find more details in the reference above. A low-level alternative to adduser is useradd, but I had adduser available on the machine I was adding the user on (it had Debian 9 on it).
The passwd command can be used to change your (or other users) password. (“PASSWD(1),” n.d.) Just type in passwd as the command and the password change process will be initiated.
If you are the superuser, you can change the password of another user by typing his/her username:
Today let’s talk about default file permissions. We know that we can change file permissions with the chmod command, but where do default permissions come from?
The answer is that default permissions come from the application that produced the file in question. (“What is ‘umask’ and how does it work?,” n.d.) Then those permissions are modified with umask. (Shotts, 2019) Think of umask as a bit mask which “shuts off” certain file permissions. Before we delve into this, let me just say that you won’t be using umask much at all (if ever), but it is very illuminating to know how file permissions get set, in my opinion.
Let’s take a look at an example. Here I printed my umask:
mislav@mislavovo-racunalo:~/Linux_folder$ umask
0022
Let’s take a hypothetical application which produces files with these permissions:
rw-rw-rw-
or in binary:
110110110
Then we apply umask to the default file permissions to mask certain bits of the file the application produced. Our umask in binary is (ignoring the leading (leftmost) zero):
000010010
So in the end we have:
110100100
We see that umask has “shut off” (turned to 0) the bits which correspond to the places where umask is 1.
To set your own umask, type:
umask newMask
I never had to mangle with this, but I think that it pays to know this because this term does tend to pop up from time to time in various tutorials. It also helps paint the picture of Linux.
Hope you learned something new!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 122-124
The chown command is used to change the owner and/or the group of the file. (Shotts, 2019) You need superuser permissions to do this. To change only the owner, its syntax is as follows:
chown newOwner file
If you want to change both the owner and the group, use:
chown newOwner:newGroup file
If you want to change just the group:
chown :newGroup file
As I said in the post where we talked about file permissions, I never needed to mangle with groups.
Hope you learned something new!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 128-129
Let’s talk about how to change the permissions of a file. To do so, let’s first talk about octal and binary numbers. Before you get all whiny and say “Oh jeez, why binary? Why do I need to know binary?”. Well, you don’t really, but I think it greatly simplifies things, so that’s why.
Let’s look at user permissions (same applies for group and world permissions). If you have:
rw-
that could be represented in binary as:
110
since binary has only two digits – 0 and 1 – so this could be a valid choice. We have a 1 where we have the right to modify the file in a certain way “enabled” and 0 where we have the right to modify the file in a certain way disabled.
Now, how do we read 110 in binary? Let’s first see how do we read it in decimal.
What you actually do is this: 110 is 0 * 10^0 + 1 * 10^1 + 1 * 10^2 (looking the digits from right to left, where ^ is the exponentiation operator). That is, the rightmost digit is the least significant one and it gets multiplied by 10^0. The digit to the left of it is multiplied by 10^1 and so on. The same is in binary, but instead of using base 10, we use base 2. So 110 will be:
0 * 2^0 + 1 * 2^1 + 1 * 2^2 = 6
Now, let’s pay attention to the highest possible number with 3 digits in binary, which is 111. By following the same logic 111 in binary is 7 in octal (1 * 2^0 + 1 * 2^1 + 2 * 2^2 = 7). Note that octal has 8 digits – 0 through 7.
So now, the problem reduces to: Figure out what octal digit represents the file permissions we want. Let me give you a list:
0 in octal – 000 in binary
1 in octal – 001 in binary
2 in octal – 010 in binary
3 in octal – 011 in binary
4 in octal – 100 in binary
5 in octal – 101 in binary
6 in octal – 110 in binary
7 in octal – 111 in binary
When you look at binary representations of the octal numbers, you can see the correspondence. For example, 5 in octal is 101 in binary, which would look like r-x, meaning we are able to read and execute the file in question.
Now we can come to chmod. chmod is used to change file permissions. It is used as follows:
chmod <filePermissions> <file>
where <filePermissions> are 3 octal numbers in a row, each representing permissions for the user, the group and the world, respectively. Only the file owner or the superuser can change file permissions. (Shotts, 2019)
You can also use symbolic notation to change permissions (you can check out the man pages for chmod or you can Google how to use symbolic notation with chmod), but I believe that octal is easier to use once you understand it. I do use chmod +x file to add executable permissions to a file and that is a fast way to do so, but whenever I need anything more that that, I use binary file permissions.
Hope you learned something useful!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 118-122
First of all, let’s talk about why are file modes and permissions important. Well, if you don’t have the right permissions, you might not be able to modify certain files (either read, write or execute them). That’s why sometimes you get the error saying “Permission denied”. That is exactly what happens – you don’t have the right permissions.
Every Linux file has a set of permissions that determine whether you have the rights to read, write or execute the file. (Ward, 2014) Let’s employ ls -l to find out permissions of a certain file:
mislav@mislavovo-racunalo:~/Linux_folder$ ls -l
total 36
-rw-r--r-- 1 mislav mislav 8 Feb 5 23:09 1264.txt
...
Let’s focus on the 1264.txt file. The permissions part is the following:
-rw-r--r--
Let’s break it down, looking from left to right:
- - this indicates the file type; - indicates a regular file, d indicates a directory and there’s some other file types as well (like links (l), which we will cover later; just know that they exist)
rw- – this indicates the user permissions – this file was created by the user mislav and this user can both read and write the file
r-- – this indicates the group permissions – this file belongs to a group called mislav (the 4th column in the ls -l output) and it can only read the file; it can’t write to it
r-- – this indicates the world permissions – the permissions of everyone else who is neither the user nor the group member
- means that that option is not supported and a letter in place of - means that that operation is supported. If in the last position x is used as a letter, that means that that the file is able to be executed.
A note on groups: If only you use your computer, you most likely won’t have to mangle with groups. Groups are sets of users – a convenient way to modify permissions for a set of users within a group (you can add users to groups and remove users from groups and change the group a file belongs to), but I never had to mangle with groups myself.
Hope you learned something useful!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 10; 33-34
14417 mislav 20 0 4222052 321880 168376 S 0.3 4.0 0:31.67 anki
...
First we have some information about current time, how long my machine has been running and how many users are logged in (Shotts, 2019)
load average – how many processes is your computer executing in the last minute, last 5 minutes, last 15 minutes
Tasks – how many tasks are there on the computer – 1 is running (meaning being actively executed), 239 are sleeping (meaning waiting for something to happen (such as data from a device) to resume their execution), 0 stopped (meaning no processes whose execution was paused manually) and 1 zombie process (zombie process is a process whose parent process doesn’t exist anymore)
%Cpu(s) tells us what percentage of the CPU is being used on what kinds of processes: us is for user processes, sy is for system (kernel) processes, ni is for nice (low-priority) processes, id is for the percentage of the CPU that is idle, wa is for percentage of the CPU waiting for some input/output tasks, hi is the time spent processing hardware interrupts, si is the time processing software interrupts and st is relevant to virtual environments – if you don’t know what virtual environments are, that needn’t concern you (“Linux ‘top’ command: What are us, sy, ni, id, wa, hi, si and st (for CPU usage)?,” n.d.)
MiB Mem and MiB swap tell you how much RAM and how much swap space is being used (measured in mebibytes (“Mebibyte,” n.d.))
Let’s now look at the columns available for each process: (“A Guide to the Linux ‘Top’ Command,” n.d.)
PID – proces ID
USER – user who owns the process
PR – process priority
NI – niceness (nice value) of a process
VIRT – total amount of memory consumed by the process (permanent storage device + RAM – the basic idea is that if the operating system runs out of RAM, it can use some memory available on the permanent storage device in addition to the RAM; this concept is called virtual memory – look it up on Google if you are interested)
RES – memory consumed by the process in RAM
SHR – amount of memory shared with other processes (processes can share memory)
S – process state (is the process running, is it sleeping, …)
%CPU – how much CPU is the process using (in percentages)
%MEM – how much memory is the process using (in percentages)
TIME+ – total time used by the process since it started
COMMAND – the name of the process
Hope you understand the output of top in detail now and hope you refreshed your memory!
Every process has to be instantiated (created) by some other process. The process which created the process is called the parent process and the created process is called the child process.
In this short article I would like to mention a few commands that enable you to monitor your computer resource usage. They are: (Ward, 2014)
vmstat
iostat
iotop
pidstat
I never used any of the above commands and so I am leaving it here for you to explore if you ever have the need. So far, I haven’t had the need.
A note: To understand the output of some of the commands above, you may need to understand how computer memory works in modern computers. In particular, you need to understand what is virtual memory and what are pages and what is a page fault. A Wikipedia article read on the topics mentioned in the previous sentence and further Googling on the topics that you don’t understand will be enough for you to gain a grasp of what is going on.
Thank you for reading!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 183-188