chmod – change permissions of a file or directory.

Jan 23 2012

The chmod is an essential command in any linux environment. The chmod command allows you set access permissions on files and directories. File permissions allow you to set access to files and directories. In the example below the first letter tells us if we’re looking at a file or a directory. If we’re looking at a directory it will be denoted with the letter “d”, otherwise it’ll be a file and simply denoted by a “-”. This is a really easy way to tell if you’re looking at a directory or a file. As for the permissions themselves they’re broken up into three main sections. We’ve got “u” which denotes the user/owner of the file, “g” which denotes the permissions a specific group has, and “o” which denotes all other users on the system. Each one of these sections has a “rwx” flag which denotes “r” read, “w” write, and “x” execute. When “x” is used on a directory it doesn’t mean “execute”, it means to “traverse” in to the directory. What this basically means is that you’re able to allow the user, group, or other to traverse into the directory.

chz@cmdlist:~/Downloads$ ls -lah
total 103M
drwx------  4 chz chz 4.0K Jan 22 17:54 .
drwxr-xr-x 31 chz chz 4.0K Jan 23 13:23 ..
drwxr-xr-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3
-rw-r--r--  1 chz chz 907K Jan 22 17:53 midori-0.4.3.tar.bz2
drwxr-xr-x  6 chz chz 4.0K Oct 25 20:56 OpenELEC-ION.x86_64-1.0.2
-rw-r--r--  1 chz chz 102M Jan 22 12:14 OpenELEC-ION.x86_64-1.0.2.tar.bz2

 

Still feeling a little lost? It’s ok. We’ll break down the example above to get a better understanding on how permissions work. Let’s take the third line in our example above. As you can see the first letter is “d” so we know midori-0.4.3 is a directory. Next we have “rwx” which means our user/owner has read, write, and execute permissions. Then we have “r-x” which means that our group, in this case “chz”, has both read and execute permission. Finally we have “r-x” again which would mean that all other users have read permission, and the ability to traverse the directory. However, they’re not allowed to write to the directory.

d rwx r-x r-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.30

 

With linux there are several ways to accomplish the same task. This is no different when it comes to chmod and permissions. We can modify our permissions by executing something like “chmod g+w midori-0.4.30.tar.bz2″, this would allow the group “chz” to write to the “midori-0.4.30.tar.bz2″ file. However, we could also use octal (read numerical) values to represent the same thing as “chmod g+w”. Most would say that using numerical values to change permissions is the quickest way to adjust file and directory permissions. Let’s take a look at how exactly the numerical numbers are broken down. As you can see in the example below I’ve added a series of three digit numbers. To convert the numbers we’ll use a simple formula. The read permission is equal to the numerical value “4″, the write permission is given the numerical value of “2″, and finally the execute permission is given the value of “1″. Therefore, if we were to breakdown our example below we would be give have the permission “755″ for our midori file. How did we get this? It’s pretty simple, we’ll just add all the numbers up. So for example 4+2+1=7, 4+0+1=5, and 4+0+1=5. This gives us our numerical value of 755.

- rwx r-x r-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.30.tar.bz2
 421 401 401

 
Examples:

So now that we know a little bit about how permissions work, let’s take some of that knowledge and put it to use. First let’s change permissions on our midori-0.4.30/ folder so that any users in the group “chz” can write to the directory. To do this we’re going to simply execute the command below. The “g” stands for group, the “w” stands for write, and both are joined by + which literally means group (plus) write.

chz@cmdlist:~/Downloads$ chmod g+w midori-0.4.3/
chz@cmdlist:~/Downloads$ ls -lah
total 103M
drwx------  4 chz chz 4.0K Jan 22 17:54 .
drwxr-xr-x 31 chz chz 4.0K Jan 23 14:01 ..
drwxrwxr-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3

 

In this example we’re going to do the exact opposite. This time we’re going to remove the write permission to the midori-0.4.3/ directory. To do this we’ll simply add chmod g-w midori-0.4.3/. The “g” stands for group, the “w” stands for write, and both are joined by – which literally means group (minus) write. Thus removing the write permission for the group.

chz@cmdlist:~/Downloads$ chmod g-w midori-0.4.3/
chz@cmdlist:~/Downloads$ ls -lah
total 103M
drwx------  4 chz chz 4.0K Jan 22 17:54 .
drwxr-xr-x 31 chz chz 4.0K Jan 23 14:01 ..
drwxr-xr-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3

 

What if we want to modify the permissions for user, group, and others all at the same time? Well there are two different ways we could do this. We could continue using something like ugo+rwx, which would literally convert to user, group, others (plus) read, write, execute. Or we could simply use the “a” flag which represents “all”, meaning user, group, and others. Check out the example below. Do note that it’s not a great idea give write permissions to others on your system, as one could modify your files or directories.

chz@cmdlist:~/Downloads$ chmod a+rwx midori-0.4.3/
chz@cmdlist:~/Downloads$ ls -lah
total 103M
drwx------  4 chz chz 4.0K Jan 22 17:54 .
drwxr-xr-x 31 chz chz 4.0K Jan 23 14:01 ..
drwxrwxrwx 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3

 

Now we’re going to repeat the same examples as above. However, this time we’ll modify our permissions using numerical values. Ready? Let’s get started. As you can see we issued the chmod command followed by 775. This will give read, write, and execute permissions to both the owner and the group associated with the file. However, all others will only have read and execute permissions. How did we get 775? Simple. Since we know read=4, write=2, and execute=1 if we add up the user values we’ll get 7. Then if we add up the group attributes we get 7. Finally, if we add up the others attributes we get 4+1 which gives up 5. Now if we put all those numbers together what do we get? If you answered 775 you’re right!

chz@cmdlist:~/Downloads$ chmod 775 midori-0.4.3/
chz@cmdlist:~/Downloads$ ls -lah
total 103M
drwx------  4 chz chz 4.0K Jan 22 17:54 .
drwxr-xr-x 31 chz chz 4.0K Jan 23 14:01 ..
drwxrwxr-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3

 

In this example we’re going to do the exact opposite as the previous one. This time we want to remove the write permissions for the group “chz”. To do this we’ll simply add up the numerical values. Are you wondering how we get 755? Simple. Since we know read=4, write=2, and execute=1 if we add up the user values we’ll get 7. Then we want read and execute which combined would be 5. Finally, if we add up the others values we get 4+1 which gives up 5. I bet you already know the answer. That’s right it’s 755!

chz@cmdlist:~/Downloads$ chmod 755 midori-0.4.3/
chz@cmdlist:~/Downloads$ ls -lah
total 103M
drwx------  4 chz chz 4.0K Jan 22 17:54 .
drwxr-xr-x 31 chz chz 4.0K Jan 23 17:13 ..
drwxr-xr-x 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3

 

In this example we’re going to give read, write, and execute permissions on midori-0.4.3/ to the owner, group, and others. This is obviously not a good, as we don’t want normal users altering our files. Any idea how we would accomplish this by using numeric values? Yeah, I figured you did. We’ll simply use 777, which gives read, write, and execute to not only our owner, but also our group and all others.

chz@cmdlist:~/Downloads$ chmod 777 midori-0.4.3/
chz@cmdlist:~/Downloads$ ls -lah
total 103M
drwx------  4 chz chz 4.0K Jan 22 17:54 .
drwxr-xr-x 31 chz chz 4.0K Jan 23 17:13 ..
drwxrwxrwx 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3

 

In this example we’ll use the chmod command with the added -R flag. The -R flag means to do recursively. So what does this mean? This means that if we add -R we can change permissions not only on a directory, but also all the files and directories within it. Take a look at the example below.

chz@cmdlist:~/Downloads$ chmod -R 777 midori-0.4.3/
chz@cmdlist:~/Downloads$ ls -lah
total 103M
drwx------  4 chz chz 4.0K Jan 22 17:54 .
drwxr-xr-x 31 chz chz 4.0K Jan 23 17:13 ..
drwxrwxrwx 16 chz chz 4.0K Jan 22 17:55 midori-0.4.3

chz@cmdlist:~/Downloads$ cd midori-0.4.3/
chz@cmdlist:~/Downloads/midori-0.4.3$ ls -lah
total 292K
drwxrwxrwx 16 chz chz 4.0K Jan 22 17:55 .
drwx------  4 chz chz 4.0K Jan 22 17:54 ..
-rwxrwxrwx  1 chz chz 3.5K Dec 17 16:47 AUTHORS
drwxrwxrwx  3 chz chz 4.0K Jan 22 17:59 _build_
-rwxrwxrwx  1 chz chz  29K Dec 17 16:47 ChangeLog
-rwxrwxrwx  1 chz chz 2.8K Dec 17 16:47 configure

 

For a full list of options type:

man chmod

No responses yet

tload – display a graph of the system load average.

Dec 06 2011

It’s nice to be able to tell what kind of load your system has at any given time. While I personally choose to monitor that data with something like top or htop, you can also do so with the simple tload command. The tload command displays your system load in one, five, and fifteen minute intervals. In most cases with a single core system you’ll want to stay around .70 load average. A 1.0 load average simply means you’re at 100% CPU utilization. Let’s check out the example below.

 

In this example we’ll execute the command without any addition flags. As you can seen from the screenshot below tload displays a nice simple ASCII graph. To exit from this display simply hit ctrl+c to end the task.

chz@cmdlist:~$ tload


 

For a full list of options type:

man tload

No responses yet

killall – kill a process by name.

Dec 05 2011

On occasion you might have an zombied application, or an application that seems to have gone locked up. In most cases you would use the kill command to end a process. However sometime you just want to kill all processes by name. This is where killall can be used to quickly kill a specific application by name. Do note that you’ll need to be owner of a process, root, or a sudoer to execute this command. Check out a couple of the examples below for more information.

 
Examples:

In this example we’ll execute the killall command, and kill the icecast2 server I currently have running. First I’ll execute the ps command to show you the running process. Next we’ll execute the killall command followed by the name of our application. In this case it’ll be icecast2.

chz@cmdlist:~$ ps aux
root      1003  0.0  0.0   2376   908 ?        Ss   Dec02   0:00 cron
daemon    1004  0.0  0.0   2248   432 ?        Ss   Dec02   0:00 atd
root      1030  0.0  0.3  73320  3860 ?        Sl   Dec02   0:00 /usr/sbin/libvirtd -d
icecast2  1058  0.0  0.2  10392  2116 ?        Sl   Dec02   1:20 /usr/bin/icecast2 -b -c /etc/icecast2/icecast.xml
ntp       1062  0.0  0.1   4424  1392 ?        Ss   Dec02   0:10 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:118

chz@cmdlist:~$ sudo killall icecast2
[sudo] password for chz: 

chz@cmdlist:~$ ps aux
root      1003  0.0  0.0   2376   908 ?        Ss   Dec02   0:00 cron
daemon    1004  0.0  0.0   2248   432 ?        Ss   Dec02   0:00 atd
root      1030  0.0  0.3  73320  3860 ?        Sl   Dec02   0:00 /usr/sbin/libvirtd -d
ntp       1062  0.0  0.1   4424  1392 ?        Ss   Dec02   0:10 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:118

 

This example is pretty much the same as the last. However, this time we’ll execute the killall command with the additional -v flag. This tells killall to display more information about the command. In this case it confirms that we actually killed the icecast2 process.

chz@cmdlist:~$ ps aux
root      1003  0.0  0.0   2376   908 ?        Ss   Dec02   0:00 cron
daemon    1004  0.0  0.0   2248   432 ?        Ss   Dec02   0:00 atd
root      1030  0.0  0.3  73320  3860 ?        Sl   Dec02   0:00 /usr/sbin/libvirtd -d
icecast2  1058  0.0  0.2  10392  2116 ?        Sl   Dec02   1:20 /usr/bin/icecast2 -b -c /etc/icecast2/icecast.xml
ntp       1062  0.0  0.1   4424  1392 ?        Ss   Dec02   0:10 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:118

chz@cmdlist:~$ sudo killall -v icecast2
Killed icecast2(8033) with signal 15

 

For a full list of options type:

man killall

No responses yet

scp – securely copy files between network hosts.

Nov 21 2011

There are several ways to transfer files between computers. In fact you could use something like rcp or maybe even setup your own ftp server. However one I would suggest using a utility like scp. The scp command uses ssh to transfer files and authenticate users from one host to another. By doing so this makes scp much more secure than using rcp or ftp. Let’s take a look at a couple examples below.

 

Examples:

As you can see in this example we copy the cyber.txt file from the secret directory on box1, and send it to the secret directory on box2. We’ll also be prompted to to enter the password for the user box2 on the host box2. If you enter the password correctly then the file will be copied.

box1@BOX1:~$ scp secret/cyber.txt box2@box2:/home/box2/secret
box2@box2's password:
cyber.txt                                100%   11KB  11.2KB/s   00:00

box2@BOX2:~$ ls secret/
cyber.txt

 

This example is pretty much the same as the first. However, this time we’ll copy the entire secret directory to the home directory on box2. You’ll notice we are using the -r flag, this tells scp to recursively copy everything within the secret directory. You’ll also noticed the ~ this represents the home directory for the box2 user.

box1@BOX1:~$ scp -r secret/ box2@box2:~
box2@box2's password:
defaults.txt                             100% 6726     6.6KB/s   00:00
aahack.txt                               100% 3112     3.0KB/s   00:00
auditool.txt                             100%   12KB  12.4KB/s   00:00
citibank2.txt                            100% 6430     6.3KB/s   00:00
23things.txt                             100% 2342     2.3KB/s   00:00
cyber.txt                                100%   11KB  11.2KB/s   00:00

box2@BOX2:~$ ls
secret

 

Being able to copy files securely using scp is awesome. However sometimes we need to throttle our transfer to prevent saturating our connection. To do this we’ll simply add the -l flag. We’ll have to enter our transfer rate in Kbps. We’re going enter 2500 Kbps which is roughly 312KB/s. We’re also going to add the -r flag to recursively copy the secret directory.

box1@BOX1:~$ scp -rl 2500 secret/ box2@box2:~
box2@box2's password:
defaults.txt                             100% 6726     312.5KB/s   00:00
aahack.txt                               100% 3112     312.5KB/s   00:00
auditool.txt                             100%   12KB  312.5KB/s   00:00
citibank2.txt                            100% 6430     312.5KB/s   00:00
23things.txt                             100% 2342     312.5KB/s   00:00
cyber.txt                                100%   11KB  312.5KB/s   00:00

box2@BOX2:~$ ls
secret

 

In this example we’ll use scp to copy our secret directory by adding the -r flag. We’ll also add the additional -P flag. This flag allows us to specify which port to connect to over ssh. While this might not seem very useful at first, it comes in extremely handy when you need it.

box1@BOX1:~$ scp -rP 2222 secret/ box2@box2:~
box2@box2's password:
defaults.txt                             100% 6726     312.5KB/s   00:00
aahack.txt                               100% 3112     312.5KB/s   00:00
auditool.txt                             100%   12KB  312.5KB/s   00:00
citibank2.txt                            100% 6430     312.5KB/s   00:00
23things.txt                             100% 2342     312.5KB/s   00:00
cyber.txt                                100%   11KB  312.5KB/s   00:00

box2@BOX2:~$ ls
secret

 

For a full list of options type:

man scp

No responses yet

dmesg – display kernel boot up messages.

Nov 13 2011

Have you ever booted linux and not had a piece of hardware detected? Maybe you saw something briefly when the system was booting up, but it streamed by so quickly you weren’t able to read it? Fear not fellow cmdliners just use the dmesg command. The dmesg command retrieves data from the kernel ring buffer. This allows us to see information about which hardware is recognized, and if the kernel can configure it. The dmesg command is often very useful when troubleshooting hardware. Let’s take a look at a few examples below.

 
Examples:

In this example we’ll run the dmesg command without any flags. We will, however pipe the output of dmesg into less, so that we can see the output one screen at a time. To progress to the next screen just hit the spacebar, and when you reach the end simply type q to quit.

chz@cmdlist:~$ dmesg | less
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Linux version 3.0.0-12-generic (buildd@crested) (gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) ) #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 (Ubuntu 3.0.0-12.20-generic 3.0.4)
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-3.0.0-12-generic root=UUID=a91013d6-f74b-42c9-99b8-fbec3162ef8e ro quiet splash vt.handoff=7
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Centaur CentaurHauls

 

In this example we’ll execute the dmesg command without any flags. However, we will direct the output to a file which we’ll call “boot.txt”. This is often great when you need to share the information with someone else.

chz@cmdlist:~$ dmesg > boot.txt

chz@cmdlist:~$ ls -l
total 2708
drwxrwxr-x  6 chz chz    4096 2011-11-13 18:13 books
-rw-rw-r--  1 chz chz   63221 2011-11-13 19:19 boot.txt
drwxr-xr-x  2 chz chz    4096 2011-10-31 23:50 Desktop
drwxr-xr-x  2 chz chz    4096 2011-10-28 23:03 Documents
drwxr-xr-x  7 chz chz    4096 2011-11-13 18:01 Downloads
drwx------ 17 chz chz    4096 2011-11-13 19:17 Dropbox
drwxr-xr-x  3 chz chz    4096 2011-10-29 00:04 Music
drwxr-xr-x  2 chz chz    4096 2011-10-27 22:56 Pictures
drwxr-xr-x  2 chz chz    4096 2011-10-27 22:56 Public
drwxr-xr-x  2 chz chz    4096 2011-11-06 00:35 Videos

 

In this example we’ll execute the dmesg with the -c flag. This flag clears the kernel ring buffer. I personally don’t know why you would use this flag, but here it is. Do note that you will have to be root to run this command.

chz@cmdlist:~$ sudo dmesg -c

 

For a full list of options type:

man dmesg

No responses yet

Older »