Servers 101: Managing your server - Archiving, Disabling/Enabling network interfaces and more..

in #tutorial7 years ago

On the 3rd chapter of Servers 101, named Management Series, we will start viewing log files to monitor our server, we'll learn some more commands and we will consider adding some more stuff to our server for better monitoring


Other stuff in the Server 101 series:


On this post of the Management series we will learn how to read a command's manual (if it exists), see log files without opening them up in an editor (such as VIM or nano), change file/folder permissions and owner, learn how to make archives (compressed or not), and disable/reenable network interfaces.


Reading the fine manual!

Most commands have a lot of switches that you may want to use. Seeing that my guides are a "quick-start" to get a first taste, I can't list all switches.

You can always search on google to find detailed information. But for that time that you need some quick help, you can use the man command!

man more will show you information about the more command.


Viewing log files

We have a command, that will show us a file in the screen, without being able to edit it. It is supposed to use less memory than using an editor, but that may not always be the case. That command is more!

The easiest way to use it, is more /path/to/file (a quick example would be more /var/log/daemon.log). You can use 2 keys. Spacebar to go to the next "page", and Escape to exit. You can't go back, that's not quite nice, but what more can we expect from more?

There are some switches we can use:

-num int: Set the number of lines to show at once (where int is an integer)

-d: Show instructions (the keys you can use)

-s: Do not show multiple blank lines. If there are 2 or more blank lines in succession, they will show up as one empty line.

Have you ever heard the phrasing "less is more?" Actually, this is applicable here!

less is the other command we can use. It allows us to go back (in contrast to more). It can also open other file types, next to text files, such as PDFs, DOCs (don't expect graphics though). You can also use it to see the files inside an archive, (tar, zip, tgz, tbz2), etc.

Some basic switches:

-f: Force open the file

-E: Quit at the first time we encounter file end

-e: Quit at the second time we encounter file end

Both more and less have a lot of switches. You can use man less and man more to read more!


File Permissions

chmod is used to change the file permissions. We will get a little bit technical here, so get ready. The syntax is this:

chmod 000 /path/to/file. The Triple-digit number in the middle, sets the permissions of the "/path/to/file" file (or folder).

How to choose the correct permissions?

We have 4 different states of permissions:

NumberMeaning
0No permissions
1Execute Only
2Read Only
4Write Only

Then, we have 3 different permission groups, one for each of the digits in the triple digit number:

The first digit (000) is the file owner

The second digit (000) is the group owner

The third digit (000) is the permission for all others

Example: If you want to give write read/write/execute permissions to the file owner, but only execute permissions to everyone else, what will you use?

The second and third groups are clear. You will use 1, and so far, your permissions are ?11. The first group though?

If you want to combine permissions, you'll have to sum them. For example, RWX (read/write/execute) permissions are 4+2+1, so the final is 7.

The correct and final permission would be 711, so chmod 711 /path/to/file does the job.

However! If the target is a folder, and you want to apply the permissions to the files and directories below, you'll have to add the -R switch:

chmod -R 711 /path/to/folder


File Owner

Changing the file owner is needed in some cases. We will not see the "why", but the "how!"

If you do ls -l /var/log/daemon.log you will get this:

-rw-r----- 1 root adm 14926 Jul 20 08:08 /var/log/daemon.log

The first part is the permission value of the file. The third part is the user-owner of the file, and the fourth part is the group-owner of the file (or folder).

That's what we will learn to change now. You can set only a username, or a username and a group seperated by :. For example, to change /path/to/file.txt from user "root" to user "cartman", you will do this:

chown cartman /path/to/file.txt

If you want to change the group to "southpark" as well, you can do:

chown cartman:southpark /path/to/file.txt

As with chmod, if your target is a folder by adding the "-R" switch, you will make the change to all files and folders inside. Otherwise it will apply to the folder only, and all files/folders will belong to the previous user and group.


Archiving

The quickest way to create an archive would be this:

tar -cvf archivename.tar /path/to/directory

This will put the whole "/path/to/directory" in an archive named "archivename.tar".

If you need to archive some specific files, you can do it:

tar -cvf archivename.tar /path/to/file1 /path2/to/file2 /path/to/file3.

Explanations of switches:

c: Create an archive

v: Verbose operation (will show more information)

f: specify archive name

Other switches:

W: Verify the archive

j: Compress with BZip2

z: Compress with GZip (most compressed tar archives are using gzip)

a: Autocompress (by default uses the z switch)


Bringing up and down network interfaces!

ifup brings a network interface up, making it available to transmit and receive data.

ifdown takes a network interface down, placing it in a state where it cannot transmit or receive data.

They both require root privileges!

If your interface is "eth0", using sudo ifdown eth0 would disable it, until you reenable it. To enable it, you would have to do sudo ifup eth0. This is useful in case you do changes in configuration that do not apply on the run (such as, setting a static IP address).

You can use these switches with both ifup & ifdown:

h: Show the help message!

n: Simulate a run. Will print out the result, but will not execute it. (A *what if... scenario)


The third part is over. We have learned more commands today, and it is time to put a stop here. Pat yourself in the back for a third time!

Feel free to write a comment, upvote and/or resteem! This will give me the push I need to keep writing these tutorials!

In the next part, we will learn about a few more tools!


If you need a place to host your servers consider Vultr, Digital Ocean and BuyVM.

These are affiliate links. If you sign up through them, you support me and I will have more free time to write more content like this.

Also If you signup for Digital Ocean through my affiliate link, you will get $10 to try them out. Note: to battle abusers of this offer, you'll have to make a $5 deposit via Paypal or add your credit/debit card, so they can confirm that you are a new user. I did a deposit via Paypal to test them out, and then I added my credit card so I won't have to deposit money manually every now and then.


Also, I am running a witness server.

Please consider voting me, dimitrisp, for a witness if you find what I post helpful.

You can read my witness declaration here

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63315.26
ETH 2668.31
USDT 1.00
SBD 2.79