Solved: Create virtual environment with python 3 in linux

Once you have installed python 3.6 as described in the last post you can create virtual environment to work with it.

  • First let’s install the package of virtual environment for python 3.

CentOS/RHEL

sudo yum install -y python3-venv

Ubuntu

sudo apt-get install -y python3-venv 

  • Create a directory for you virtual environment and get inside it.
mkdir virtualenvs
cd virtualenvs
  • Now let’s create our virtual environment.
python3.6 -m venv cloudvedasenv
  • Finally activate the new virtual environment. Notice the new environment name in brackets once you run the source command. That means you are now inside the virtual environment.
[ec2-user@cloudvedas ~]$ source cloudvedasenv/bin/activate

(cloudvedasenv) [ec2-user@cloudvedas ~]$
  • If you want to install a package inside the virtual environment you can use pip .
(cloudvedasenv) [ec2-user@cloudvedas ~]$ pip install zappa requests flask

Solved: Install Python 3 in Linux and set it as default

In this post we will show you how to install python 3 in a Linux box. We have Centos 7 installed in our server.

Check installed python version

Most of the Linux versions by default have python installed. So let’s check the current version.

[ec2-user@cloudvedas ~]$ python --version
 Python 2.7.5
[ec2-user@cloudvedas ~]$

As we can see from the above output, currently we have Python 2 installed and we need Python 3 so let’s install it.

Install pre-requisites

Before we install python 3 let’s first update the yum.

[ec2-user@cloudvedas ~]$ sudo yum -y update

Also, install yum utils.

[ec2-user@cloudvedas ~]$ sudo yum -y install yum-utils

And at the end install the CentOS development tools which will help you build and compile software from source code.

[ec2-user@cloudvedas ~]$ sudo yum -y groupinstall development

Install python 3.6.4

The standard yum repos does not have the latest python release, so we will install IUM (Inline with Upstream Stable) which will have the latest packages.

[ec2-user@cloudvedas ~]$ sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm

Now let’s install Python 3.6.

[ec2-user@cloudvedas ~]$ sudo yum -y install python36u

Next we will install pip, which will manage software packages for Python:

[ec2-user@cloudvedas ~]$ sudo yum -y install python36u-pip

Finally, we will install the development packages.

[ec2-user@cloudvedas ~]$ sudo yum -y install python36u-devel

Let’ check our  python version

[ec2-user@cloudvedas ~]$ python3.6 -V
Python 3.6.4
[ec2-user@cloudvedas ~]$

Update default python

But if you still run the normal command to check python version it will show the Python 2 version if it was installed by default.

[ec2-user@cloudvedas ~]$ python --version
 Python 2.7.5
 [ec2-user@cloudvedas ~]$

To reflect the new python version by default you create an alias in .bashrc file as described below in the last line of the file.

[ec2-user@cloudvedas ~]$ sudo vi ~/.bashrc

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then . /etc/bashrc fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
alias python=python3.6

After making the changes save the file. Now, logout and login again, it should show you the new version.

[ec2-user@cloudvedas ~]$ python -V
Python 3.6.4
[ec2-user@cloudvedas ~]$

Check all installed python versions

Do remember that the default python 2 will still remain in the system as lot of system binaries have dependency on it.

To check all the python versions in your box you can check the installed packages as below.

[ec2-user@cloudvedas ~]$ yum list installed | grep -i python

If you want to create virtual environments in python read our next post.

Solved: How to reset root password in Redhat Linux or CentOS

In this post we will discuss how to reset the root password of a Redhat linux server if you forgot it.

  • Reboot the server
  • While you are at boot menu press “Up or Down” arrow key to prevent system from booting.
  • Select the boot device and press “e” to edit it.(refer Image 1)

Image 1

  • Scroll down the program with arrow key, till you see a line starting with “linux16”. That line will have words “rhgb quiet” .(refer Image 2)
Image 2
  • Now change  “rhgb quiet” to init=/bin/bash  as in image 3.
Image 3
  • After making the change press “ctrl x” to boot the system.
  • Screen will now take you to command prompt without asking for password.

On the command prompt remount root as read-write.

mount -o remount,rw /

Now execute “passwd” command to change the password of root user.  (refer image 4)

Tip:

  1. Pick strong password else it will throw error of bad password and may not change the password.
  2. If you are getting error “passwd command not found” ensure that your machine is booted with the disk and not the ISO. Many people working on VMs make this mistake so you are not alone 🙂
  • If you are using SElinux you will have to run this extra relabel step before reboot or else you won’t be able to login with the new password.
touch  /.autorelabel
  • Finally reboot the server
exec /sbin/init

Now you should be able to login with new password.

Hope this post is helpful to you. Do let me know if you have any query.

Solved: How to use nmcli for adding new IP and managing network in Redhat Linux

In our last post we have seen how to use nmtui for network management. In this post we will see how to use nmcli for doing the same activity.

“nmcli” is a command line interface. Benefit with both nmtui and nmcli is that you don’t have to update the network configuration files manually. All the required changes are done by nmtui and nmcli themselves.

Good thing with nmcli is that you don’t have to install any extra package like nmtui. nmcli is generally included with NetworkManager.

  • Let’s create our new connection.
[root@cloudvedas ~]# nmcli con add type ethernet con-name cldvds-2 ifname enp0s3 ip4 192.168.99.103/24 gw4 192.168.99.1
Connection 'cldvds-2' (33de1c00-4ab4-4777-0a43-c004f0bd47ff) successfully added.
[root@cloudvedas ~]#

In the above example we are using these parameters:-

Connection name:- cldvds-2

Network Interface:- enp0s3

IP/Netmask:- 192.168.99.103/24

Gateway:- 192.168.99.1

  • Check in nmcli if our new connection is present.
[root@cloudvedas ~]# nmcli con show
NAME                 UUID                                 TYPE       DEVICE
System enp0s8    00cb8200-feb0-44b7-a378-3fdc720e0bc7 802-3-ethernet enp0s8
enp0s3           1a03478c-0307-4f23-a7fa-247ad74c37bf 802-3-ethernet --
cldvds-1         303ccf07-e77c-4770-b787-370407f73edc 802-3-ethernet enp0s3
cldvds-2         33de1c00-4ab4-4777-0a43-c004f0bd47ff 802-3-ethernet --
[root@cloudvedas ~]#

We can see the new connection cldvds-2 is created.

  • Activate the new connection
nmcli con up cldvds-2
  • If you are following up from the last post we will first deactivate our old connection cldvds-1 and then activate our new connection cldvds-2 .
[root@cloudvedas network-scripts]# nmcli con down cldvds-1 ; nmcli con up cldvds-2
  • Let’s check if our ip got plumbed

Yes we can see the new IP up and running

  • As I said earlier “nmcli” will automatically update the config file. You can find the file in the directory “/etc/sysconfig/network-scripts” . Cross check the contents of the file
cd /etc/sysconfig/network-scripts
cat ifcfg-cldvds-2

You can also modify the connection properties. Let’s say you want to add DNS server to the connection we created.

[root@cloudvedas ~]# nmcli con mod cldvds-2 ipv4.dns "192.168.99.254"

If you want to assign IP using DHCP you can create a new dhcp connection

[root@cloudvedas ~]# nmcli con add type ethernet con-name dhcp-1 ifname enp0s3

You can activate the new dhcp connection.(Be careful because if you are connected with the same interface which you are changing, you will lose the connection.)

nmcli con down cldvds-2 ; nmcli con up dhcp-1

Bonus:-

If you want to add an IPv6 IP to the interface you can do it with ip6 and gw6 options.

[root@cloudvedas ~]# nmcli con add type ethernet con-name cldvds-ip6 ifname enp0s3 ip4 192.168.99.104/24 gw4 192.168.99.1 ip6 fe80::cafe gw6 2001:db8::1
Connection 'cldvds-ip6' (83fb7f17-fc2f-424c-b446-33c929abcb56) successfully added.

Activate the connection

[root@cloudvedas ~]# nmcli con down cldvds-2 ; nmcli con up cldvds-ip6

If you no longer need a connection you can delete it after deactivating it.

[root@cloudvedas ~]# nmcli con down cldvds-ip6
[root@cloudvedas ~]# nmcli con delete cldvds-ip6

Hope this post is helpful to you. Do let me know if you have any query.

Solved: How to change hostname using nmtui or nmcli in redhat linux

In our last post we have seen how to change hostname manually or with hostnamectl .

In this post we will discuss how to change hostname with nmtui and nmcli tools.

nmtui

Let’s first try with nmtui tool

  • If you don’t have the nmtui tool installed, you can install it using yum
yum install NetworkManager-tui
  • Invoke the nmtui interface from the command line by executing as root user
nmtui
  • In the menu select “Set system hostname” as shown below

  • Write the new hostname which you want to keep.  We want to change it to “prodvedas”.

  • To make it effective restart the “hostnamed” service
systemctl restart systemd-hostnamed
  • Now if you login with new session or do “su -” in same session you should see new hostname.

nmcli

Let’s try changing the hostname with nmcli tool in Redhat Linux.

  • To check the current hostname
nmcli general hostname
  • If you want to change the hostname
nmcli general hostname prodvedas
  • To make it effective restart hostnamed service
systemctl restart systemd-hostnamed
  • Now if you login with new session or do “su -” in same session you should see new hostname.

Hostname changed by any of the above method will be persistent across reboot.

If you are trying to change hostname of AWS EC2 linux instance the process will be slightly different. Refer this post to change AWS EC2 instance hostname.

Refer this post to change IP using nmtui and nmcli.