Custom Search

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 search files using locate command

Ubuntu locate files tutorial is a guide on how to use locate command to find files in the system.


This is a part of locate manual page:



NAME
locate - find files by name
SYNOPSIS
locate [OPTION]... PATTERN...DESCRIPTION

locate reads one or more databases prepared by updatedb(8) and writes filenames
matching at least one of the PATTERNs to standard output, one per line.


Here is an example of how to use locate command to find file in Ubuntu system:




luzar@ubuntu:~$ sudo locate mkdir
[sudo] password for luzar:
locate: can not open `/var/lib/mlocate/mlocate.db': No such file or directory
luzar@ubuntu:~$


Oppss...what's wrong? Well, that's a usual warning we'll get on the first time we use locate command. So we need to update mlocate database first. Use command updatedb to update mlocate.db like an example below:




luzar@ubuntu:~$ sudo updatedb


It'll take some times for updatedb command to finished update mlocate database. When it finished, try the locate command again.




luzar@ubuntu:~$ sudo locate mkdir

/bin/mkdir
/usr/lib/klibc/bin/mkdir
/usr/lib/perl/5.8.8/auto/POSIX/mkdir.al
/usr/share/man/man1/mkdir.1.gz
luzar@ubuntu:~$


The command success this time. So what locate without any option does, it prints all matching mkdir it can find in the mlocate database. However, since locate command search files in the database, it can't tell if the files still exist. It also can't find new files that haven't been update into the database.



Ubuntu locate files using -c option example:



luzar@ubuntu:~$ locate -c mkdir
4
luzar@ubuntu:~$ locate -c adduser
43
luzar@ubuntu:~$

The locate -c option prints result numbers instead of file names. The result numbers are the matching pattern locate has counted. Below is another example of locate -c but this time we use it with --basename option.



luzar@ubuntu:~$ locate --basename -c adduser
29


Now with another option:



luzar@ubuntu:~$ locate --wholename -c adduser
43

See the result different? The --basename match only the base name against the specified patterns. While the --wholename does the opposite.



Ubuntu locate files using -i option example:



luzar@ubuntu:~$ locate -i \file.txt
/home/luzar/File.txt
/home/luzar/New_File.txt
/usr/share/doc/dovecot-common/wiki/AuthDatabase.PasswdFile.txt
luzar@ubuntu:~$


The locate -i option will find matching pattern ignoring case distinctions. Note that I used Linux glob character \ to find the matching characters.



That's it for now. Below is my final notes of locate command for you:



luzar@ubuntu:~$ ls -l
total 32-rw-r--r-- 1 luzar luzar 0 2008-10-21 22:16 File.txt
drwxr-xr-x 3 luzar luzar 4096 2008-10-23 05:00 folder
-rw-r--r-- 1 luzar luzar 268 2008-10-23 05:43 interfaces.bac
luzar@ubuntu:~$ mv interfaces.bac Interfaces.txt


I changed the file interfaces.bac to Interfaces.txt. There's no more interfaces.bac in the folder. Now, let's check with locate command to find the file.



luzar@ubuntu:~$ locate -i \interfaces
/etc/network/interfaces
/home/luzar/interfaces.bac
/usr/lib/ppr/interfaces
/usr/lib/ppr/interfaces/foomatic-rip
/usr/lib/ppr/interfaces/ppromatic


See that the interfaces.bac still in the database? Where is interfaces.txt?




luzar@ubuntu:~$ locate -i \interfaces.txt
luzar@ubuntu:~$


No Interfaces.txt file. Why? I forgot to update mlocate database. Now, again:




luzar@ubuntu:~$ sudo updatedb
[sudo] password for luzar:
luzar@ubuntu:~$ locate -i interfaces
/etc/network/interfaces
/home/luzar/Interfaces.txt
/usr/lib/ppr/interfaces
/usr/lib/ppr/interfaces/foomatic-rip
/usr/lib/ppr/interfaces/ppromatic
luzar@ubuntu:~$


There it is. My final tip, update the mlocate database frequently.

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 mysql reset root password

Perhaps you provided the wrong password? Don't worry, you can reset Ubuntu mysql root password, here's how to do it:

If mysql is running, then you have to stop it first. If you not sure, we can check Ubuntu running services with ps command to check mysql service is there or not.


luzar@ubuntu:~$ ps aux | grep mysqld
root 4078 0.0 0.1 1772 524 ? S 05:19 0:00 /bin/sh /usr/bin/mysqld_safe
mysql 4120 0.0 3.1 127088 16312 ? Sl 05:19 0:02 /usr/sbin/mysqld --basedir
root 4122 0.0 0.1 1700 556 ? S 05:19 0:00 logger -p daemon.err -t
luzar 4885 0.0 0.1 3004 752 pts/0 R+ 09:50 0:00 grep mysqld


If you see mysql service like in the example above, then you need to stop it. All you need from the ps command above is the process id. Look closely at mysql service, and write down that number. Then you can issue the command below:


luzar@ubuntu:~$ sudo kill 4120
luzar@ubuntu:~$ ps aux | grep mysqld
luzar 4891 0.0 0.1 3004 752 pts/0 R+ 09:50 0:00 grep mysqld
luzar@ubuntu:~$


We can use the same ps command as before to check that mysql really died. So there is no more mysql service.

There are two ways to reset mysql root password. The first way is by putting a script of mysql command to reset root password in a file. Then run mysqld_safe command with the file.

The second way is by running mysql directly from the shell.

Here is a step by step example of resetting mysql root password with a file:

1) Create a text file in your home directory named mysql-passwd with the content below:


UPDATE mysql.user SET Password=PASSWORD('Password') WHERE User='root';
FLUSH PRIVILEGES;


2) Now you can start mysql with the mysql password file to reset Ubuntu mysql root password. Follow the syntax below but change the password name to yours.


luzar@ubuntu:~$ mysqld_safe --init-file=/home/me/mysql-passwd &


3)After the mysql server successfully started, delete the root password file you in your home directory.

4)You can now login mysql using your new password.