Thursday, January 30, 2014

Number of CPU cores

If you need to know how many CPU cores your machine has try this:-

grep -E "core id|physical id" /proc/cpuinfo | tr -d "\n" | sed s/physical/\\nphysical/g | grep -v ^$ | sort | uniq | wc -l


Number of sockets:-
grep "physical id" /proc/cpuinfo | sort | uniq | wc -l

Thursday, January 23, 2014

Force password change

To force a user to change their password at the next log in run the chage command:-

Check current settings

[root@sbt-centos sysconfig]# chage -l fred
Last password change                    : Jan 23, 2014
Password expires                    : never
Password inactive                    : never
Account expires                        : never
Minimum number of days between password change        : 0
Maximum number of days between password change        : 99999
Number of days of warning before password expires    : 7
 

Expire the password

[root@sbt-centos sysconfig]# chage -d 0 fred




Check new settings
 
[root@sbt-centos sysconfig]# chage -l fred
Last password change                    : password must be changed
Password expires                    : password must be changed
Password inactive                    : password must be changed
Account expires                        : never
Minimum number of days between password change        : 0
Maximum number of days between password change        : 99999
Number of days of warning before password expires    : 7

Amanda Restores

Amanda is a backup and recovery solution - http://www.amanda.org/

It is possible to restore files from backups manually or using Amanda utils.

Manually

Find the backup that contains the data you're interested in.  You'll need to skip the Amanda headers, I use DD:-
dd if=amanda_backup_file bs=32k skip=1 > backup_file.tar.gz

Assuming the backup was created as a gzipped tar you can then list the contents and, optionally, restore:-
tar -tvzf backup_file.tar.gz

tar -xvzf backup_file.tar.gz stats.txt



File Transfers

I prefer to use SCP to transfer files to and from Linux hosts.  If you're transferring from a Windows machine then either use the command line utility Putty SCP (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) or something like WinSCP (http://winscp.net/eng/download.php)

To transfer a file called stats.txt from current server to the /tmp directory on a server called fred as the user steve do the following.  If you haven't exchanged public keys then you will be prompted for steve's password:-

scp statz.txt steve@fred:/tmp
steve@fred's password:
stats.txt              100%   38MB   6.4MB/s   00:06

Bash loops

As simple as it comes, loop round 10 times

#!/bin/bash

for i in {1..10}
do
  echo ${i}
done


Similar, but stepping in up in 2s

#!/bin/bash

for i in {1..10..2}
do
  echo ${i}
done


Loop from 1 to 10, but continue if i = 4 (so the continue statement makes execution return to the top of the for loop)

#!/bin/bash

for i in {1..10}
do
  if [ ${i} -eq 4 ]; then
    continue
  fi
  echo ${i}
done





And iterate over files in a directory


#!/bin/bash
 
for file in /tmp/*
do
  ls -l ${file}
done