Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
TetraWiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Linux 101 hacks
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
='''Chapter 9: System Administration Tasks'''= =='''Hack 65. Partition using fdisk'''== After you’ve installed brand new disks on your server, you have to use tools like fdisk to partition it accordingly. Following are the 5 typical actions (commands) that you can execute inside fdisk. o n – New Partition creation o d – Delete an existing partition o p - Print Partition Table o w – Write the changes to the partition table. i.e save. o q – Quit the fdisk utility '''Create a partition''' In the following example, I created a /dev/sda1 primary partition. <nowiki># </nowiki>'''fdisk /dev/sda''' Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. The number of cylinders for this disk is set to 34893. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: # software that runs at boot time (e.g., old versions of LILO) # booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) Command (m for help): '''p''' Disk /dev/sda: 287.0 GB, 287005343744 bytes 255 heads, 63 sectors/track, 34893 cylinders Units <nowiki>= </nowiki>cylinders of 16065 <nowiki>* </nowiki>512 <nowiki>= </nowiki>8225280 bytes Device Boot Start EndBlocks Id System Command (m for help): '''n''' Command action e extended p primary partition (1-4) '''p''' Partition number (1-4): '''1''' First cylinder (1-34893, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-34893, default 34893): Using default value 34893 Command (m for help): '''w''' The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. '''Verify that the partition got created successfully''' <nowiki># </nowiki>'''fdisk /dev/sda''' The number of cylinders for this disk is set to 34893. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: # software that runs at boot time (e.g., old versions of LILO) # booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) '''Command (m for help): p''' Disk /dev/sda: 287.0 GB, 287005343744 bytes 255 heads, 63 sectors/track, 34893 cylinders Units <nowiki>= </nowiki>cylinders of 16065 <nowiki>* </nowiki>512 <nowiki>= </nowiki>8225280 bytes {| style="border-spacing:0;" | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Device Boot | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Start</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">End</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">Blocks</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Id | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| System |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| /dev/sda1 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">34893</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">280277991</div> | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 83 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux |- | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Command (m for help): '''q''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |} =='''Hack 66. Format a partition using mke2fsk'''== After partitioning the disks, it is still not ready for usage, as we need to format the disk. At this stage, if you try to view the disk information, it will give the following error message indicating that no valid superblock is present. <nowiki># </nowiki>'''tune2fs -l /dev/sda1''' tune2fs 1.35 (28-Feb-2004) tune2fs: Bad magic number in super-block while trying to open /dev/sda1 Couldn't find valid filesystem superblock. To format the disk, use mke2fs as shown below. <nowiki># </nowiki>'''mke2fs /dev/sda1''' You can also pass the following optional parameter to the mke2fs. # -m 0 : reserved-blocks-percentage – This indicates the percentage of the filesystem blocks reserved for the root user. Default is 5%. In the following example, it is set to 0. # ## -b 4096 : block-size specified in bytes. Valid values are 1024, 2048 and 4096 bytes per block. # '''mke2fs -m 0 -b 4096 /dev/sda1 ''' mke2fs 1.35 (28-Feb-2004) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 205344 inodes, 70069497 blocks 0 blocks (0.00%) reserved for the super user First data block=0 Maximum filesystem blocks=71303168 2139 block groups 32768 blocks per group, 32768 fragments per group 96 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872 Writing inode tables: done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 32 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. The above command will create an ext2 filesystem. To create an ext3 file system do the following: # '''mkfs.ext3 /dev/sda1 ''' # '''mke2fs –j /dev/sda1 ''' =='''Hack 67. Mount the partition'''== After creating a partition and formatting, you can mount it to a mount point. First create a directory where the partition should be mounted. # mkdir /home/database Mount the file system. # '''mount /dev/sda1 /home/database ''' To automatically mount the filesystem after the reboot, add the following entry to the /etc/fstab /dev/sdaa /home/database ext3 defaults 0 2 =='''Hack 68. Fine tune the partition using tune2fs'''== Use the tune2fs –l /dev/sda1 to view the filesystem information as shown below. <nowiki># </nowiki>'''tune2fs -l /dev/sda1''' tune2fs 1.35 (28-Feb-2004) Filesystem volume name: /home/database Last mounted on:<nowiki><not </nowiki>available> Filesystem UUID:f1234556-e123-1234-abcd- bbbbaaaaae11 Filesystem magic number: 0xEF44 Filesystem revision <nowiki>#:</nowiki>1 (dynamic) Filesystem features:resize_inode filetype sparse_super Default mount options:(none) Filesystem state:not clean84 {| style="border-spacing:0;" | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| '''Linux 101 Hacks''' | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">www.thegeekstuff.com</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Errors behavior: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Continue | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Filesystem OS type: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Linux | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode count: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1094912 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Block count: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 140138994 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved block count: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free blocks: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16848481 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Free inodes: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 1014969 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| First block: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Block size: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2048 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Fragment size: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2048 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved GDT blocks: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 512 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Blocks per group: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16384 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Fragments per group: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 16384 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inodes per group: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 128 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode blocks per group: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 8 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Filesystem created: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Tue Jul | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1 00:06:03 2008</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Last mount time: | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Thu Aug 21 05:58:25 2008 |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Last write time: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Fri Jan | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">2 15:40:36 2009</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Mount count: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 2 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Maximum mount count: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 20 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Last checked: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Tue Jul | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| <div align="right">1 00:06:03 2008</div> |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Check interval: | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 15552000 (6 months) |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Next check after: | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Sat Dec 27 23:06:03 2008 |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved blocks uid: | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 (user root) |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Reserved blocks gid: | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 0 (group root) |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| First inode: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 11 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Inode size: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 128 | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Default directory hash: | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| tea | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| Directory Hash Seed: | colspan="2" style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| 12345829-1236-4123-9aaa- |- | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| ccccc123292b | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| | style="border:none;padding-top:0in;padding-bottom:0in;padding-left:0.075in;padding-right:0.075in;"| |} You can also use the tune2fs to tune the ex2/ext3 filesystem parameter. For example, if you want to change the Filesystem volume name, you can do it as shown below. # '''tune2fs -l /dev/sda1 | grep volume ''' Filesystem volume name: /home/database # '''tune2fs -L database-home /dev/emcpowera1 '''tune2fs 1.35 (28-Feb-2004) <nowiki># </nowiki>'''tune2fs -l /dev/sda1 | grep volume''' Filesystem volume name: database-home =='''Hack 69. Create a swap file system.'''== Create a file for swap usage as shown below. # '''dd if=/dev/zero of=/home/swap-fs bs=1M count=512 ''' 512+0 records in 512+0 records out # ls -l /home/swap-fs -rw-r--r-- 1 root root 536870912 Jan 2 23:13 /home/swap-fs Use mkswap to setup a Linux swap area in the /home/swap-fs file that was created above. <nowiki># </nowiki>'''mkswap /home/swap-fs''' Setting up swapspace version 1, size <nowiki>= </nowiki>536866 kB Once the file is created and has been setup for Linux swap area, it is time to enable the swap using swapon as shown below. <nowiki># </nowiki>'''swapon /home/swap-fs''' Add the following line to /etc/fstab and reboot the system for the swap to take into effect. /home/swap-fs swap swap defaults 0 0 =='''Hack 70. Create a new user'''== '''Add a new user – Basic method''' Specify only the user name. <nowiki># </nowiki>useradd jsmith '''Add a new user with additional Parameter''' You can also specify the following parameter to the useradd o -c : Description about the user. # ## -e : expiry date of the user in mm/dd/yy format # '''adduser -c "John Smith - Oracle Developer" -e 12/31/09 jsmith ''' Verify that the user got added successfully. <nowiki># grep jsmith /etc/passwd jsmith:x:510:510:John Smith - Oracle Developer:/home/jsmith:/bin/bash</nowiki> '''Change the user password.''' <nowiki># </nowiki>'''passwd jsmith''' Changing password for user jsmith. New UNIX password: BAD PASSWORD: it is based on a dictionary word Retype new UNIX password: passwd: all authentication tokens updated successfully. '''Note: '''Make sure to follow''' '''[http://www.thegeekstuff.com/2008/06/the-ultimate-guide-for-creating-strong-passwords/ ][http://www.thegeekstuff.com/2008/06/the-ultimate-guide-for-creating-strong-passwords/ these best practices][http://www.thegeekstuff.com/2008/06/the-ultimate-guide-for-creating-strong-passwords/ ]to''' '''create a strong password''' '''for the user. '''How to identify the default values used by useradd?''' Following are the default values that will be used when an user is created. <nowiki># </nowiki>'''useradd –D''' GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/bash SKEL=/etc/skel =='''Hack 71. Create a new group and assign to an user'''== '''Create a new developer group.''' <nowiki># </nowiki>groupadd developers '''Validate that the group was created successfully.''' <nowiki># grep developer /etc/group developers:x:511:</nowiki> '''Add an user to an existing group.''' You cannot use useradd to modify an existing user, as you’ll get the following error message. # useradd -G developers jsmith useradd: user jsmith exists # usermod -g developers jsmith '''Validate the users group was modified successfully.''' <nowiki># grep jsmith /etc/passwd jsmith:x:510:511:Oracle Developer:/home/jsmith:/bin/bash</nowiki> <nowiki># </nowiki>'''id jsmith''' uid=510(jsmith) gid=511(developers) groups=511(developers) <nowiki># grep jsmith /etc/group jsmith:x:510: developers:x:511:jsmith</nowiki> =='''Hack 72. Setup SSH passwordless login in OpenSSH'''== You can login to a remote Linux server without entering password in 3 simple steps using ssky-keygen and ssh-copy-id as explained in this example. ssh-keygen creates the public and private keys. ssh-copy-id copies the local-host’s public key to the remote-host’s authorized_keys file. ssh-copy-id also assigns proper permission to the remote-host’s home, ~/.ssh, and ~/.ssh/authorized_keys. '''Step 1: Create public and private keys using ssh-key-gen on local-host''' jsmith@local-host$ '''ssh-keygen''' Generating public/private rsa key pair. <nowiki>Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]</nowiki> <nowiki>Enter passphrase (empty for no passphrase): [Press enter key]</nowiki> <nowiki>Enter same passphrase again: [Pess enter key] Your identification has been saved in /home/jsmith/.ssh/id_rsa.</nowiki> Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub. The key fingerprint is: 33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host '''Step 2: Copy the public key to remote-host using ssh-copy-id''' jsmith@local-host$ '''ssh-copy-id -i ~/.ssh/id_rsa.pub''' '''remote-host''' jsmith@remote-host’s password: Now try logging into the machine, with “ssh ‘remote-host’”, and check in: .ssh/authorized_keys to make sure we haven’t added extra keys that you weren’t expecting. '''Note: '''ssh-copy-id appends the keys to the remote-host’s .ssh/authorized_key. '''Step 3: Login to remote-host without entering the password''' jsmith@local-host$ '''ssh remote-host''' Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2 <nowiki>[</nowiki>'''Note''': SSH did not ask for password.] jsmith@remote-host$ <nowiki>[</nowiki>'''Note''': You are on remote-host here] =='''Hack 73. Use ssh-copy-id along with ssh-agent'''== '''Using ssh-copy-id along with the ssh-add/ssh-agent''' When no value is passed for the option -i and If ~/.ssh/identity.pub is not available, ssh-copy-id will display the following error message. jsmith@local-host$ ssh-copy-id -i remote-host /usr/bin/ssh-copy-id: ERROR: No identities found If you have loaded keys to the ssh-agent using the ssh-add, then ssh-copy-id will get the keys from the ssh-agent to copy to the remote-host. i.e, it copies the keys provided by ssh-add -L command to the remote-host, when you don’t pass option -i to the ssh-copy-id. jsmith@local-host$ ssh-agent $SHELL jsmith@local-host$ ssh-add -L The agent has no identities. jsmith@local-host$ ssh-add Identity added: /home/jsmith/.ssh/id_rsa (/home/jsmith/.ssh/id_rsa) jsmith@local-host$ ssh-add -L ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79D aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow== /home/jsmith/.ssh/id_rsa jsmith@local-host$ ssh-copy-id -i remote-host jsmith@remote-host’s password: Now try logging into the machine, with “ssh ‘remote-host’”, and check in: .ssh/authorized_keys to make sure we haven’t added extra keys that you weren’t expecting. <nowiki>[</nowiki>'''Note''': This has added the key displayed by ssh-add -L] '''Three Minor Annoyances of ssh-copy-id''' Following are few minor annoyances of the ssh-copy-id. # Default public key: ssh-copy-id uses ~/.ssh/identity.pub as the default public key file (i.e when no value is passed to option -i). Instead, I wish it uses id_dsa.pub, or id_rsa.pub, or identity.pub as default keys. i.e If any one of them exist, it should copy that to the remote-host. If two or three of them exist, it should copy identity.pub as default. # The agent has no identities: When the ssh-agent is running and the ssh-add -L returns “The agent has no identities” (i.e no keys are added to the ssh-agent), the ssh-copy-id will still copy the message “The agent has no identities” to the remote-host’s authorized_keys entry. # Duplicate entry in authorized_keys: I wish ssh-copy-id validates duplicate entry on the remote-host’s authorized_keys. If you execute ssh-copy-id multiple times on the local-host, it will keep appending the same key on the remote-host’s authorized_keys file without checking for duplicates. Even with duplicate entries everything works as expected. But, I would like to have my authorized_keys file clutter free. =='''Hack 74. Crontab'''== Using cron you can execute a shell-script or Linux commands at a specific time and date. For example a sysadmin can schedule a backup job that can run every day. '''How to add a job to the cron?''' <nowiki># </nowiki>crontab –e 0 5 <nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/backup.sh This will execute /root/bin/backup.sh at 5 a.m every day. '''Description of Cron fields.''' Following is the format of the crontab file. {minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script} o minute: Allowed range 0 – 59 o hour: Allowed range 0 – 23 o day-of-month: Allowed range 0 – 31 o month: Allowed range 1 – 12. 1 = January. 12 = December. o Day-of-week: Allowed range 0 – 7. Sunday is either 0 or 7. '''Crontab examples''' # Run at 12:01 a.m. 1 minute after midnight everyday. This is a good time to run backup when the system is not under load. # ## 0 <nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/backup.sh # Run backup every weekday (Mon – Fri) at 11:59 p.m. # ## 11 <nowiki>* </nowiki><nowiki>* </nowiki>1,2,3,4,5 /root/bin/backup.sh Following will also do the same. 59 11 <nowiki>* </nowiki><nowiki>* </nowiki>1-5 /root/bin/backup.sh # Execute the command every 5 minutes. <nowiki>*/5 </nowiki><nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/check-status.sh # Execute at 1:10 p.m on 1st of every month # ## 13 1 <nowiki>* </nowiki><nowiki>* </nowiki>/root/bin/full-backup.sh # Execute 11 p.m on weekdays. # ## 23 <nowiki>* </nowiki><nowiki>* </nowiki>1-5 /root/bin/incremental-backup.sh '''Crontab Options ''' Following are the available options with crontab: # crontab –e : Edit the crontab file. This will create a crontab, if it doesn’t exist o crontab –l : Display the crontab file. o crontab -r : Remove the crontab file. o crontab -ir : This will prompt user before deleting a crontab. =='''Hack 75. Safe Reboot Of Linux Using Magic SysRq Key'''== The magic SysRq key is a key combination in the Linux kernel which allows the user to perform various low level commands regardless of the system’s state. It is often used to recover from freezes, or to reboot a computer without corrupting the filesystem. The key combination consists of Alt+SysRq+commandkey. In many systems the SysRq key is the printscreen key. First, you need to enable the SysRq key, as shown below. echo "1" > /proc/sys/kernel/sysrq '''List of SysRq Command Keys''' Following are the command keys available for Alt+SysRq+commandkey. o ‘k’ – Kills all the process running on the current virtual console. o ’s’ – This will attempt to sync all the mounted file system. # ‘b’ - Immediately reboot the system, without unmounting partitions or syncing. o ‘e’ – Sends SIGTERM to all process except init. o ‘m’ – Output current memory information to the console. o ‘i’ - Send the SIGKILL signal to all processes except init # ‘r’ - Switch the keyboard from raw mode (the mode used by programs such as X11), to XLATE mode. o ’s’ – sync all mounted file system. # ‘t’ - Output a list of current tasks and their information to the console. o ‘u’ - Remount all mounted filesystems in readonly mode. o ‘o’ – Shutdown the system immediately. o ‘p’ – Print the current registers and flags to the console. # ‘0-9′ - Sets the console log level, controlling which kernel messages will be printed to your console. o ‘f’ - Will call oom_kill to kill process which takes more memory. # ‘h’ – Used to display the help. But any other keys than the above listed will print help. We can also do this by echoing the keys to the /proc/sysrq-trigger file. For example, to re-boot a system you can perform the following. echo "b" > /proc/sysrq-trigger '''Perform a Safe reboot of Linux using Magic SysRq Key''' To perform a safe reboot of a Linux computer which hangs up, do the following. This will avoid the fsck during the next re-booting. i.e Press Alt+SysRq+letter highlighted below. o un'''R'''aw (take control of keyboard back from X11, # t'''E'''rminate (send SIGTERM to all processes, allowing them to terminate gracefully), # '''k'''Ill (send SIGILL to all processes, forcing them to terminate''' '''immediately), o '''S'''ync (flush data to disk), o '''U'''nmount (remount all filesystems read-only), o re'''B'''oot.
Summary:
Please note that all contributions to TetraWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
TetraWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)