Showing posts with label command line. Show all posts
Showing posts with label command line. Show all posts

Wednesday, July 30, 2014

Using sudo -l to check sudo rights

If you need to do a quick check whether on sudo rights for your userid, you can check quickly by
doing a sudo -l

$ sudo -l 

Matching Defaults entries for user1 on this host:
    requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS
    DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS", env_keep+="MAIL PS1
    PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE
    LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY
    LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL
    LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY",
    secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin

User user1 may run the following commands on this host:
    (ALL) ALL

Sunday, June 29, 2014

Quick check for directory size using du

The utilities du has a few parameter which could affect timing of checking directory size. But the difference is not that large 


Using du -csh on the current directory 
time du -csh .
544G

real    0m20.485s
user    0m0.308s
sys     0m2.146s

Using du -cmshis rather fast
$ time du -cmsh
544G

real    0m17.306s
user    0m0.266s
sys     0m1.951s

Using du -sh is the longest
$ time du -sh .
544G    .

real    0m21.790s
user    0m0.312s
sys     0m2.289s


Monday, March 31, 2014

Using passwd command to lock and unlock Linux Account

To lock a linux account, you can use the command

# passwd -l username
Locking password for user username.
passwd: Success

You will notice on the /etc/shadow file, that there will be a ! before the encrypted password string
user1:!...........................

To unlock the linux account, you can use the command
# passwd -u username
Unlocking password for user username.
passwd: Success.

Tuesday, October 1, 2013

Linux Commands Line Resources

Recommended Sites for Linux Command Lines. Good and easy to start
  1. LinuxCommand.Org
  2. LinuxCommand.org: Tips, News And Rants

Thursday, December 20, 2012

Using getent to query /etc/nsswitch.conf

getent program is useful for querying information setup on the /etc/nsswitch.conf. Some of the usage includes

Example 1: To get the user1 entry at /etc/passwd, you will do something like 
# getent passwd user1
user1:x:604:100:User 1:/home/user1:/bin/bash

Example 2: To get the hosts entry at /etc/hosts, you will do something like
# getent hosts node1
192.168.1.5     node1.private.mycluster.com node1

Example 3: To get the groups at /etc/group, you will do something like
# getent group gaussian
gaussian:x:501:user1,user2

For more information, see getent Linux manual pages

Monday, December 10, 2012

Modifying default template for user settings in Linux

If you wish to put or modify a standard template when creating new users, you may wish to put them in the /etc/skel. The /etc/skel acts as a containers where you can out the typical .bashrc .bash_profile .bash_profile or other scripts that you would want all the default users should have. In CentOS, you would typically see

drwxr-xr-x   3 root root  4096 Oct 31 13:12 .
drwxr-xr-x 126 root root 12288 Dec 11 22:48 ..
-rw-r--r--   1 root root    33 Jan 22  2009 .bash_logout
-rw-r--r--   1 root root   290 Oct 31 13:12 .bash_profile
-rw-r--r--   1 root root   176 Jan 22  2009 .bash_profile.old
-rw-r--r--   1 root root   461 Oct 31 13:12 .bashrc
-rw-r--r--   1 root root   124 Jan 22  2009 .bashrc.old
-rw-r--r--   1 root root   515 Jun 15  2008 .emacs
drwxr-xr-x   4 root root  4096 Sep  9  2010 .mozilla
-rw-r--r--   1 root root   658 Sep 22  2009 .zshrc

Do when you do a useradd, you will invoke the following workflow Default values used by useradd command  and inclusion of the template found in /etc/skel

Monday, December 3, 2012

Default values used by useradd command

When the users issues a useradd command, the useradd commands reads the /etc/default/useradd and the /etc/login.defs and determine the default value for useradd. To display the value for /etc/defaults/useradd, see Displaying defaults for useradd

Do read also Modifying default template for user settings in Linux which will add in the files settings for the users.

To read the /etc/login.defs,
# vim /etc/login.defs

# Password aging controls:
#
#       PASS_MAX_DAYS   Maximum number of days a password may be used.
#       PASS_MIN_DAYS   Minimum number of days allowed between password changes.
#       PASS_MIN_LEN    Minimum acceptable password length.
#       PASS_WARN_AGE   Number of days warning given before a password expires.
#
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7

#
# Min/max values for automatic uid selection in useradd
#
UID_MIN                   500
UID_MAX                 60000

#
# Min/max values for automatic gid selection in groupadd
#
GID_MIN                   500
GID_MAX                 60000

#
# If defined, this command is run when removing a user.
# It should remove any at/cron/print jobs etc. owned by
# the user to be removed (passed as the first argument).
#
#USERDEL_CMD    /usr/sbin/userdel_local

#
# If useradd should create home directories for users by default
# On RH systems, we do. This option is overridden with the -m flag on
# useradd command line.
#
CREATE_HOME     yes

# The permission mask is initialized to this value. If not specified,
# the permission mask will be initialized to 022.
UMASK           077

# This enables userdel to remove user groups if no members exist.
#
USERGROUPS_ENAB yes

# Use MD5 or DES to encrypt password? Red Hat use MD5 by default.
MD5_CRYPT_ENAB yes

ENCRYPT_METHOD MD5

Friday, November 30, 2012

Quick Listing of users who have current login session

If you wish to have quicklisting of users who are logging on to your servers, you can use the command "users". If a user is running multiple session, they will appear multiple time.

# users
root root user1 user2 user3 user3
There are more comprehensive tools like who and finger. Will write in future blog entries.

Thursday, November 29, 2012

Using host command as an alternative to nslookup

host is a simple utility for performing DNS lookups. It is normally used to convert names to IP addresses and vice versa. When no arguments or options are given, host prints a short summary of its command line arguments and options.

Common basic Usages

Using host command to check resolving DNS Servers
# host www.google.com.sg

www.google.com.sg has address 173.194.38.159
www.google.com.sg has address 173.194.38.151
www.google.com.sg has address 173.194.38.152
www.google.com.sg has IPv6 address 2404:6800:4003:802::1018

Using host command with "-a" to display a query of type ANY

# host -a google.com.sg

Trying "google.com.sg"
;; ->>HEADER<<- 51686="51686" br="br" id:="id:" noerror="noerror" opcode:="opcode:" query="query" status:="status:">;; flags: qr rd ra; QUERY: 1, ANSWER: 14, AUTHORITY: 4, ADDITIONAL: 3

;; QUESTION SECTION:
;google.com.sg.                 IN      ANY

ANSWER SECTION:
google.com.sg.          177     IN      TXT     "v=spf1 -all"
google.com.sg.          86277   IN      SOA     ns1.google.com. dns-admin.google.com. 2012032600 21600 3600 1209600 300
google.com.sg.          177     IN      AAAA    2404:6800:4003:801::101f
google.com.sg.          177     IN      A       74.125.235.63
google.com.sg.          177     IN      A       74.125.235.55
google.com.sg.          177     IN      A       74.125.235.56
google.com.sg.          10677   IN      MX      10 google.com.s9b1.psmtp.com.
google.com.sg.          10677   IN      MX      10 google.com.s9b2.psmtp.com.
google.com.sg.          10677   IN      MX      10 google.com.s9a1.psmtp.com.
google.com.sg.          10677   IN      MX      10 google.com.s9a2.psmtp.com.
google.com.sg.          345477  IN      NS      ns2.google.com.
google.com.sg.          345477  IN      NS      ns3.google.com.
google.com.sg.          345477  IN      NS      ns1.google.com.
google.com.sg.          345477  IN      NS      ns4.google.com.

;; AUTHORITY SECTION:
google.com.sg.          345477  IN      NS      ns2.google.com.
google.com.sg.          345477  IN      NS      ns3.google.com.
google.com.sg.          345477  IN      NS      ns1.google.com.
google.com.sg.          345477  IN      NS      ns4.google.com.

;; ADDITIONAL SECTION:
ns1.google.com.         188413  IN      A       216.239.32.10
ns2.google.com.         188413  IN      A       216.239.34.10
ns3.google.com.         188413  IN      A       216.239.36.10

Using host -t parameter to select the query type
# host -t MX google.com.sg

google.com.sg mail is handled by 10 google.com.s9b2.psmtp.com.
google.com.sg mail is handled by 10 google.com.s9a1.psmtp.com.
google.com.sg mail is handled by 10 google.com.s9a2.psmtp.com.
google.com.sg mail is handled by 10 google.com.s9b1.psmtp.com.

Friday, November 16, 2012

Graphical Interface to manage runlevels - ntsysv


If you like GUI outlay for chkconfig, you may want to use this utility ntsysv which you can modify very utility. Installation could not be easier.

# yum install ntsysv


================================================================================
 Package            Arch            Version                 Repository     Size
================================================================================
Updating:
 ntsysv             x86_64          1.3.49.3-2.el6          base           29 k
Updating for dependencies:
 chkconfig          x86_64          1.3.49.3-2.el6          base          159 k

Transaction Summary
================================================================================
Upgrade       2 Package(s)

Total download size: 188 k
Is this ok [y/N]:y 

Sunday, September 23, 2012

Using Chage to manage password expiration and aging

As administrators, tools like chage to help manage the /etc/shadow information. /etc/shadow contains information such as

myuseid:$xxxxxxxxxeeerrrrr:15607:0:900:10:0::

Column Description
1 UserID
2 Encrypted Password
3 Set the number of days since January 1st, 1970 when the password was last changed.
4 Minimum number of days between password changes to MIN_DAYS
5 Maximum number of days during which a password is valid
6 Set the number of days of warning before a password change is required
7 Set the number of days of inactivity after a password has expired before the account is locked
8 Set the date or number of days since January 1, 1970 on which the userid account will no longer be accessible. The date may also be expressed in the format YYYY-MM-DD

Best if you can use the command chage. For example, you can use the command

1. Listing of  password details
# chage --list  username

Last password change                                    : Feb 03, 2012
Password expires                                        : Jul 22, 2013
Password inactive                                       : Jul 22, 2013
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 60
Number of days of warning before password expires       : 10

2. Disable password aging for an user account
# chage -m 0 -M 99999 -I -1 -E -1 username

-m 0 (Min number of  days between password change to 0)
-M 99999 (Max Number of days between password change to 99999)
-I -1 (Set "Password Inactive" to never)
-E -1 (Set "Account expires" to never)
Last password change                                    : Feb 03, 2012
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       : 10

For more complete information, see
  1.  6.6. Linux Password & Shadow File Formats
  2. 7 Examples to Manage Linux Password Expiration and Aging Using chage

Wednesday, September 12, 2012

Udev Rules Documention

Taken from the documentation Writing udev rules

Udev is targetted to provide a userspace for a dynamic /dev directory with persistent device naming. If  you need to lock down your device naming, do take a look at this good writeup

Tuesday, September 11, 2012

Unable to open /dev/sdb with fdisk

Fdisk is a menu driven program for creation and manipulation of partition tables. The device is usually something like /dev/sda, /dev/sdb. A device name refers to the entire disks. /dev/sd? is the partition of the device. For example, /dev/sda1 refers to the first partition of the first device.

If you issued a command and you receive a corresponding message "unable to open /dev/sdb"
# fdisk /dev/sdb

Unable to open /dev/sdb

Linux is unable to locate or find the partition. One method to verify that it is so, do a listing of the
devices fdisk can see. In this example below, the partition has been created already.

# fdisk -l

Disk /dev/sdb: 2997.4 GB, 2997426536960 bytes
255 heads, 63 sectors/track, 364416 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      267349  2147480811   83  Linux

WARNING: The size of this disk is 3.0 TB (2997400633344 bytes).
DOS partition table format can not be used on drives for volumes
larger than 2.2 TB (2199023255040 bytes). Use parted(1) and GUID
partition table format (GPT).

Once you have verified the presence of the device, do a fdisk /dev/sdb again

Friday, September 7, 2012

Adding time for dd to test and analyse read and write performance

This is an extension of a previous blog entry Using dd to test and analyse read and write performance. If you add time to dd

# time dd if=/dev/zero of=/home/myaccount/outfile bs=4M count=4096

4096+0 records in
4096+0 records out
17179869184 bytes (17 GB) copied, 136.832 seconds, 126 MB/s
real    2m16.834s
user    0m0.017s
sys     0m12.670s

Monday, September 3, 2012

Scanning for SCSI new devices dynamically on CentOS

sg3_utils is a package of utilities for sending SCSI commands. For this particular blog entry, we will focus on the scanning for new SCSI new scsi dynamically without booting. Do

# yum install sg3_utils

==============================================================================
 Package                          Arch                     Version                       Repository              Size
==============================================================================
Installing:
 sg3_utils                        x86_64                   1.25-5.el5                    base                   501 k
Installing for dependencies:
 sg3_utils-libs                   x86_64                   1.25-5.el5                    base                    51 k

Transaction Summary
==============================================================================
Install      2 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total download size: 553 k
Is this ok [y/N]:

To scan the SCSI-Bus. use the command
# rescan-scsi-bus.sh

Host adapter 0 (aacraid) found.
Host adapter 1 (ata_piix) found.
Host adapter 2 (ata_piix) found.
Host adapter 3 (qla2xxx) found.
Host adapter 4 (qla2xxx) found.
Scanning SCSI subsystem for new devices
Scanning host 0 for  SCSI target IDs  0 1 2 3 4 5 6 7, all LUNs
Scanning for device 0 0 0 0 ...
OLD: Host: scsi0 Channel: 00 Id: 00 Lun: 00
      Vendor: ServeRA  Model: A                Rev: V1.0 
      Type:   Direct-Access                    ANSI SCSI revision: 02
Scanning for device 0 1 0 0 ...
OLD: Host: scsi0 Channel: 01 Id: 00 Lun: 00
      Vendor: IBM-ESXS Model: VPA146C3-ETS10 N Rev: A650
      Type:   Direct-Access                    ANSI SCSI revision: 05
.....
.....
.....
0 new device(s) found.
0 device(s) removed.

Thursday, August 2, 2012

Modifying users group on the fly

This is a simple entry but sometimes tend to forget until I do a man page. Adding a users to a group cannot be easier on Linux

If you are adding a user to a secondary group, use the flag -G
# useradd -d /home/users -g users -G g09 user1


If you are modifying a user from a secondary group, use the flag -G
# usermod -G g09 user1

Thursday, July 26, 2012

Another look at Changing hostname for CentOS

This is an extension of the article Changing the hostname on CentOS. You can replace Step 2 and 3 found in the article with a one-line hostname command

# hostname www.hostserver.com

To test the hostname
# hostname

www.hostserver.com



Wednesday, July 18, 2012

Locating executable or binary for a command

Linux can be so fun and yet easy. This commands most probably you would have known but it is so important if you want to see where the binary is called. The good command is R

1. If you are looking for binary for a particular program. For example R
$ whereis -b R

R: /usr/bin/R /usr/lib/R /usr/local/bin/R /usr/include/R /usr/share/R


2. If you are looking for source for a particular program. For example R
$ whereis -s R


3. If you are looking for manual for a particular program. For example R
$ whereis -m R

R: /usr/share/man/man1/R.1.gz

Sunday, June 24, 2012

Using netstat to diagnose network

Netstat is one good "swiss army knife" to look deeper into the workings linux networking.

I thought I just quickly put some netstat commands which I often used and find it helpgul in resolving networking issue.

1. Checking of networking interfaces
netstat -i

Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500   0 95453656      0      0      0   177764      0      0      0 BMRU
lo        16436   0       70      0      0      0       70      0      0      0 LRU

 

2. Show Kernel Routing Table Information
# netstat -r 

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
  1.1.57.28     *               255.255.255.128 U         0 0          0 eth0
link-local      *               255.255.0.0     U         0 0          0 eth0
default             1.1.1.125   0.0.0.0         UG        0 0          0 eth0

3. Show active listening ports ( -l) with the specific process (-p) associated with listening port
# netstat -ap

....
unix  2      [ ACC ]     STREAM     LISTENING     21138  2474/gnome-session  @/tmp/.ICE-unix/2474
unix  2      [ ACC ]     STREAM     LISTENING     23166  2674/pulseaudio     /tmp/.esd-0/socket
....

3a. To show a specific process ie ssh
# netstat -ap |grep ssh

tcp        0      0 *:ssh                       *:*                         LISTEN      1771/sshd
tcp        0     52 1.1.57.28:ssh            172.21.4.129:50591          ESTABLISHED 7837/sshd
tcp        0      0 *:ssh                       *:*                         LISTEN      1771/sshd
unix       2      [ ACC ]     STREAM     LISTENING     21646  2464/gnome-keyring- /tmp/keyring-i1zxcd/socket.ssh
unix       2      [ ]         DGRAM                    8766783 7837/sshd

4. View operational statistics for network protocol
# netstat -s

Ip:
    12311840 total packets received
    1801583 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    10510256 incoming packets delivered
    174002 requests sent out
Icmp:
    300 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 18
        echo requests: 282
    555 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 125
        echo request: 148
        echo replies: 282
...
A good resource can be found at
  1.  UNIX / Linux: 10 Netstat Command Examples

Wednesday, May 30, 2012

Painting /etc/motd with some colour

Adding colours to foreground and background of the /etc/motd is a useful way to hightlight remarks or message that users can take a note. It is not very difficult. Firstly, do take a look at the colour codes.

^[[0m    - Reset the terminal characteristics
^[[1m    - emphasis (bold)

^[[30m    - Foreground black
^[[31m    - Foreground red
^[[32m    - Foreground green
^[[33m    - Foreground yellow
^[[34m    - Foreground blue
^[[35m    - Foreground magenta
^[[36m    - Foreground cyan
^[[37m    - Foreground white

^[[40m    - Background black
^[[41m    - Background red
^[[42m    - Background green
^[[43m    - Background yellow
^[[44m    - Background blue
^[[45m    - Background magenta
^[[46m    - Background cyan
^[[47m    - Background white

The ^[ can be achieved by pressing CTRL-ESC then ESC. Do note that CTRL-ESC then ESC is not the same as directly typing ^[

The steps are as followed
  1. CTRL-ESC then ESC and type "[31m" (without the quotation)
  2. The Message you wish to convey
  3. Terminating the with CTRL-ESC then ESC and type "[[0m"
For example,

^[[31m #########Hello I'm foreground red############  ^[[0m