Saturday, December 19, 2015

How to change default login shell permanently in linux ?

  
In Red hat Linux the default shell you login to is /bin/bash but in case you want to change the default login shell follow the below procedure

To check the currently logged in shell

# echo $SHELL
/bin/bash

To view all the available shells in your machine

# chsh -l
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

You can also view the available shell details from the below file
# less /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

To change the shell temporarily
To do this just provide the full path of the shell you want to use. But this is just a temporary change as next time you switch terminal you will login to the default shell
[root@linuxforfreshers ~]# /bin/sh
sh-4.1#
As you see above my shell prompt changed from /bin/bash to /bin/sh

To change the shell prompt permanently
# chsh -s /bin/sh
Changing shell for root.
Shell changed.
[root@linuxforfreshers ~]#
But as you notice even though our shell was changed successfully but still we see bash shell prompt.

NOTE: To make the changes affect you need to log out and log back in
Using username "root".
root@192.168.8.130's password:
Last login: Fri Mar 21 10:15:03 2014 from 192.168.8.20
-sh-4.1# echo $SHELL
/bin/sh
-sh-4.1#

So now as I try to login I see my default login shell is changed to /bin/sh

Which file is responsible for assigning shell by default?
What if you want next time you create a user, he/she should get different shell and every time you don't change their shell manually

Check the below file
# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes


As you see the SHELL argument has /bin/bash as default. Just change this value to any shell you want to provide for a new user.

Saturday, December 12, 2015

how to assign network setting in rhel 7 using nmtui and nmcli ?

How to assign network in RHEL 7?


networking two ways

lagacy
network manager

networkmanager
--------------

rpm -qa | grep -i Networkmanager

systemctl status NetworkManager

There are  three  different ways assign network settings

GUI ----->Graphical
text ... nmtui   --->Text based
cli .... nmcli    ----> Command line



Using lagacy network service
----------------------

planing to add manually values for eth0

vim /etc/sysconfig/netwok-scripts/ifcfg-eth0

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp

for static

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none   or static
IPADDR=172.25.X.11
PREFIX=24  or  NETMASK=255.255.255.0
GATEWAY=172.25.X.254
DNS1=172.25.254.254

:wq
sytemctl restart NetworkManager

To assign  system name (hostname)
Using command line

hostnamectl set-hostname serverx.example.com

or

vim /etc/hostname
serverx.example.com

before rhel7 .....to set hostname

vim /etc/sysconfig/network
HOSTNAME=serverx.example.com


updating name server info

vim /etc/resolv.conf
nameserver 8.8.8.8

or local dns 172.25.254.254


local name mapping
------------------
if u have to systems .... call with nicknames ...alias name user /etc/hosts
give ip call that system with name  ( nss switch will manage this proccess)
ex:-  172.25.x.11 system1
     172.25.x.10 system2

vim /etc/hosts

172.25.x.11 system1
172.25.x.10 system2
:wq






using nmtui

nmtui stands for Network Manager Text User Interface.
nmtui is actually a very easy text based tool to configure IP address and host names. Just type “nmtui” on command prompt and follow the simple steps

Now choose your connection type after selecting “Edit a connection”. I am choosing ethernet as I don’t have any other connection to my virtual machine.








As I showing here in rhel7 configuring IP addresses. So I had selected “edit” to manually set the ip address





Now after selecting “manual” you can configure ip addresses as per your specifications.

After you press “OK” to come out of this window. Don’t forget to activate the network connection again.



Once you are done with the nmtui tool in rhel7 configuring IP addresses.  You can check using ‘ifconfig or ip a or hostname –i.



Using  nmcli (Network Manager Command Line Interface )

show the connections using

nmcli connection show
nmcli connection show eno1

Configure an IP address
nmcli connection modify eno1 ipv4.addresses 192.168.0.5/24

Configure an IP address with default gateway
nmcli connection modify eno1 ipv4.addresses 192.168.0.5/24 \  ipv4.gateway 192.168.0.1 ipv4.never-default no

Configure an additional IP address
nmcli connection modify eno1 +ipv4.addresses 192.168.0.6

Add a static route
These will be saved in the route-<interface> file in /etc/sysconfig/network-scripts/ :


nmcli connection modify eno1 +ipv4.routes '192.168.111.0/24 192.168.99.1'

Wednesday, December 9, 2015

how to read or view utmp, wtmp and btmp files in Linux ?

 utmp, wtmp and btmp 

In Linux/Unix operating systems everything is logged some where. Most of the system logs are logged in to /var/log folder. This folder contains logs related to different services and applications. In this folder we have some files such as utmp, wtmp and btmp. These files contains all the details about login’s and logout’s which are from local as well as from remote systems and system status such as uptime etc.

Some info about utmp, wtmp and btmp

utmp: will give you complete picture of users logins at which terminals, logouts, system events and current status of the system, system boot time (used by uptime) etc.
wtmp: gives historical data of utmp.
btmp: records only failed login attempts.
Normally when we try to view these files using cat command or vi editor they used to throw some junk characters and garbage values or in encrypted form or hex values. The output of these files when open with vi or cat command are shown below to show how wtmp file look when opened with vi.

This is totally unreadable, then how we can read this file?

We can read this file with only last command. last command is one of the important command which will give you how logged in, when they logged in and when they logged out etc info on the screen.

My last command output.

root@linuxforfreshers:~# last

vasu pts/1 :0 Mon Oct 1 19:11 still logged in
vasu pts/2 :0 Mon Oct 1 18:40 – 19:11 (00:30)
reboot system boot 3.2.0-30-generic Mon Oct 1 18:39 – 19:12 (00:32)
vasu pts/1 :0 Mon Oct 1 18:34 – 18:39 (00:05)
reboot system boot 3.2.0-30-generic Mon Oct 1 18:33 – 18:39 (00:05)
vasu pts/1 :0 Mon Oct 1 18:31 – 18:32 (00:01)
reboot system boot 3.2.0-30-generic Mon Oct 1 18:30 – 18:39 (00:09)
vasu pts/1 :0 Mon Oct 1 13:29 – 18:27 (04:57)
root pts/1 203.217.144.12 Mon Oct 1 13:13 – 13:13 (00:00)
reboot system boot 3.2.0-30-generic Mon Oct 1 12:08 – 18:29 (06:20)
reboot system boot 3.2.0-30-generic Mon Oct 1 12:04 – 12:08 (00:03)
vasu pts/1 :0 Mon Oct 1 10:34 – down (01:12)
reboot system boot 3.2.0-30-generic Mon Oct 1 10:33 – 11:46 (01:12)

This last command display many details about user login/logout activity. The same command can be used to view wtmp, utmp and btmp files.

To open wtmp file and view its content use blow command

last -f /var/log/wtmp

To see still logged in users view utmp file use last command

last -f /var/run/utmp

 To view btmp file use same command
last -f /var/log/btmp

Sample output of last -f wtmp command output.

last -f wtmp.1

root pts/1 ae.ptr10.public. Sun Sep 30 13:01 – 13:11 (00:10)
vasu pts/1 :0 Sun Sep 30 09:23 – 10:55 (01:32)
reboot system boot 3.2.0-30-generic Sun Sep 30 07:36 – 20:12 (1+12:36)
reboot system boot 3.2.0-30-generic Sat Sep 29 21:56 – 01:19 (03:23)
vasu pts/1 :0 Sat Sep 29 09:36 – 14:37 (05:01)

<–output clipped here–>
reboot system boot 3.2.0-30-generic Fri Sep 28 22:51 – 14:37 (15:46)
reboot system boot 3.2.0-30-generic Fri Sep 28 21:39 – 21:45 (00:05)
reboot system boot 3.2.0-29-generic Sat Sep 1 22:53 – 23:07 (00:14)


wtmp.1 begins Sat Sep 1 18:28:10 2012

Tuesday, December 8, 2015

Setup Local Repository In Ubuntu 15.04

Setup Local Repository In Ubuntu 15.04


Why Local repository is important?

As a System administrator, you have to install software, security updates and fixes often in all systems. Obviously, it will consume more Internet bandwidth. So instead of downloading and installing applications every time in all systems from the Ubuntu repositories, it is good idea to save all applications in a local server in your LAN and distribute them to the other Ubuntu systems when required. Having a local repository is really fast and efficient way, because all required applications will be transferred over the fast LAN connection from your local server. So that it will save the Internet bandwidth and ultimately it reduces the annual cost of Internet.

In this tutorial, I will show you how to setup local repository in Ubuntu 15.04 server in two methods.

APT-Mirror ;
APT-Cacher.

Both methods are very easy to set up and configure.
All you need is sufficient hard drive space. At least 50GB or more free space in your local or external hard drive is recommended. Also, you can use an external hard drive to setup a portable repository. So, you can use the portable repository on multiple locations in your LAN.

Method 1: APT-Mirror

In this method, we are going to pull all packages from the public repository (Ubuntu global server) and save them in our local Ubuntu server hard drive.
First install Apache server. Apache web server is important to share the packages over the network.

sudo apt-get install apache2

Now, install APT-Mirror using command:

sudo apt-get install apt-mirror

Now, create a directory to save all packages.
For example, let us create a directory called “/myrepo”. We are going to save all packages in this directory:

sudo mkdir /myrepo

Now, open the file /etc/apt/mirror.list file,

sudo vi /etc/apt/mirror.list

Add the line: set base_path    /myrepo
############# config ##################
#
# set base_path /var/spool/apt-mirror

set base_path /myrepo

#
# set mirror_path $base_path/mirror
# set skel_path $base_path/skel
# set var_path $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads 20
set _tilde 0
#
############# end config ##############

deb http://archive.ubuntu.com/ubuntu trusty main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu trusty main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse

clean http://archive.ubuntu.com/ubuntu
In the above configuration file, you can add the Ubuntu source lists depending upon the distribution you use.
For this tutorial, I use the default source list. Change them as per your requirements.
If you use both 32bit and 64bit architectures, you should name them separately in the above file. For example, if you use 32bit architecture, the lines should start with deb-i386 and for 64bit, the lines should start as deb-amd64. Clear? Well, once you saved the configuration file, populate your repository using the following command:
sudo apt-mirror
Sample output:
Downloading 162 index files using 20 threads...
Begin time: Wed Aug 5 16:09:16 2015
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]...
Now the packages from the Ubuntu public repositories are being pulled and saved to your local directory (In our case it’s /myrepo). Depending upon your Internet speed, it will take hours.
You can cancel this process at any time. When you start it again, it will resume the downloading process where you left it off.  I strongly advise you to use a fast broadband Internet connection.
You don’t have to run this command every day to get new softwares/updates. You can schedule this process using a cron job. So your machine will automatically run the apt-mirror command on a regular daily basis and will keep your repository up-to-date.

To do that, edit file /etc/cron.d/apt-mirror,

sudo vi /etc/cron.d/apt-mirror
Uncomment the line shown in bold:
#
# Regular cron jobs for the apt-mirror package
#
0 4 * * * apt-mirror /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron.log
As per the above example, the cron job will run every day morning 4am and will start to download the packages.

As I mentioned above, all downloaded packages are saved in “/myrepo” directory on our local server.
Let us have a look under the /myrepo directory to make sure the packages are downloaded as shown below:

ls /myrepo/

Sample output:

mirror  skel  var

Now the contents of /myrepo directory should be made available over HTTP (web) to our clients. To do that, simply create a symbolic link to the /myrepo directory:

cd /myrepo/

sudo ln -s /myrepo/mirror/us.archive.ubuntu.com/ubuntu/ ubuntu

Please note: I made this article only for the testing purpose. So I didn’t download the whole public repository. I canceled the download process after a couple of minutes.
Client Configuration

It’s quite easy to configure in client side. Just open your client systems /etc/apt/sources.list file,

sudo vi /etc/apt/sources.list
and add your local repository path:
[...]
deb http://192.168.1.102/ubuntu trusty universe
deb http://192.168.1.102/ubuntu trusty main restricted
deb http://192.168.1.102/ubuntu trusty-updates main restricted
[...]
That’s it. Here 192.168.1.102 is my Ubuntu server IP address.
Now, update the sources list using command:
sudo apt-get update
Finally, install packages of your choice using command:
sudo apt-get install <package-name>
That’s it. The clients need not to be connected to the Internet to download packages. Instead, it will get all packages and updates from your Ubuntu server’s local repository.
Method 2: APT-Cacher
APT-Cacher is different from APT-Mirror. It does not mirror the entire repository contents. Instead, It saves the packages requested by the clients on your local network and make them available to the rest of the clients for future use.
First install Apache server. This is required to share the packages to your Ubuntu clients over the network:
sudo apt-get install apache2
Now install APT-Cacher.
sudo apt-get install apt-cacher
Choose daemon to run as a standalone daemon and click OK.
sk@server: -myrepo_001
Now, Edit file /etc/default/apt-cacher,
sudo vi /etc/default/apt-cacher
and set autostart=1 if it is 0, else leave as it is.
# apt-cacher daemon startup configuration file

# Set to 1 to run apt-cacher as a standalone daemon, set to 0 if you are going
# to run apt-cacher from /etc/inetd or in CGI mode (deprecated).  Alternatively,
# invoking "dpkg-reconfigure apt-cacher" should do the work for you.
#
AUTOSTART=1

# extra settings to override the ones in apt-cacher.conf
# EXTRAOPT=" daemon_port=3142 limit=30 "
You can also allow or deny the no of hosts to access the cache (packages).
To do that, open the /etc/apt-cacher/apt-cacher.conf file.
sudo vi /etc/apt-cacher/apt-cacher.conf
Uncomment and update the value for allowed_hosts to match the individual hosts. Here I allowed systems from 192.168.1.20 to 192.168.1.30.
[...]
## Uncomment and set the IP range ##
allowed_hosts = 192.168.1.20 - 192.168.1.30
#denied_hosts =
[...]
After completing all the steps, restart apache2 service:
sudo systemctl restart apache2
Or,
sudo service apache2 restart
Client Side Configuration
Now, create a file called /etc/apt/apt.conf.d/01proxy:
sudo nano /etc/apt/apt.conf.d/01proxy
Add the following line:
Acquire::http::Proxy "http://192.168.1.102:3142";
Here, 192.168.1.102 is my Ubuntu local repository server’s IP address. Replace the IP address with your server IP address.
Now, update the sources list using command:
sudo apt-get update
Finally, install packages of your choice using command:
sudo apt-get install <package-name>
That’s it.

Conclusion

Due to lack of resources, time and Internet bandwidth, I didn’t completely test both methods. As far as I know, both methods should work fine and they are highly recommended to save your Internet bandwidth. Although, both methods are pretty easy to configure and maintain. You don’t need to be a master In Linux to setup local repository. Give it a try, you won’t be disappointed.

Good luck!