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

Ubuntu terminal not responding tips

Have you ever seen this problem when using Ubuntu terminal? You run a command, then it just hang there, not responding. You press Enter, Esc, Ctrl+c, Ctrl+z, type anything but it won't respond. Well, I always have because I work in terminal a lot.

Don't worry. You do remember that Linux is multiuser and multitasking operating system, do you? So, we don't have to restart our Ubuntu system. Here is what I do:

1) Open other terminal and run ps command. Use grep command to grab the not responding terminal. We need the PID (process id) of the hanged command.


luzar@ubuntu:~$ ps aux | grep tty1
root 4356 0.0 0.2 2568 1212 tty1 Ss 13:04 0:00 /bin/login --
root 4362 0.0 0.3 4144 1808 tty1 S+ 13:05 0:00 -bashluzar
4619 0.0 0.1 3004 756 pts/0 R+ 14:05 0:00 grep tty1


2) Use the kill command to terminate the not responding process.


luzar@ubuntu:~$ sudo kill 4362
[sudo] password for luzar:
luzar@ubuntu:~$ ps aux | grep tty1
root 4624 0.0 0.1 1716 516 tty1 Ss+ 14:05 0:00 /sbin/getty 38400 tty1
luzar 4626 0.0 0.1 3004 756 pts/0 R+ 14:05 0:00 grep tty1


3) If it's still there, you can force terminate with kill -9 option or just kill the login terminal.


luzar@ubuntu:~$ sudo kill -9 4628
luzar@ubuntu:~$ ps aux | grep tty1
root 4632 0.0 0.0 1716 512 tty1 Ss+ 14:06 0:00 /sbin/getty 38400 tty1
luzar 4634 0.0 0.1 3004 752 pts/0 R+ 14:06 0:00 grep tty1


Here is what happened in the not responding terminal when we were using the kill command:

Ubuntu ssh connection refused

Ubuntu ssh connection refused can be caused by several factors. This is a guide for Ubuntu ssh connection refused troubleshooting.

1) Check whether ssh is installed in your system using dpkg tool.


luzar@ubuntu:~$ dpkg -l | grep ssh
ii openssh-blacklist 0.1-1ubuntu0.8.04.1 list of blacklisted OpenSSH RSA and DSA keys
ii openssh-client 1:4.7p1-8ubuntu1.2 secure shell client, an rlogin/rsh/rcp repla
ii openssh-server 1:4.7p1-8ubuntu1.2 secure shell server, an rshd replacement
luzar@ubuntu:~$


2) Check ssh daemon is running in your server with ps command.


luzar@ubuntu:~$ ps aux | grep sshd
root 4191 0.0 0.1 5316 1016 ? Ss Nov05 0:00 /usr/sbin/sshd
root 4608 0.0 0.7 11356 3728 ? Ss Nov05 0:00 sshd: luzar [priv]
luzar 4610 0.0 0.3 11512 2036 ? S Nov05 0:02 sshd: luzar@pts/0
luzar 14146 0.0 0.1 3008 776 pts/0 S+ 04:14 0:00 grep sshd
luzar@ubuntu:~$


3) Make sure firewall is not blocking ssh. This is a default Ubuntu iptables with no rules configured.


luzar@ubuntu:~$ sudo iptables -L
Chain INPUT (policy ACCEPT)target prot opt source destination
Chain FORWARD (policy ACCEPT)target prot opt source destination
Chain OUTPUT (policy ACCEPT)target prot opt source destination
luzar@ubuntu:~$


4) Use netstat command to check that ssh is listening on port 22.


luzar@ubuntu:~$ sudo netstat -nap | grep :22
[sudo] password for luzar:
tcp6 0 0 :::22 :::* LISTEN 4191/sshd
tcp6 0 52 192.168.44.128:22 192.168.44.1:1219 ESTABLISHED 4608/sshd:luzar [p
luzar@ubuntu:~$

Ubuntu server mount usb drive

Ubuntu server automatically detect usb drive when you plug it in the usb port. Here is an example of how Ubuntu detected my usb drive or thumb drive when I plugged it:



Ubuntu automatically assign the usb drive as sdb. If your Ubuntu doesn't detect the usb drive, pull it out and try connect it again.

Before mount the usb drive, normally I would create a new directory called usb in /mnt to be a destination place for the mounted usb drive.

luzar@ubuntu:~$ sudo mkdir /mnt/usb

You can mount it in /mnt if you want, it doesn't matter. I created a special directory for usb drive because sometimes I need to mount several file systems and devices, so I need a mount point for each of them.

The mount syntax is mount device destination. Don't forget the sudo command. Here is an example of Ubuntu server mount usb drive:

luzar@ubuntu:~$ sudo mount /dev/sdb1 /mnt/usb/
luzar@ubuntu:~$ ls /mnt/usb/
logo.png usbthumbdrive.bmp
luzar@ubuntu:~$

That's it.

Ubuntu help for beginners

Ubuntu help for beginners tutorial is a guide on how to find help in Ubuntu. I am not going for a long lecture because I know you're in trouble. Otherwise you won't looking for help, won't you? Don't worry, Ubuntu comes with several powerful online help to assist you working in Ubuntu. You don't need to call help desk. Here are what you need:

Using --help

The first thing you should try is --help. Most command have this option. The syntax is command --help. It'll display information about the command. Here is an example of --help:
luzar@ubuntu:~$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
As you can see from the example above, --help is used with Ubuntu command to display help about the command's usage and options available.

Ubuntu help using man command

The man command is used to read the online reference manuals. Perhaps the man command is the most famous help in Ubuntu and other Unix-based system. It's a bit weird, but to open man manual, type man man. Here is a part of manual page about man command:
NAME
man - an interface to the on-line reference manuals

SYNOPSIS
man [-c|-w|-tZ] [-H[browser]] [-T[device]] [-X[dpi]] [-adhu7V] [-i|-I]
[-m system[,...]] [-L locale] [-p string] [-C file] [-M path] [-P
pager] [-r prompt] [-S list] [-e extension] [--warnings [warnings]]
[[section] page ...] ...
man -l [-7] [-tZ] [-H[browser]] [-T[device]] [-X[dpi]] [-p string] [-P
pager] [-r prompt] [--warnings[warnings]] file ...
man -k [apropos options] regexp ...
man -f [whatis options] page ...

DESCRIPTION
man is the systems manual pager. Each page argument given to man is
normally the name of a program, utility or function. The manual page
associated with each of these arguments is then found and displayed. A
section, if provided, will direct man to look only in that section of
the manual. The default action is to search in all of the available
sections, following a pre-defined order and to show only the first page
found, even if page exists in several sections.
Ubuntu (and all Linux) administration command manual is in section 8. You can see a section number is printed at the top of the page every time you open the manual page. See an example below:



Here are the complete section numbers of the manual and the
types of pages they contain.
1   Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conven-
tions), e.g. man(7), groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]
Normally each manual page provides the information subject below:

  • NAME

  • SYNOPSIS

  • DESCRIPTION

  • OPTIONS

  • FILES

  • SEE ALSO

  • BUGS

  • AUTHOR

Ubuntu help using whatis command

Another Ubuntu help available is whatis command. The different between man command and whatis command is, whatis displays only the description. Here is a part of whatis manual page:
NAME
whatis - display manual page descriptions

SYNOPSIS
whatis [-dlhV] [-r|-w] [-s section] [-m system[,...]] [-M path] [-L
locale] [-C file] name ...

DESCRIPTION
Each manual page has a short description available within it. whatis
searches the manual page names and displays the manual page descripâ
tions of any name matched.

name may contain wildcards (-w) or be a regular expression (-r). Using
these options, it may be necessary to quote the name or escape (\) the
special characters to stop the shell from interpreting them.
Example of Ubuntu help using whatis command:
luzar@ubuntu:~$ whatis info
info (1) - read Info documents
info (5) - readable online documentation
luzar@ubuntu:~$

Ubuntu help using apropos command

Apropos command is another Ubuntu help for beginners. The apropos command search the manual page names and descriptions.
NAME
apropos - search the manual page names and descriptions

SYNOPSIS
apropos [-dalhV] [-e|-w|-r] [-s section] [-m system[,...]] [-M path]
[-L locale] [-C file] keyword ...

DESCRIPTION
Each manual page has a short description available within it. apropos
searches the descriptions for instances of keyword.
Example using apropos to get help in Ubuntu system:
luzar@ubuntu:~$ apropos adduser
/etc/adduser.conf (5) [adduser.conf] - configuration file for adduser(8) and ...
adduser (8) - add a user or group to the system
adduser.conf (5) - configuration file for adduser(8) and addgroup(8) .
luzar@ubuntu:~$

Ubuntu help using info command

Another help you can get in Ubuntu is info command. Info command prints info documentations. You can use info command to read a complete information about certain topic in Ubuntu. The info command is like a book. Here is an information in manual page:
NAME
info - read Info documents

SYNOPSIS
info [OPTION]... [MENU-ITEM...]

DESCRIPTION
Read documentation in Info format.
Example of info command:

luzar@ubuntu:~$ info cpio




As you see from the example above, info documentation is like a book. To read info documentation, you can use space bar to view next page. The up/down keys also can be used to scroll up/down the page.

Ubuntu terminal shortcut

Ubuntu terminal shortcut is a shortcut key used in Ubuntu command line terminal. It is not just a normal shortcut. Ubuntu terminal shortcut has special function and some of them are very useful and you are going to use them often. Believe me.

There maybe other Ubuntu terminal shortcut available, but here are the mostly used in my system:

  • Ctrl + d = Logout. Press Ctrl + d to logout or exit your session. This terminal shortcut can protect your server from unauthorize access to your system. It should be your habit to press Ctrl + d whenever you leave your server. Ctrl + d shortcut also can be used to end Ubuntu command which waiting for user input.

  • Ctrl + c = Terminate running command. Press Ctrl + c to stop or end running command on the screen. This terminal shortcut is useful especially if you want to terminate a command which give non-stop result running on the screen or the command you invoke takes too long to execute.

  • Ctrl + z = Stop or cancel running task. This terminal shortcut act the same as Ctrl + c. The different is the situation it can be used. Ctrl + z usually use to stop or cancel operation in the middle of running tool or executable program.
  • Ctrl + r = Open the reverse-i-search tool looking for previously used command in history. This terminal shortcut is extremely useful if you want to recycle your previous long command which you hate to type. Here is an example:




You just type part of the command that you need, and reverse-i-search tool will display the full command for you. When you find the right command, just enter to run the command.

Ubuntu cannot delete files from trash

This Ubuntu tips are for Ubuntu server with x-window installed, Ubuntu desktop and perhaps other Linux distributions as well.

I have seen this problem a few times in my FTP server. When I try to clear the trash from x-window, Ubuntu cannot delete the files from trash. I know that must be permission problem. I normally log in to my server using normal user, especially x-window.

So what I did was, I restored the file that cannot be deleted. As I said earlier, it was a file from my ftp server. I open a terminal in x-window and change directory to ftp directory. Long listing the directory content to see the file's permission and ownership (then smile). That's not necessary, just a habit and satisfaction for me. Then switch to root and delete the file. Don't ask me why I chose the way I did just to delete files from trash. Sometimes I did what I did just to have fun using Ubuntu commands.

The normal method most users would do if Ubuntu cannot delete files from trash is change to trash directory and use sudo command for root privilege to delete the file.

luzar@ubuntu:~$ cd
luzar@ubuntu:~$ pwd
/home/luzar
luzar@ubuntu:~$ cd .local/share/Trash/
luzar@ubuntu:~/.local/share/Trash$
luzar@ubuntu:~/.local/share/Trash$ sudo rm -r

Use rm -rf instead if you have many files in the folder. If you need help with delete files in Ubuntu, check my previous post, Ubuntu delete file. Note for Ubuntu user with gnome, the trash directory is in the /home/username/.Trash.

Ubuntu server login as root

As you already know, Ubuntu does not prompt for root password during installation process. Ubuntu just asked to create a normal user account and to provide password for the user. What about the root account and root password? Perhaps it use the same password as the user account we created? Let's try:



We got login incorrect. No. That's not the Ubuntu root password. Then how do we login as root into Ubuntu server?

All Linux distributions created root account during installation. In Ubuntu server case, the root account has been disable for safety purpose. You know, root can do anything in the system. Like the agents in the Matrix, you know. So, Ubuntu has reasons not to let user login into the system using root. But if you really need to login with root anyway, then here is how I do it:

Firstly, login with the normal user account we created during the installation. Then issue this command: sudo passwd root. See example below:

luzar@ubuntu:~$ sudo passwd root
[sudo] password for luzar:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
luzar@ubuntu:~$


The sudo command give us root privilege to change the password for user root. When we press enter, Ubuntu server will ask for user password. This is the normal user account password, not the root password. So, enter the user password. Then, Ubuntu will ask for the new UNIX password. Now this is our root password. So enter whatever you choose to be your root password. You are going to be asked to enter the root password again for confirmation. If Ubuntu confirm the password update with this:
passwd: password updated successfully, then the root password setup is successful.

We can logout and test our root account like the example below:



That's it. Please remember that root is the most powerful user in Linux system. Login as root only for administration purposes only. I would recommend login as a normal user and use sudo command. If you are not comfortable with sudo, then switch to root with su command. You can switch back to normal user with exit command after finished your administration tasks.

Ubuntu mount cdrom command line

This is a tip on how to mount cdrom in Ubuntu command line. In Ubuntu, mounting a cdrom and dvdrom is the same. You must use sudo command to invoke the mount command because only root can mount a cdrom. So you must have a root privilege to that.

luzar@ubuntu:~$ sudo mount /dev/cdrom /mnt/dvd/
mount: block device /dev/scd0 is write-protected, mounting read-only
luzar@ubuntu:~$


Ubuntu tells us that the mounting is read-only. So if we are going to do something with the file, we have to copy it else where. Now let's browse the cdrom we mounted in the /mnt/dvd directory.

luzar@ubuntu:~$ ls -l /mnt/dvd/
total 143
-r-xr-xr-x 1 root root 942 2011-05-22 02:07 cdromupgrade
dr-xr-xr-x 3 root root 2048 2011-05-01 00:38 dists
dr-xr-xr-x 3 root root 2048 2011-05-01 00:38 doc
dr-xr-xr-x 3 root root 2048 2011-05-01 00:39 install
dr-xr-xr-x 2 root root 12288 2011-05-01 00:39 isolinux
-r--r--r-- 1 root root 119942 2011-05-01 00:39 md5sum.txt
dr-xr-xr-x 2 root root 2048 2011-05-01 00:38 pics
dr-xr-xr-x 4 root root 2048 2011-05-01 00:38 pool
dr-xr-xr-x 2 root root 2048 2011-05-01 00:38 preseed
-r--r--r-- 1 root root 230 2011-05-01 00:38 README.diskdefines
lr-xr-xr-x 1 root root 1 2011-05-01 00:38 ubuntu -> .
luzar@ubuntu:~$


That all about the Ubuntu mount cdrom command line tips.

Ubuntu server login message

When I first login into Ubuntu server, I got this login message:


Ubuntu login: luzar
Password:
Linux ubuntu 2.6.24-19-server #1 SMP Wed Jun 18 15:18:00 UTC 2008 i686
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/
Last login: Sat Oct 18 12:14:07 2008
luzar@ubuntu:~$

And every time I login I got the same message. Why don't we change the message to something else? The Ubuntu server login message is called motd, stand for message of the day. How do we change the meesage if don't know where it is? We ask Ubuntu:

luzar@ubuntu:~$ whereis motd
motd: /etc/motd.tail /etc/motd /usr/share/man/man5/motd.5.gz
luzar@ubuntu:~$

So, its location is in /etc directory. We know where the location is but how do we edit the message? To edit the message, we use a text editor called vim. Now, please remember some vim basics before we start editing.



  1. No mouse

  2. Use arrow up/down to move the cursor.

  3. i to insert text.

  4. Esc to finish.

  5. dd to delete line

  6. x to delete character.

  7. :q! to force exit without save.

  8. :qw to save and exit.


If you're ready, enter the command below:

luzar@ubuntu:~$ sudo vim /etc/motd
[sudo] password for luzar:

Press enter and you'll see the Ubuntu login message open in vim text editor.


Linux ubuntu 2.6.24-19-server #1 SMP Wed Jun 18 15:18:00 UTC 2008 i686
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/
~
~
~
~
~
"/etc/motd" [readonly] 11 lines, 417 characters

Firstly, we are going to delete all the the login message. It's easier if we delete the whole line. So, place the cursor at the beginning of the first line, and press dd. Repeat until you delete all the message content.


Now we can put whatever we want as a welcome message. Press i to insert data. Type whatever message to pleased yourself and replace the default Ubuntu login message.

Finally, we need to save our new message. Key in this:

This is my machine, so I can do whatever I want.
~
~
~
:wq!

That's all. If anything bad happen, I mean during the editing session in vim editor, please use the basic vim tips I mention before. I know it's quite annoying for a newbie. But you'll get used to it in the near future.


So that's it.

Ubuntu scp command guide

There are many methods available to copy files from Ubuntu server. The common method is file transfer protocol. You can see that in my previous post, Ubuntu ftp guides. Another way of copying files from Ubuntu server is using scp command. Let's see a brief information about scp from manual page:




NAME
scp -- secure copy (remote file copy program)

SYNOPSIS
scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 ... [[user@]host2:]file2

DESCRIPTION
scp copies files between hosts on a network. It uses ssh(1) for data
transfer, and uses the same authentication and provides the same security
as ssh(1). Unlike rcp(1), scp will ask for passwords or passphrases if
they are needed for authentication.

File names may contain a user and host specification to indicate that the
file is to be copied to/from that host. Local file names can be made
explicit using absolute or relative pathnames to avoid scp treating file
names containing ':' as host specifiers. Copies between two remote hosts
are also permitted.


As you can see from the manual page above, scp is a secure copy command uses ssh protocol to copy files from remote machine. We should know why it's called secure copy. It uses ssh protocol so it means data transferred is encrypted, and authentication is needed to copy the data.


Maybe it's easy for advanced Linux user to read and understand manual page. Can beginner understand the synopsis above and be able to use scp just by referring to the synopsis alone? Some can but mostly can't. So here's a simple scp format for most of you who can't read manual yet:


scp [options] [source_file] [destination_file]


scp [-r -p -v] [user@host:filename] [user@host:filename]


Here is an example of using scp command:




luzar@ubuntu:~$ scp luzar@192.168.1.6:/home/luzar/etc/UserManual.pdf .
luzar@192.168.1.6's password:
UserManual.pdf 100% 3812KB 3.7MB/s 00:00
luzar@ubuntu:~$


The example above demonstrate how to use scp to remotely copy a file. The target(remote server) is luzar@192.168.1.6 and the destination is our current directory (. means current directory). If you want to copy a folder, add -r option for the scp command.

How to check Ubuntu server Linux version

If you are a Windows user before, there is no ver command in Linux that can be used to check the version of Ubuntu server.  In  Linux  or  Ubuntu  server specifically, there are many commands that can be used to check operating system version.

The first command that can be used to check Ubuntu server version is, as Ubuntu suggest, the lsb_release -a command. Here is the example:

luzar@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 8.10
Release:        8.10
Codename:       intrepid
luzar@ubuntu:~$

Second command that we can use to check Linux version is cat /proc/version. See example below:

luzar@ubuntu:~$ cat /proc/version
Linux version 2.6.27-14-server (buildd@palmer) (gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu12) ) #1 SMP Wed Apr 15 19:44:38 UTC 2009
luzar@ubuntu:~$
 
We can also view all information about Ubuntu server using uname -a option:

luzar@ubuntu:~$ uname -a
Linux ubuntu 2.6.27-14-server #1 SMP Wed Apr 15 19:44:38 UTC 2009 i686 GNU/Linux

There you go. We've got more information than what we ask for. Well, that's good, isn't it?

How to mount external hard drive in Ubuntu server

This is a step by step guide on how to mount external hard drive in Linux for Ubuntu server beginner and Linux beginner in general. The guide includes error happened during the process to show how it to mount device in real situation.

Mount external hard drive in Ubuntu server

Insert external hard drive usb connector into the Ubuntu server usb port.  Ubuntu server automatically detect the external hard drive as a usb device. Below is the example screenshot when ubuntu detected the external hard drive:

 
We can see that the external hard drive has been detected as sdb. That is the name of the external hard drive to use when we are going to mount the device. We can check the device in /proc/scsi/scsi file. To do that, issue the command as in the example below:

luzar@ubuntu:~$ cat /proc/scsi/scsi
Attached devices:
Host: scsi1 Channel: 00 Id: 00 Lun: 00
  Vendor: NECVMWar Model: VMware IDE CDR10 Rev: 1.00
  Type:   CD-ROM                           ANSI  SCSI revision: 05
Host: scsi2 Channel: 00 Id: 00 Lun: 00
  Vendor: VMware,  Model: VMware Virtual S Rev: 1.0
  Type:   Direct-Access                    ANSI  SCSI revision: 02
Host: scsi3 Channel: 00 Id: 00 Lun: 00
  Vendor: Generic  Model: USB Disk         Rev: 9.02
  Type:   Direct-Access                    ANSI  SCSI revision: 02
luzar@ubuntu:~$

Create a proper directory in Ubuntu server to mount the external hard drive. In this example, we create a directory named extdisk in /mnt directory:

luzar@ubuntu:~$ sudo mkdir /mnt/extdisk
[sudo] password for luzar:
luzar@ubuntu:~$ ls /mnt/
dvd  extdisk  usb  win

Now we can mount the external hard drive with Linux mount command:

luzar@ubuntu:~$ sudo mount /dev/sdb /mnt/extdisk/
mount: you must specify the filesystem type

The external hard drive cannot be mounted. This happened when we mount a windows formatted external hard drive. We must specify the filesystem. Windows filesystem format is known as nsfs-3g in Linux. So we mount the external hard drive again with the complete command:

luzar@ubuntu:~$ sudo mount -t ntfs-3g /dev/sdb /mnt/extdisk/
NTFS signature is missing.
Failed to mount '/dev/sdb': Invalid argument
The device '/dev/sdb' doesn't have a valid NTFS.
Maybe you selected the wrong device? Or the whole disk instead of a
partition (e.g. /dev/hda, not /dev/hda1)? Or the other way around?

We still cannot mount the external hard drive. Again, we mount the external hard drive but this we change the device name from /dev/sdb to /dev/sdb1 as suggested by Ubuntu message above.

luzar@ubuntu:~$ sudo mount -t ntfs-3g /dev/sdb1 /mnt/extdisk/
luzar@ubuntu:~$ ls /mnt/extdisk/
ghost dell170l  ghost mimos  RECYCLER  System Volume Information
luzar@ubuntu:~$

We successfully mounted the external hard drive this time. Now that the external drive has been mounted, we can use it as other directory in Ubuntu.

To unmount the external hard drive, we can use the Linux umount command like in the example below:

luzar@ubuntu:~$ sudo umount /mnt/extdisk/
[sudo] password for luzar:
luzar@ubuntu:~$

That's all.

How to add user to a group in Linux operating system

We can assign user to a group during adding a new user account. But how do we add existing user to a group? This tutorial is a guide on how to add user to a new group. The right Linux command for the job is the usermod command.

This is some information about Linux usermod command from manual page:

NAME
usermod - modify a user account

SYNOPSIS
usermod [options] LOGIN

DESCRIPTION
The usermod command modifies the system account files to reflect the
changes that are specified on the command line.


As you can see, the Linux usermod command can be used to modify a user account. However in this tutorial, we'll only use usermod command to add user to a new group. For this example, we'll create a new group to practice. Use the groupadd command to create a new group:

luzar@ubuntu:~$ groupadd programmer
groupadd: unable to lock group file
luzar@ubuntu:~$ sudo groupadd programmer
[sudo] password for luzar:
luzar@ubuntu:~$

Don't forget to use sudo command in Ubuntu, else you'll get the groupadd: unable to lock group file error as in the example above. Check whether the programmer group has been created in /etc/group file:

luzar@ubuntu:~$ less /etc/group | grep programmer
programmer:x:1001:
luzar@ubuntu:~$

Next, we are going to add user to a new group. For this example, we are going to add a user called luzar to the programmer group. Below are step by step instructions.

Use usermod -G option to add user to a new group:

luzar@ubuntu:~# sudo usermod -G programmer luzar

Use Linux groups command to check whether the programmer group has been added to luzar's group:

luzar@ubuntu:~# groups luzar
luzar : users programmer
luzar@ubuntu:~#

As you can see, the user luzar now has programmer as a second group. We can also check /etc/group to verify user currently in the programmer group:

luzar@ubuntu:~$ less /etc/group | grep programmer
programmer:x:1001:luzar
luzar@ubuntu:~$

That's all.

How to change hostname in Ubuntu server

Change a hostname in Ubuntu server is very easy. From the command line terminal, type hostname newname. View the new hostname with hostname command. See the step by step example on how to change Ubuntu server hostname below:



1) View current hostname:



luzar@hitam:~$ hostname
hitam
luzar@hitam:~$


2) Change hostname and view latest hostname:



luzar@hitam:~$ sudo hostname ubuntu
luzar@hitam:~$ hostname
ubuntu
luzar@hitam:~$


See, that's how easy it is. Heh, don't fall for it. That doesn't change anything. The hostname is still the same as you can see at the prompt. Even reboot won't change anything. The exact method to change the hostname permanently is by editing /etc/hostname file. Here is the step:



1) Open /etc/hostname with your favorite text editor:



luzar@hitam:~$ sudo vim /etc/hostname
[sudo] password for luzar:


Here the vim /etc/hostname screenshot:





Now you can change hostname and save. The changed is permanent even after you reboot your system.

Ubuntu copy paste in command line terminal

Sometimes we need to copy text from somewhere and paste it in Ubuntu command line for some reasons. For example, you are learning Linux bash shell scripting from the Internet. It is faster, easier and safer if you can copy an example bash script from the Internet and paste it in command line rather than typing it. How do you copy and paste text in Ubuntu command line? Here is how I do it.



Using cat command


This is one of the mostly used command in Linux, at least by me. The cat command normally used to read file but also can be used to append text to a file. We are going to use that append part to copy and paste text in command line terminal. Let's see an example:





This is a script from the Internet that I want to copy. So I highlight the text, right-click and choose copy.



Next, open Ubuntu command line terminal and using cat command to paste the text I copied earlier. Here is the steps:




luzar@ubuntu:~$ cat >> paste.txt
NameVirtualHost *
<VirtualHost * >
ServerAdmin webmaster@localhost

DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
luzar@ubuntu:~$ ls -l | grep paste.txt
-rw-r--r-- 1 luzar luzar 432 2011-05-12 18:55 paste.txt
luzar@ubuntu:~$ cat paste.txt
NameVirtualHost *
<VirtualHost * >
ServerAdmin webmaster@localhost

DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
luzar@ubuntu:~$


Here is explanation of the example above. The command cat >> paste.txt will append text(input) into a file name paste.txt. Press enter and you'll see no command prompt. That means cat is waiting for the input. So, right-click to paste the text we copied earlier. When we finished, press Ctrl+d to save and exit.



The ls -l | grep paste.txt and cat paste.txt just to show result of what we did.



That's all. Light and easy.

Add another admin in Ubuntu server

There are several reasons why we need extra admin user in Ubuntu server. One of the reason perhaps it's an account for your boss. Yes, in several occasions, your boss needs root privilege to do whatever 'his job' that need to be done.

Well, whatever your reason is, here is how you can add another user in the admin group in Ubuntu server:

Login into Ubuntu server using user in admin group username(user with root privilege). This is the user that can use 'sudo' command, or the default user you created during Ubuntu server installation.

In the command prompt, type:

luzar@ubuntu:~$ sudo adduser jimi admin
Adding user `jimi' to group `admin' ...
Adding user jimi to group admin
Done.
luzar@ubuntu:~$


Replace jimi with a username that you want to add to the admin group. Let's check the /etc/group file to see whether the user has been added to the admin group:

luzar@ubuntu:~$ cat /etc/group


Here is the result:

luzar:x:1000:
lpadmin:x:112:
sambashare:x:113:
admin:x:114:luzar,jimi
programmer:x:1001:luzar
dhcpd:x:115:
jimi:x:1002:


That's it. Now jimi can run whatever commands that needs root privilege by using sudo command. He has the same power as you now.

How to copy a long file name from thumb drive?

When copying files and directories from thumb drive or usb drive, we always dealing with windows file. There is no problem if you are using Ubuntu desktop because you just right-click the file, copy and paste it in your Linux. In Ubuntu server we are working in the command line terminal. To copy a long file name with white space need some creativity.

Here are some tips on how to copy longer file name with white space from thumb drive/usb drive in Linux command line terminal:

We need to add a backslash at the end of every words in the file name. Here is an example on how to copy longer file with white space:

kucing@ubuntu-server:/media/disk$ ls | grep Linux

Linux copy command tutorial.txt

Linux copy windows file.txt

Linux command copy file example01.txt


Type cp and the first word in the file name:

 kucing@ubuntu-server:/media/disk$ cp Linux


Pressing the tab key wouldn't help here. What you have to do is to put a backslash after the first word:

kucing@ubuntu-server:/media/disk$ cp Linux\ 


Press spacebar to put a white space and key in the second word:

kucing@ubuntu-server:/media/disk$ cp Linux\ copy\


Repeat steps above if you have a long file name.

kucing@ubuntu-server:/media/disk$ cp Linux\ copy\ windows\ file.txt /home/tutorials/


That's it. A simple thing but can cause a headache for a Linux beginner.

unix tips, convert man pages to text files, linux tips, solaris tips, How to replace text in multiple files, execute commands at specified times

 Hi
Below some important general unix tips:
  • How to run a command to a set of files (The while read command can handle names with spaces but you need to quote the variable after):
    [unix]$ for i in `ls *mp3`; do cp $i $i.bak; done
    [unix]$ ls *mp3 | while read i; do cp "$i" "$i with more spaces"; done
  • How to rename part of a file:
    [unix]$ echo filename | sed 's/file/another/'
  • How to replace text in multiple files:
    [unix]$ sed -e 's/oldtext/newtext/' -i file1 file2 file3 ...
  • How to delete lines matching a pattern in some files:
    [unix]$ sed -e '/pattern/ d' -i file1 file2 file3 ...
  • How to change all letters to lowercase:
    [unix]$ echo FilENamE | tr A-Z a-z
  • A good way to change all filenames in a directory to lowercase:
    This handles a colored ls (hence the quotes around ls, but you can use any file list output, i.e. find) and names with spaces (hence the while read and not the for).
    [unix]$ "ls" | while read name; do mv "$name" "`echo $name | tr A-Z a-z`"; done
  • How to convert man pages to text files:
    [unix]$ man man | col -b
  • How to list all the open ports and programs:
    [linux]$ netstat -apne --inet
    [freebsd]$ sockstat
  • How to list all the open files and programs:
    [linux]$ lsof
    [freebsd]$ fstat
  • How to find information about an ip (i.e. 10.0.0.0):
    [unix]$ host 10.0.0.0
    [unix]$ dig -x 10.0.0.0
  • How to find information about an ip owner (i.e. 10.0.0.0):
    [unix]$ whois -h whois.arin.net 10.0.0.0
  • How to find out the mailserver for an ip:
    [unix]$ dig mx some.domain.name.net
  • How to generate a filename with yesterdays date (i.e. for logfiles/backups):
    [linux]$ touch logfile-`date -dyesterday '+%Y-%m-%d'`
    [freebsd]$ touch logfile-`date -v-1d '+%Y-%m-%d'`

Commands/options you should know about, read the man pages
  • tail - display the end of a file
  • head - display the beggining of a file
  • xargs - convert stdin to command line arguments
  • nmap - network scanner
  • watch (linux) - run a command periodically and watch the output
  • watch (freebsd) - monitor a tty
  • lsof - lists the open files
  • strace (linux) - trace the system calls of a program
  • truss (freebsd) - trace the system calls of a program
  • calc - command line calculator program
  • bc - another command line calculator program
  • dc - command line RPN calculator program
  • du - how much disk space is being used by something
  • df - disk free, how much free space there is
  • touch - creates an epty file or updates the timestamp on a file
  • diff - show the difference between 2 files
  • strings - show the strings in a file (useful for binaries)
  • file - tells you what type a file is
  • finger - find out information about users
  • w / who - who is currently on the machine and what are they doing
  • id - tells you who you are
  • uname - tells you basic information about the system.
  • whois - information about hosts
  • crontab - execute commands at specified times
  • last - last users to log into the system
  • mail - very basic mailer, useful for redirection
  • nice/renice - set the niceness ("priority") level of a program
  • nohup - leave a program running after logging off
  • screen - virtual terminal for leaving programs running
  • psnup - print multiple pages in one page
  • time - time the execution of a command
  • wc - word count, tells you the size in bytes, words and lines
  • which / whereis - locates commands
  • locate - locates files
  • whatis - gives brief summary of a comm
  • and
  • wget / fetch - get files off the net
  • talk - chat with other users on the system
  • write / wall - send messages to users of the system
  • zcat - cat gzipped files
  • ln - link files
  • sleep - wait a specified amount of time
  • md5sum (linux) - calculate the md5 signature of a file
  • md5 (freebsd) - calculate the md5 signature of a file
  • enscript - convert text files to ps
  • awk, sed - useful scripting programs
  • cat, echo, more/less, find, grep - you need to know these, it's not an option.

Resource: http://sluglug.ucsc.edu/~isolis/knowledge/unix-tips/