środa, 14 stycznia 2015

bash script for changing password

I have seen a lot of articles describing how to change password via a script, some of them work most of them don't.
Below I present option that works very well.

On a local machine can perform command:
echo -e "user:password" | chpasswd

If we are not happy with pasting in password in plain text then we use mkpasswd to encrypt the password and modify slightly command for changing password:
root@debian:/var/www# mkpasswd

Then we use our encrypted password in command:
echo -e "user:TqDuqPhgvp7pM" | chpasswd -e

What about running this command remotely on a server:
ssh root@IP_ADDRESS 'echo -e "user:password" | chpasswd'

encrypted version works almost the same
ssh root@IP_ADDRESS 'echo -e "user:encypted_password" | chpasswd -e'

What if I want to change password on multiple servers at once:
create a file (list.txt) with a list of ip addresses one under another

and run a command in a loop:
for i in $(cat list.txt); do ssh root@$i 'echo -e "user:password" | chpasswd' && echo $i ; done

This post might be little bit outdated because everyone should be using keys instead of passwords.
At my work we use both this is why I wanted to do it almost automatically.

sobota, 10 stycznia 2015

Compile kernel on CentOs 7 (should be very similar on RedHat and Fedora)

For me the reason why I decided to compile kernel was that in work I am testing software that is not included in default release.

Steps required to compile your own kernel:
Download kernel from https://www.kernel.org/.

Extract kernel by command tar -xvzf archive_name

Copy configuration file to location where you have extracted kernel:
cp /boot/config-3.10.0-123.el7.x86_64 your_extracted_archive_location

Prerequisites (install packages required):
yum groupinstall "Development Tools"
yum install bc.x86_64
yum install ncurses-devel

Configure what you want to be compiled to your new kernel:
- make menuconfig (choose option load file that you took earlier from /boot/config*, make modifications, choose save )
- make bzImage -j your_number_here (-j stands for the number of jobs (commands) to run simultaneously, I usually set one job per one core of processor)
- make modules -j your_number_here
- make modules_install -j your_number_here
- make install

The reason why I decided to choose configuration file from /boot/config* is that to do it manually properly would take much time, there is also less chance that I will make an error which will make machine to be unable boot. It is much easier and faster to edit existing proper configuration file.

Make install does all the magic, modifies grub but does not set new kernel as default.

In order to use compiled kernel during next boot modify file /etc/default/grub.
[root@localhost ~]# cat /etc/default/grub
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_CMDLINE_LINUX="vconsole.keymap=pl2 crashkernel=auto vconsole.font=latarcyrheb-sun16 rhgb quiet"

As you can see I've added GRUB_DEFAULT=0 and commented out "#GRUB_DEFAULT=saved".

I have read some articles that compiling kernel does not necessarily make Linux work faster but I have this strange filling that it does (even login via ssh takes less time).

A proof that it went through successfully:
[root@localhost ~]# uname -r