How to enable or disable SELinux and check status on centOS

The SELinux stands for Security-Enhanced Linux where it is a linux kernel security module. It is enabled by default on most of the linux distribution that we use for servers like centOS. It provides  enhanced security measurements. It gives you fine control over all programs and daemons  on their activities like communicating with out side programs  or controlling whether to establish a outside connections for a particular program.

It is always recommended to have SELinux enabled on a server to avoid common security glitches.

To query the current status of SELinux  use the following commands

The above command will report the current status  of SELinux. Whether SELinux is enforcing, permissive, or disabled. If it is already disabled.

Disabling SELinux

Open the file  /etc/selinux/config  and change the option SELINUX to disabled

if you open file you would see something like

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing – SELinux security policy is enforced.
# permissive – SELinux prints warnings instead of enforcing.
# disabled – No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
# targeted – Targeted processes are protected,
# mls – Multi Level Security protection.
SELINUXTYPE=targeted

If SELinux is enabled you would see enforcing replace it with disabled.

You should restart the machine to take effect If you change SELINUX status from Enabled to Disabled or vice versa.

Enabling SELInux

To enable SELinux follow the below instructions,

  1. Open the file /etc/selinux/config  
  2. Change option SELINUX from disabled to enforcing
  3. Restart the machine

Change mode

To change the mode of SELinux which is running

Check Status

SELinux is the linux  kernel module for enhanced security. SELinux stands for Security-Enhanced Linux. If SELinux is installed on your machine or server you can check the current status  by using following commands

The above command will give you one of the following as an output

You use the below command which will give simple overview

If enabled you will output something like

 

You can also check the configuration which is located at /etc/selinux/config.

In above config file  the option SELINUX  describes the status of SELinux. But it’s not precise to determine the status from the configuration file, it’s better to determine the status by using commands mentioned above.

 

 

Note: You need administrator privileges to either enable or disable SELinux

How to change hostname of the linux machine

Host name is the name or label which is assigned to your computer that is used to identify and communicate. Usually you can choose your hostname while installation of operating system. If you take a server you may get machine with pre labeled hostname. It is always possible to change the hostname.

Note: You need root or sudo access to change the hostname of the machine.

There are different ways to changes the hostname of the computer. you  canchoose one of them. There is a volatile way and not volatile way. If you want change host name of the computer permanently you need to write your hostname in couple of files. If you want to change instantly (temporarily) you can change using couple of commands, but this change is volatile you will loose your hostname after reboot.

Using command hostname

Now, we will see  the way to change hostname instantly. This is not a persistent change. You will loose after reboot

The command hostname without arguments will display the current hostname of the system

Where you can give qualified hostname in the place of lintel.in. If you are using desktop linux(ubuntu) you can observer your change on (new session)shell, ie, user@hostname

Using sysctl

sysctl is the command that we use to configure kernel parameter at runtime. We can change hostname using this command as well. You can query the hostname using sysctl  as

To change hostname,

Changes made using either way are volatile. To make permanent change. Follow the below steps.

Edit hostname file

You have to edit the file /etc/hostname to make persistent change. This file contains only the hostname. To update hostname,

Including the above change you may need to edit /etc/hosts file. Change the 127.0.0.1   old_hostname  section. Replace old hostname with new hostname if you find.

it could be something like

Restart the machine to get your changes applied. You can combine both ways to get new hostname now and then after reboot.

Using hostnamectl

The linux distributions are also providing the command called hostnamectl to manage and set the hostname.

hostamectl with out any arguments will give the system hostname and other misc details

Check hostname

Set the hostname using hostnamectl

Chaging hostname using nmcli

Oh yeah, you can change the hostname using command nmcli as well. This command can be used to set the static hostname.

Query the hostname

Change the hostname

 

How to listen on an arbitrary port and print the data which is coming using netcat

We are well known about  telnet which is pretty much handy tool to debug and open a TCP connection to remote server:port. But we want the tool which will listen on given port for connections unlike telnet. Yeah, there is tool called netcat. Usually it comes with many linux distributions by default, also called nc. There is also a windows version . According to the scope of the article we will see how to open a TCP or UDP connection and print the data sent by connected clients, but this tool  is more than capable of just listening on port for a connection.

Synopsis

nc [-46DdhklnrStUuvzC] [-i interval] [-p source_port] [-s source_ip_address] [-T ToS] [-wtimeout] [-X proxy_protocol] [ -x proxy_address[ :port]] [hostname] [port[s]]

 

Listen for UDP on specific host and port using netcat

Listen for the connections and print the data sent by the clients

nc -lvu [IP Address] 5060

nc is the command alias for netcat.

The break down of above command

l        Forcing netcat to listen on given host and port instead of opening/making  connection with remote host and port

v          Give verbose output

u          Use UDP. That is, listen for UDP on give port, by default it listens for TCP unless we give this option.

Ip Address   It is optional, if it is not specified localhost will be used

Port                You must specify the port on which nc should listen on.

Listen for TCP on specific host and port using netcat

This is same as UDP mentioned above except we haven’t specified the option -u . Where netcat will listen on TCP port by default with out that option.

Now netcat is listening on the given port on localhost. You can connect to this TCP port from the other  tab(terminal)   using either netcat or telnet

Connect Using Telnet

After  connected, type “Hello”, you should see that text in the tab where netcat is listening

Connect Using NetCat

After   connected,  we sent some text. It should be displayed on tab where nc is listening.

You can quit the client by sending interrupt  CTRL + D. If you quit the client, the server(nc command) will also stop listening. We can use the option -k     to forces nc to stay listening for another connection after its current connection is completed.  It is an error to use this option without the -l option.

I hope, these examples gave you some good understanding. As you can see, we can use nc as both server and client. Practically it is more capable than you think. If you are networking geek or dealing with network application or protocols. You can find this handy for your debugging purpose.

Case Studies

Let’s say, I have web hook URL configured on other 3rd party website. I want to check whether that server is sending events or not. If sending, I would like to see how is API and message format and I don’t have my program ready yet. In such a case, you simply listen on host and port which are configured on 3rd party web site to receive events and simply you can check the data received by nc 

Something like,

The tool netcat (nc) is very powerful tool, it is very simple tool but there are lot of possibilities. Anti-viruses on windows will detect this program as malware sometimes. Where it can also be used as a backdoor.

How to add delete routes in linux

There are two commands which are useful either to add or delete route, those are  route and ip. We will see how to change route using command route.

Route Synopsis

Adding route

Deleting route

A quick way to add default route

A  quick way to delete defualt route