Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Wednesday, April 22, 2015

How to set a password from command line in linux bash?

If you need to set a password from a command line automatically, you can use the following script:

echo myPassword | passwd myUser --stdin

This will ensure your password is send via pipe into passwd and set appropriately. It can be useful when you need to assign same password on different servers, so you can run this command remotely, over ssh.

Linux command tricks

Some useful tricks and tips regarding linux commands.

1. Use !! for the last entered command
If you want to repeat your last command, you can use !!. That will be replaced by the last entered command. You can also use !! within other words around the command, in that case the previous command will be inserted in place of "!!", and the rest will be kept.
a.gavrin@wp [14:44:27] ~> vi /etc/passwd

a.gavrin@wp [14:44:43] ~> !!


Wednesday, April 2, 2014

How to forward ports in linux

If you need to pass over some port numbers from one server to another, you can use port forwarding in linux. It should be very easy as soon as you understand the concept.

Say, you would like to access port 80 on server1. However, server1 is behind firewall (say, a router), and you can login to that router (server2) to ssh port number 2224. Here is how you can do the port forwarding via ssh.


ssh -p 2224 -L 8080:server1:80 youruser@your.router.com

After you login with this command, keep the window open, and you can open your browser pointing to
http://localhost:8080/

Your ssh will forward requests to 8080 to server1:80. That's all!

Wednesday, February 12, 2014

Print list of zombie's processes in linux

Sometimes you may need to print out list of zombie processes in Linux. Here is the command which can help you:

~> ps -eo pid,ppid,state,user,comm awk 'BEGIN { count=0 } $3 ~ /Z/ { count++; print $1,$2,$3,$4,$5 } END { print "\n" count " Zombie(s) to slay." }'

 

Monday, November 19, 2012

Enable history timestamp for bash history command

Here is a way of how you can see a timestamp for executed command when using "history" command in Linux.

Wednesday, October 3, 2012

Create symlink to the most recent directory

Recently I needed to find a way to create a symlink to the most recent directory so it can be synced up to some other place. Here is the command in linux which can do that:


find /home/ -maxdepth 1 -mindepth 1 -type d -printf '%TY-%Tm-%Td %TT %p\n' | sort -r | cut -d ' ' -f 3 | head -n 1 | awk -F" " '{print "ln -s "$1" latest-dir"}'| bash

The command above creates a symlink to the most recent directory under /home/. As usual, first try it without "| bash" at the end, so you can test what does it print out.

Thursday, September 20, 2012

curl order of show-error and silent options, and stderr redirect

Today I found that the order of curl parameters really makes a difference when you want to print error messages with set "show-error" and "silent" options. 

Example:

user@server:~/> curl --silent --show-error -k "http://wweeee.com"
curl: (6) Couldn't resolve host 'wweeee.com'


user@server:~/> curl --show-error --silent -k "http://wweeee.com"

user@server:~/> 


Another interesting observation - I have a script which does curl requests and captures the output. When I want to get error messages, I have to redirect error to stderr file first like this:

curl --silent --show-error -k "http://blablabla" 2>$OUTFILE.error | sed -e "...." >> $OUTFILE 

If I change the order to have error redirect at the end of command, it does not work (the error output is printed to console, but not to the file I requested):

curl --silent --show-error -k "http://blablabla" | sed -e "...." >> $OUTFILE 
2>$OUTFILE.error

Monday, September 17, 2012

Export or import list of repositories in Linux for yast2

If you need to migrate your linux yast2 repositories from one machine to another, you can do the following:

Export list of yast2 repositories into a text file:

zypper lr –export

By default it saves the list of repositories in xport.repo file

On machine 2 you may import the list created by the command above, into the system by importing the list of repositories:

zypper ar -r xport.repo

"ar" stands for "Add Repository", whereas "lr" stands for "list repositories"

Upgrade linux SUSE to SP2

Steps to upgrade Linux SUSE 11 from SP1 to SP2.

The original steps are listed at novell site:
http://www.novell.com/support/kb/doc.php?id=7010200

Here are the steps I executed in order to upgrade Linux SUSE11 from SP1 to SP2:

1. Installed the following packages:

SUSE_SLES-SP2-migration
sle-sdk-SP2-migration:



2. Started "wagon" (linux update tool)
3. Followed wagon's instructions.
 - registered the system
 - enabled both SP1 and SP2 repositories (this is critical, otherwise there were some dependcies shown which could not be resolved)
 - accepted all defaults and exected the migration process
4. Rebooted the machine
5. Ran wagon again
6. After going through the steps it showed that the system upgrade was successful


Sunday, September 16, 2012

Setup passwordless ssh in linux in 4 easy steps

Say, you need to setup a way to login to another machine without the need to enter a password. You will need to do a few steps to configure this so called "passwordless ssh access"

For simplicity we assume you are on $server1 and want to be able to ssh to $server2. Your username is $sshuser.

1. Generate a pair of keys: 

sshuser@server1:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/sshuser/.ssh/id_rsa):
Created directory '/home/sshuser/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/sshuser/.ssh/id_rsa.
Your public key has been saved in /home/sshuser/.ssh/id_rsa.pub.
The key fingerprint is:
22:80:5e:b6:31:d9:a4:fa:2e:7b:de:fb:f0:a9:81:05 sshuser@server1
The key's randomart image is:
+--[ RSA 2048]----+
|     ..          |
|    . +.         |
|   . o +Eo.      |
|    ....o+o      |
|        So.      |
|        .o       |
|       . .o      |
|      o .. + .   |
|      .B+ +++    |
+-----------------+



2. Create "~/.ssh" directory on server2 for sshuser. It may exist already, but it will not hurt anyway:

sshuser@server1:~> ssh $server2 'mkdir -p $/.ssh; chmod 700 $/.ssh'
Password:

3. Upload public key of the user to server2:

sshuser@server1:~> cat ~/.ssh/id_rsa.pub | ssh $server2 'cat >> .ssh/authorized_keys2; chmod 640 .ssh/authorized_keys2' 


4. That's it! Now you should be able to login to server2 without password:
sshuser@server1:~> ssh $server2

Finally, see all the commands in a single shoot:

ssh-keygen -t rsa
ssh $server2 'mkdir -p ~/.ssh; chmod 700 $/.ssh'
cat ~/.ssh/id_rsa.pub | ssh $server2 'cat >> .ssh/authorized_keys2' 
ssh $server2 'chmod 640 .ssh/authorized_keys2' 
ssh $server2

Thursday, September 13, 2012

Find out your linux version number

If you forgot your linux version number, if should be easy for you to find it out.

SUSE Linux:

~> cat /etc/SuSE-release
SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 1


You may also use another command:
~> cat /etc/issue
Welcome to SUSE Linux Enterprise Server 11 SP1  (x86_64) - Kernel \r (\l).

To print out linux kernel version, you can execute the following command:

~> uname -a
Linux yourserver 2.6.32.54-0.3-default #1 SMP 2012-01-27 17:38:56 +0100 x86_64 x86_64 x86_64 GNU/Linux
 



Wednesday, September 12, 2012

How to get list of top 10 files sorted by size

Sometimes you may need to find list of top 10 or 100 files within a directory or any of it's subdirectories sorted by size. It is quite easy to do that in linux. Do something similar to this:

du -a . | sort -n -r | head -n 10

The command above will return you top 10 files sorted by size in descending order from your current directory. If you want to get list of files from your "/opt" directory, sorted in a similar way, use the following command:
du -a /opt | sort -n -r | head -n 10

Tuesday, September 11, 2012

How to kill multiple processes with name pattern

Say, you need to kill multiple processes in linux which comply with some string pattern. here is the command which can help you:

ps x | grep Admin | awk '{print "kill -9 " $1}' | bash

I usually test it without "bash" first, so you can see what will be killed:

ps x | grep Admin | awk '{print "kill -9 " $1}' 

Note that all processes with "Admin" will be killed after the invocation of this command.

Monday, September 10, 2012

How to delete files not updated within X days

How to get a list of files which were not modified within last X days? Here is the trick. This command will print out information about those files which were not modified within last 30 days in the current directory:

find . -mtime +30 -type f -exec ls -la {} \; 


This command will delete those files which were not modified within last 30 days in the current directory:

find . -mtime +30 -type f -exec rm -fr {} \;