Pages

Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Saturday, 27 September 2014

Linux: Change network interface name


The network interface name, e.g. eth0, is assigned to each hardware in the Linux kernel through the user space configuration mechanism, udev (see Section 3.5.11, “The udev system”), as it is found. The network interface name is referred as physical interface in ifup(8) and interfaces(5).
In order to ensure each network interface to be named persistently for each reboot using MAC address etc., there is a record file "/etc/udev/rules.d/70-persistent-net.rules". This file is automatically generated by the "/lib/udev/write_net_rules" program, probably run by the "persistent-net-generator.rules" rules file. You can modify it to change naming rule.

Caution: When editing the "/etc/udev/rules.d/70-persistent-net.rules" rules file, you must keep each rule on a single line and the MAC address in lowercase. For example, if you find "Firewire device" and "PCI device" in this file, you probably want to name "PCI device" as eth0 and configure it as the primary network interface.

Wednesday, 23 October 2013

Vi Tip: Search and Replace Text

There are times when you would like to change text within a specific range:
First show the line numbers using;

:set nu

Then do the search and replace task like so:

:start_line,end_lines/text_to_be_searched_for_and_replaced/replacing_text/g

Modifying multiple files in one stroke 

The other day, I was working on code and felt that I had to change the date creation function and make use of SQLAlchemy's server_default = text("sysdate") from a rather bizare default = datetime.now()

so I run this;

sed -e 's/default = datetime.now()/server_default = text("sysdate")/g' -i models/*.py

and all python files were updated

How to install RapidSMS in Ubuntu 12.10

RapidSMS is a free and open-source framework for dynamic data collection, logistics coordination and communication, leveraging basic short message service (SMS) mobile phone technology (according to rapidsms.org). RapidSMS is a python program built on top of django. This is a quick "how to" to have rapidsms quickly set up on an Ubuntu machine. It is seven step process.

How to view bios info

There exist a myriad of ways of retrieving hardware details of a host machine in Linux (as is case with Linux).
To extract information such as bios date, bios vendor, your board vendor and the like, you can use the virtual file system sysfs. That information is stored in the /sys directory.The precise directory is: /sys/devices/virtual/dmi/id So typing $head /sys/devices/virtual/dmi/id should display all the necessary details for you. You may also want to use sudo or su for elevated privileges to view some restricted files.An alternative method to retrieving info on your BIOS and even other hardware information from dmi is dmidecode. This usually requires root privileges to execute.
dmidecode is actually a dmi decoder what decodes readings from the dmi table.

Bash: List of commands (Executing Commands Sequentially)

The bash shell has an interesting feature where one is able to execute a list of commands sequentially. A list refers to a sequence of one or more commands that are separated by the following operators; '&', '&&', '||', ';', or the new line character.

Bash Tips: Command History

There are several ways of working with command line history in a bash shell. The history command helps you check what commands have been typed previously. The bash command line history is usually stored in the file .bash_history in a user's home directory (that is; if you use bash as your user shell-which is true for Linux (I run the 3.0.0.12 kernel). You can check which shell is your default from the /etc/passwd file. It is usually the last value after the last colon on your username line e.g:
user_name:x:1000:1000:FirstName LastName,,,:/home/my_user_name:/bin/bash
The command line history is controlled by HISTSIZE and HISTFILE environment variables...

Vi Tip: Prepending Text to multiple lines

There are cases when you want (for many reasons), to add same text to the beginning of multiple lines. This may happen when you are coding (say python) and then you realise that you need to enclose your function within a class. So you add the class definition at the start of the module file. You then need to indent the function, as python requires. To indent you need to add four spaces (or your preferred number). In vi, you do this like so;

Limiting the number of simultaneous user logins

When a user is created, the user generally has an unlimited number of simultaneous logins. This, I think, is generally not a good idea. There should be a limit to the number of virtual terminals that a user can open simultaneously. Those users who just cannot resist opening many terminals should seriously consider installing screen. Read on...

VirtualBox: Managing Virtual Machines In Headless Mode


Virtual Box is a virtualization software that allows many operating systems (guests) to run simultaneously on a host operating system. In the concept of cloud computing, virtualization has become the "in-thing". It is increasingly common for applications to be run on different virtual machines running on one physical machine/server. The guest operating systems (Virtual Machines) can be run in either headed or headless modes. The difference is that in "Headed" mode, the guest displays an interface similar to that displayed when the monitor is connected to the system. More...

Kismet: Ultimate Access Point Discovery Tool




Kismet is an absolutely incredible piece of software for Linux boxes to discover any wireless access points (AP) in your neighborhood. Even if the AP does not broadcast anything and is pretending to hide, kismet will bust it. Mac users have kismac.Kismet is one of the ultra modern wireless sniffers. It can and should be used in together with others such as aircrack-ng, airodump, airdecap, airreplay and others. I view these applications as complementary rather than competing.  Just read on

Remove files or directories in Linux

This is a comprehensive way to remove/ delete files/ directories from konsole, or Terminal (whatever you call your command line). `rm' removes each given FILE. By default, it does not remove directories. rm short form of remove deletes directories and files from your disk.

What is Intelligence - Isaac Asimov

What is intelligence, anyway? When I was in the army, I received the kind of aptitude test that all soldiers took and, against a normal of 100, scored 160. No one at the base had ever seen a figure like that, and for two hours they made a big fuss over me. (It didn't mean anything. The next day I was still a buck private with KP - kitchen police - as my highest duty.) Read More

Working with Microsoft Ofice Excel Files



Want to use HSSF and XSSF read and write spreadsheets in a hurry? This guide is for you. If you're after more in-depth coverage of the HSSF and XSSF user-APIs, please consult the HOWTO guide.  Read More...

Wednesday, 15 May 2013

Vi Tip: Search and Replace

 Search and Replace Text in Specific Range

There are times when you would like to change text within a specific range:
First show the line numbers using;

:set nu

Then do the search and replace task like so:

:start_line,end_lines/text_to_be_searched_for_and_replaced/replacing_text/g

Example:


100 
101     def _set_attr(j_patient):
102         self.id = j_patient.pop("patient.id", None)
103         self.voided = j_patient.pop("patient.voided", False)
104         self.created_by = j_patient.pop("patient.created_by", None)
105         self.created_on = j_patient.pop("patient.created_on", None)
106         self.modified_by = j_patient.pop("patient.modified_by", None)
107         self.modified_on = j_patient.pop("patient.modified_on", None)
108 
109     def _set_patient(self, patient):
110         patient.id = self.id
111         patient.voided = self.voided
112         patient.created_by = self.created_by
113         patient.created_on = self.created_on
114         patient.modified_by = self.modified_by
115         patient.modified_on = self.modified_on
116     


So, to change the word patient to person only under the _set_patient function, do this:

:109,115s/patient/person/g

Friday, 29 March 2013

Modifying multiple files in one stroke



The other day, I was working on code and felt that I had to change the date creation function and make use of SQLAlchemy's server_default = text("sysdate") from a rather bizare default = datetime.now()

so I run this;

sed -e 's/default = datetime.now()/server_default = text("sysdate")/g' -i models/*.py

and all python files were updated

Note: the letter g at the after the replacement text ensures that all instances of the
text to be replaced are actually replaced and not only the first instance if the g (global) were left out

Thursday, 4 October 2012

How to view bios info

There exist a myriad of ways of retrieving hardware details of a host machine in Linux (as is case with Linux).
To extract information such as bios date, bios vendor, your board vendor and the like, you can use the virtual file system sysfs. That information is stored in the /sys directory.
The precise directory is: /sys/devices/virtual/dmi/id
So typing $head /sys/devices/virtual/dmi/id/* should display all the necessary details for you. You may also want to use sudo or su for elevated privileges to view some restricted files.
   1 ~$ ls -l /sys/devices/virtual/dmi/id/
   2 total 0
   3 -r--r--r-- 1 root root 4096 Oct  4 17:24 bios_date
   4 -r--r--r-- 1 root root 4096 Oct  4 08:16 bios_vendor
   5 -r--r--r-- 1 root root 4096 Oct  4 17:24 bios_version
   6 -r--r--r-- 1 root root 4096 Oct  4 19:37 board_asset_tag
   7 -r--r--r-- 1 root root 4096 Oct  4 17:24 board_name
   8 -r-------- 1 root root 4096 Oct  4 19:37 board_serial
   9 -r--r--r-- 1 root root 4096 Oct  4 08:16 board_vendor
  10 -r--r--r-- 1 root root 4096 Oct  4 17:24 board_version
  11 -r--r--r-- 1 root root 4096 Oct  4 19:37 chassis_asset_tag
  12 -r-------- 1 root root 4096 Oct  4 19:37 chassis_serial
  13 -r--r--r-- 1 root root 4096 Oct  4 08:16 chassis_type
  14 -r--r--r-- 1 root root 4096 Oct  4 19:37 chassis_vendor
  15 -r--r--r-- 1 root root 4096 Oct  4 19:37 chassis_version
  16 -r--r--r-- 1 root root 4096 Oct  4 19:37 modalias
  17 drwxr-xr-x 2 root root    0 Oct  4 19:37 power
  18 -r--r--r-- 1 root root 4096 Oct  4 08:16 product_name
  19 -r-------- 1 root root 4096 Oct  4 19:37 product_serial
  20 -r-------- 1 root root 4096 Oct  4 08:16 product_uuid
  21 -r--r--r-- 1 root root 4096 Oct  4 08:16 product_version
  22 lrwxrwxrwx 1 root root    0 Oct  4 11:15 subsystem -> ../../../../class/dmi
  23 -r--r--r-- 1 root root 4096 Oct  4 08:16 sys_vendor
  24 -rw-r--r-- 1 root root 4096 Oct  4 11:15 uevent

dmidecode

An alternative method to retrieving info on your BIOS and even other hardware information from dmi is dmidecode. This usually requires root privileges to execute. dmidecode is actually a dmi decoder what decodes readings from the dmi table. This is an example from my box:
   1 ~$ sudo dmidecode 
   2 # dmidecode 2.11
   3 SMBIOS 2.5 present.
   4 34 structures occupying 1791 bytes.
   5 Table at 0x000FCFE0.
   6 
   7 Handle 0x0000, DMI type 0, 24 bytes
   8 BIOS Information
   9     Vendor: American Megatrends Inc.
  10     Version: 210   
  11     Release Date: 03/02/2010
  12     Address: 0xF0000
  13     Runtime Size: 64 kB
  14     ROM Size: 1024 kB
  15     Characteristics:
  16         ISA is supported
  17         PCI is supported
  18         PNP is supported
  19         BIOS is upgradeable
  20         BIOS shadowing is allowed
  21         ESCD support is available
  22         Boot from CD is supported
  23         Selectable boot is supported
  24         EDD is supported
  25         5.25"/1.2 MB floppy services are supported (int 13h)
  26         3.5"/720 kB floppy services are supported (int 13h)
  27         3.5"/2.88 MB floppy services are supported (int 13h)
  28         Print screen service is supported (int 5h)
  29         8042 keyboard services are supported (int 9h)
  30         Printer services are supported (int 17h)
  31         CGA/mono video services are supported (int 10h)
  32         ACPI is supported
  33         USB legacy is supported
  34         Smart battery is supported
  35         BIOS boot specification is supported
  36         Function key-initiated network boot is supported
  37         Targeted content distribution is supported
  38     BIOS Revision: 2.10
  39     Firmware Revision: 176.1
  40 
  41 Handle 0x0001, DMI type 1, 27 bytes
  42 System Information
  43     Manufacturer: ASUSTeK Computer Inc.       
  44     Product Name: UL30VT             
  45     Version: 1.0      
  46     Serial Number: A3N0AS515480129    
  47     UUID: 8006A65E-0B33-DF81-3D9D-485B3926D33D
  48     Wake-up Type: Power Switch
  49     SKU Number:                    
  50     Family:                    
  51 
  52 Handle 0x0002, DMI type 2, 15 bytes
  53 Base Board Information
  54     Manufacturer: ASUSTeK Computer Inc.       
  55     Product Name: UL30VT   
  56     Version: 1.0      
  57     Serial Number: BSN12345678901234567
  58     Asset Tag: ATN12345678901234567
  59     Features:
  60         Board is a hosting board
  61         Board requires at least one daughter board
  62         Board is replaceable
  63     Location In Chassis: MIDDLE             
  64     Chassis Handle: 0x0003
  65     Type: Motherboard
  66     Contained Object Handles: 0
  67 
  68 Handle 0x0003, DMI type 3, 21 bytes
  69 Chassis Information
  70     Manufacturer: ASUSTeK Computer Inc.       
  71     Type: Notebook
  72     Lock: Not Present
  73     Version: 1.0      
  74     Serial Number: CSN12345678901234567
  75     Asset Tag: ATN12345678901234567
  76     Boot-up State: Safe
  77     Power Supply State: Safe
  78     Thermal State: Other
  79     Security Status: Other
  80     OEM Information: 0x00000000
  81     Height: Unspecified
  82     Number Of Power Cords: 1
  83     Contained Elements: 0
  84     
  85     ..............so on and so forth

Friday, 13 April 2012

Bash: List of commands (Executing Commands Sequentially)


The bash shell has an interesting feature where one is able to execute a list of commands sequentially. A list refers to a sequence of one or more commands that are separated by the following operators; '&', '&&', '||', ';', or the new line character.

Operator Meaning Example
&
Sends processes to the background. This effectively creates a sub-shell for each command that ends with the & operator. One can view the list of background processes by typing the jobs command Command1 & Command2 & Command3 &
&&
This operator represents the AND option. What is essentially means is that a command will execute if and only if the/a previous command returns with a zero exit status/return value. Command1 && Command2 && Command3
||
This operator represents the OR option. A command will only be executed by the bash shell if and only if the previous command returned with a non-zero exit status. Command1 || Command2 || Command3
;
The shell executes commands separately this operator sequentially. This means that the shell waits for the previous command to complete executing and return control to the shell that it executes the next shell. This command is similar to separating commands using the new line. Command1; Command2; Command3
\n
This command represents the new line command. It is similar to entering a first command followed by pressing the “Enter” or “Return” keyboard key. Command1 \n Command2 \n Command3





Sunday, 20 November 2011

Bash Tips: Command History


There are several ways of working with command line history in a bash shell. The history command helps you check what commands have been typed previously. The bash command line history is usually stored in the file .bash_history in a user's home directory (that is; if you use bash as your user shell-which is true for Linux (I run the 3.0.0.12 kernel). You can check which shell is your default from the /etc/passwd file. It is usually the last value after the last colon on your username line e.g: user_name:x:1000:1000:FirstName LastName,,,:/home/my_user_name:/bin/bash The command line history is controlled by HISTSIZE and HISTFILE environment variables. The default HISTSIZE is 1000 and the default HISTFILE is ~/bash_history. This is where a history of your bash activity is stored. The HISTFILESIZE variable is set to 2000 (default). You can modify the defaults by editing the file ~/.bashrc (in your home directory) or /etc/skel/.bashrc for new subsequent users. In the .bashrc file, the line shopt -s histappend explicitly requests bash to append to the HISTFILE deleting this line will likely result in the contents of bash_history being overwritten every other time bash tries to save to history the commands that you have entered. An important thing to note about the bash history is that a command is written to history before bash actually expands the command. For example, the command ls Do* will be saved in ~/.bash_history as typed by the user before the bash shell expands it to list the contents of directories with names starting with the two letters “Do”. Also, if a user logs multiple times into a host, the last shell to be closed on that host will be the one whose commands are written to history. As with Linux/UNIX, there are many ways in which one can navigate the bash command line history. These include; Assuming that after typing the history command, we get this:
1687 dhclient eth0
1688 sudo dhclient eth0
1689 ifconfig
1690 clear
1691 pgrep mysql
1692 kill 3071
1693 pgrep mysql
1694 ssh-keygen -R 192.168.1.1
1695 ssh 192.168.1.1 -l userxyz
1696 sudo ifconfig vboxnet0 192.168.1.20
1697 clear
1698 ssh 192.168.1.1 -l userxyz
1699 ssh-keygen -R 192.168.1.1
1700 ssh 192.168.1.1 -l user
1701 ifconfig
1702 ifconfig vboxnet0 down
1703 sudo ifconfig vboxnet0 down
1704 ssh 192.168.1.1 -l user
1705 pgrep mysql
1706 pgrep mysql | xargs kill
1707 pgrep mysql
1708 clear
1709 htop
1710 clear
1711 info whohas
1712 man dpkg
1713 dpkg half-installed
1714 man dpkg
1715 sudo apt-get purge mysql-query-browser
1716 sudo apt-get purge mysql-admin
1717 kill `pgrep mysql`
1718 sudo apt-get purge mysql-workbench-gpl
1719 sudo dpkg-reconfigure mysql-workbench-gpl
1720 sudo apt-get remove mysql-workbench-gpl
1721 clear
1722 VBoxManage startvm "Test"
1723 sudo bin/vm-manager start Dev
1724 ifconfig
1725 ifconfig -a
1726 VBoxManage controlvm Dev poweroff
1727 ifconfig
1728 clear
1729 ping 192.168.56.101
1730 VBoxManage list vms
1731 VBoxManage list runningvms
1732 clear
1733 env | grep -i hist
1734 export
1735 clear
1736 ls /etc/
1737 cat /etc/environment
1738 cat /etc/bash_completion
  • Arrow keys: pressing the up arrow key navigates through previously entered commands. A similar action is accomplished by pressing Ctrl + P. To navigate forward, pressing the down arrow key moves through the next entered commands. This only works is one was previously navigating through earlier entered commands. Ctrl + N should work the same.
  • !!: immediately executes the previously/last entered command.
    Using our history output above,
    !! would result in line 1738 being executed.
  • !n: executes command number n. Example: Typing !1735 would result in the clear command being executed.
  • !-n: executes command number: current command minus n command. If your last command was history, then !-1 executes that last command history.
    Example: Typing
    !-3 would result in 1736 being executed (listing of /etc directory-if you have read privileges to the /etc directory)
  • history: a list of previously entered commands is displayed. The number of commands displayed depends on the configured HISTSIZE value.
  • !string: execute the most recent command starting with string.
    Example: Typing
    !VBox would execute the line 1731 which displays a list of currently running VirtualBox virtual machines (if you have VirtualBox installed)
  • !?string: execute the most recent command containing string.
    Example: Typing
    !?mysql would execute line 1720 (which removes mysql-workbench package)
  • ^string1^string2: repeat the last command replacing the first occurrence of the string1 with string2. Example: Typing ^mysql^postgres would search the most recent command containing the word mysql and replace it with the word postgres, in our sample history file, this would replace line 1720: sudo apt-get remove mysql-workbench-gpl with this sudo apt-get remove postgres-workbench-gpl (if there is any such package)
  • Ctrl+R, string: This command does a reverse search through history to find the most recent command that contains string. The command does not have to start with string. This brings a prompt (reverse-i-search:) that requests you to enter the characters of the command that you would like to execute. These characters do not have to be the entire command or starting letters of the command. Any words in the command are good enough. The more words of the command you type, the narrower the search becomes making it easier to retrieve the command that you want. Typing Ctrl + R repeats the search further backwards through history until you get to the command you want or reach the top of the bash_history file.

Friday, 26 August 2011

Limiting the number of simultaneous user logins


When a user is created, the user generally has an unlimited number of simultaneous logins. This, I think, is generally not a good idea. There should be a limit to the number of virtual terminals that a user can open simultaneously. Those users who just cannot resist opening many terminals should seriously consider installing screen (sudo apt-get install screen - Debian; yum install screen – RedHat, Fedora and others).
The secret to this is this file: /etc/security/limits.conf. This file, which is owned by root is readable by all users but only writeable by the file owner (root): -rw-r--r--.
This file consists of four columns; domain, type, item and value.
domain: this consists of usernames, group-names (expressed using the @groupname e.g @finance, @marketing etc). The asterisk (*) implies everybody.
type: this value can be one of two; soft or hard limit. A soft limit is one that may be temporarily exceeded by the user. A hard limit cannot be exceeded by a user under any circumstances.
item: This can be one of the following;
- core - limits the core file size (KB)
- data - max data size (KB)
- fsize - maximum filesize (KB)
- memlock - max locked-in-memory address space (KB)
- nofile - max number of open files
- rss - max resident set size (KB)
- stack - max stack size (KB)
- cpu - max CPU time (MIN)
- nproc - max number of processes
- as - address space limit (KB)
- maxlogins - max number of logins for this user
- maxsyslogins - max number of logins on the system
- priority - the priority to run user process with
- locks - max number of file locks the user can hold
- sigpending - max number of pending signals
- msgqueue - max memory used by POSIX message queues (bytes)
- nice - max nice priority allowed to raise to values: [-20, 19]
- rtprio - max realtime priority
- chroot - change root to directory (Debian-specific)

The bold values above are the ones of interest for this particular post. The descriptions next to the values are fairly self explanatory.
value: this is the actual value that is to be compared against. It can be numeric or a system path where appropriate. So, to limit the number of simultaneous logins for a specified user, do this;
username hard maxlogins 3

username - is the username of the user whose number of simultaneous logins you want to restrict.
You can separate the values using a single tab. This restricts user (username) to a maximum of 3 logins.

The nice thing about this is that as soon as you successfully save the file, the security policy is immediately enforced.