connect to the server using ssh

Log into the server using ssh

To connect to the server using ssh, ssh daemon (ssh server) should be installed on the server and ssh client should be installed on the client (from where you are connecting to the server)
For installation process, click here

Steps
Open terminal on the client and run following command.
$ ssh username@xxx.xxx.xx.xx
where xxx.xx.xx.xx is IP address of the server/.
Example - $ ssh root@192.168.2.7 

It asks for server's root password. Now enter server's password and press return key, you have logged into the server's root account.

if you run 
$ ssh root@192.168.2.7  or $ ssh 192.168.2.7
both are same thing. To log in to root's account, you do not need to mention root as a username. Username is necessary if you log into any user's account.

Suppose you want to log into John's account, Run
$ ssh john@192.168.2.7
where john is username &
192.168.2.7 is IP of the server

It asks for john's password. After submit password, you log into john's account.

Install ssh on Ubuntu/Debian


ssh (secure shell)
         ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine.  It is intended to replace rlogin and rsh, and provide secure encrypted communications between two untrusted hosts over an insecure network.  X11 connections and arbitrary TCP ports can also be forwarded over the secure channel.

  ssh connects and logs into the specified hostname (with optional user name).  The user must prove his/her identity to the remote machine using one of several methods depending on the protocol version used (see below).


Install ssh on Ubuntu/Debian

Run following command in terminal
# apt-get install openssh-server openssh-client

Start and stop the ssh service
# service ssh start & # service ssh stop

Automatically start the service at boot
# chkconfig ssh on

Check status of ssh
# service ssh status

Check port 22 is opened or not
Run following command
# netstat -tulnp | grep :22

Output should be like this
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 9657/sshd tcp 0 0 :::22 :::* LISTEN 9657/sshd

Install ssh on CentPS/RHEL/Fedora
click here

Install ssh on Linux - CentOS/Fedora/RHEL/SL


ssh (secure shell)
         ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine.  It is intended to replace rlogin and rsh, and provide secure encrypted communications between two untrusted hosts over an insecure network.  X11 connections and arbitrary TCP ports can also be forwarded over the secure channel.

  ssh connects and logs into the specified hostname (with optional user name).  The user must prove his/her identity to the remote machine using one of several methods depending on the protocol version used (see below).

sshd (secure shell daemon) - ssh server

Install ssh on CentOS/Fedora/RHEL/Scientific Linux

Run following command in terminal
# yum install openssh-server openssh-clients

Start and stop the ssh service
# service sshd start & # service sshd stop

Automatically start the service at boot
# chkconfig sshd on

Check status of ssh
# service sshd status

Firewall :
Open Port 22 in Firewall :
Add following line in the file /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

Restart iptables
# service iptables restart

Check port 22 is opened or not
Run following command
# netstat -lnp | grep :22

Output should be like this
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 9657/sshd tcp 0 0 :::22 :::* LISTEN 9657/sshd

Install ssh on Debian/Ubuntu
click here

Error: Host key verification failed

Error: Host key verification failed
Please select another viewer and try again.

If you are trying to access directory structure using sftp and you are getting Host Key verification failed error. Try following solutions to remove it.





Solution 1 : Remove key-prints from your known_hosts file
Remove all key-prints of the server from your known_hosts file.
$ ssh -R hostname-of -the-server
Suppose you are trying to access server 192.168.2.2 using
$ ssh 192.168.2.2
and it gives above error. Just remove key prints of the server from your known_hosts file using
$ ssh -R 192.168.2.2
The keys are removed and it gives following message
/root/.ssh/known_hosts updated. Original contents retained as /root/.ssh/known_hosts.old
Now try to log in again.

Solution 2 : Remove key-prints manually from known_hosts file
If you can identify old key prints of the server in the file known_hosts . Remove it and it will ask to add new key-prints when you will try to re-connect.

Solution 3 : Remove known_hosts file
Delete the file known_hosts and it will be created again automatically when you will try to connect to the server using ssh.

If you are trying to connect to the server first time after deleting the file known_hosts, it will ask to add the key-prints in known_hosts file. 
The authenticity of host '192.168.2.2 (192.168.2.2)' can't be established. RSA key fingerprint is 3c:36:9d:ff:10:2c:c6:0d:b1:45:30:9d:cf:2d:42:a6.
Once you add the key prints, it will not ask again for same server. It asks for permission every time when you try to connect to the new server.

Path of known_hosts file :
If you are root - /root/.ssh/known_hosts
If you are user - /home/username/.ssh/known_hosts




WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that the RSA host key has just been changed.


If you are trying to access other host using ssh and you are getting the error. Try following solutions to remove it.




Solution 1 : Remove key-prints from your known_hosts file
Remove all key-prints of the server from your known_hosts file.
$ ssh -R hostname-of -the-server
Suppose you are trying to access server 192.168.2.2 using
$ ssh 192.168.2.2
and it gives above error. Just remove key prints of the server from your known_hosts file using
$ ssh -R 192.168.2.2
The keys are removed and it gives following message
/root/.ssh/known_hosts updated. Original contents retained as /root/.ssh/known_hosts.old
Now try to log in again.

Solution 2 : Remove key-prints manually from known_hosts file
If you can identify old key prints of the server in the file known_hosts . Remove it and it will ask to add new key-prints when you will try to re-connect.

Solution 3 : Remove known_hosts file
Delete the file known_hosts and it will be created again automatically when you will try to connect to the server using ssh.

If you are trying to connect to the server first time after deleting the file known_hosts, it will ask to add the key-prints in known_hosts file. 
The authenticity of host '192.168.2.2 (192.168.2.2)' can't be established. RSA key fingerprint is 3c:36:9d:ff:10:2c:c6:0d:b1:45:30:9d:cf:2d:42:a6.
Once you add the key prints, it will not ask again for same server. It asks for permission every time when you try to connect to the new server.

Path of known_hosts file :
If you are root - /root/.ssh/known_hosts
If you are user - /home/username/.ssh/known_hosts


Add / Remove user to an existing Group in Linux

Users & Groups in Linux : ( Add & Remove )

All Groups of unix system are listed in the file /etc/group
To see the list of all groups of system
$ cut -d: -f1 /etc/group

See all Groups of a user :
$ id -nG username
Suppose I want to see all Groups of user Thomas whose username is thomas :
$ id -nG thomas
The output is name of groups of thomas.
user1 root admin sambashare vboxusers usbuser mysql apache2
Here user1 is the Primary Group (First Group) & others are secondary Groups.
You can use following command too to see groups of a user.
$ groups username
The first group after colon is Primary Group.



Add an existing Group to user (Other groups should not be removed) :
$ usermod -a -G groupname username
Suppose I want to add a new group to user thomas, other groups should not be removed.
$ usermod -a -G newgroup thomas
Now an existing group newgroup has been added to user thomas. To see the changes, Run
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql apache2 newgroup



Add an existing Group to user (other groups should be removed, only Primary Group and currently added Group should be available) :
$ usermod -G groupname username
Suppose I want to add new group to user thomas, other groups should be removed. Only Primary Group and currently added group should be assigned to user.
$ usermod -G grp thomas
Now an existing group grp has been added to user thomas. All other secondary groups have been removed from thomas
To find current groups of user thomas
$ id -nG thomas
The output is
user1 grp
All other secondary groups are replaced with grp. Now there is only one secondary group for thomas.




Remove Group of a user :
You can remove only secondary groups of a user. Primary Group can't be removed by this command. If you want to remove primary group, you have to assign a new primary group for user. A user can't be modified without primary group.
$ gpasswd -d username groupname
Suppose I want to remove a secondary group apache2 from user thomas.
Right now there are following Primary Group & Secondary Groups of user thomas.
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql apache2 newgroup
Now I delete apache2
$ gpasswd -d thomas apache2
See the groups of user thomas
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql newgroup
apache2 has been removed for thomas.



Change Primary Group of a user :
$ usermod -g groupname username
It removes the current Primary Group of the user and replaces with new added primary group. Secondary groups are as it is. There is no change in Secondary Groups.
Suppose I want to change primary group of user thomas.
To see the groups of user thomas
$ id -nG thomas
The output is
user1 root admin sambashare vboxusers usbuser mysql apache2 newgroup
Now I run following command to change Primary Group
$ usermod -g newgroup2 thomas
Now Primary Group user1 has been removed from thomas and it is replaced with newgroup2. Now newgroup2 is primary group of thomas.
To verify the changes, I see the groups list.
$ id -nG thomas
The output is
newgroup2 root admin sambashare vboxusers usbuser mysql apache2 newgroup
Now the Primary Group is newgroup2.


fatal: Queue report unavailable - mail system is down

mail postfix/postqueue[20397]: fatal: Queue report unavailable - mail system is down

If you are getting this error while sending mail via Postfix, Restart the postfix.
$ sudo service postfix restart
Now try to send a mail.

If sendmail and postfix both are installed on the system, stop sendmail and try again with postfix.

If you are still getting same error. Remove and install postfix again.

dpkg: unrecoverable fatal error, aborting: Cannot allocate memory

dpkg: unrecoverable fatal error, aborting: fork failed: Cannot allocate memory

It means you do not have enough memory to execute the operation.

Since it is a dpkg error, try to configure.
$ sudo dpkg --configure -a

But if you are still getting the error.

Check the free and used physical memory in megabytes by following command.
$ free -m
It displays the total amount of free and used physical memory and swap space in the system, as well as the buffers and cache consumed by the kernel.


Flush file system buffers by executing :
$ sync

Now try again.

If you are still facing the same problem, follow this.
To free page cache:
$ echo 1 > /proc/sys/vm/drop_caches

To free dentries and i-nodes :
$ echo 2 > /proc/sys/vm/drop_caches

To free page cache, dentries and i-nodes :
$ echo 3 > /proc/sys/vm/drop_caches

Now try again.

Mysql error : start: Job failed to start

start: Job failed to start Error while starting mysql 

If you are not able to start mysql because of this error, try following solutions.

Solutions :

1) There should not be incorrect bind-address in my.cnf. If you have not uncommented it intentionally to enable binding, the line should be commented. If you are not sure about the address, comment the line and restart mysql.

2) Run following command and try to start mysql.
$ sudo dpkg-reconfigure mysql-server-5.5

3) Remove logfiles and try to start mysql.
$ sudo rm -rf /var/lib/mysql/ib_logfile*

4) If mysqld_safe is running with mysql, kill mysqld_safe
$ sudo killall -9 mysqld_safe

5) If any of these solutions are not working for you, try final solution.
    Remove and re-install mysql again.
Debian based :
$ sudo apt-get --purge remove mysql-server mysql-common mysql-client $ sudo apt-get install mysql-server mysql-common mysql-client
Yum Based :
$ sudo yum --purge remove mysql-server mysql-common mysql-client $ sudo yum install mysql-server mysql-common mysql-client

phpmyadmin : Cannot start session without errors

phpmyadmin error : cannot start session without errors please check errors given in your PHP and/or webserver log file and configure your PHP installation properly.




Open file php.ini.
apache2 (Debian, Ubuntu) : /etc/php5/apache2/php.ini 
httpd (RHEL, CentOS) : /etc/php.ini

Uncomment following line there.
If it is CentOS or RHEL :
session.save_path = "/var/lib/php/session" 
If it is debian or ubuntu :
session.save_path = "/tmp"

Restart Apache.

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Solutions:

1) Try to change root's password. But it does let you reach on command prompt. So what will you do? Follow this.

Stop mysql first
$ service mysql stop or $ service mysqld stop

Open mysql in safe mode
$ mysqld_safe --skip-grant-tables &

Login with root
$ mysql -u root

Update root's password.
mysql> use mysql; mysql> update user set password=PASSWORD("testpassword") where User='root';

If it does not give permission to update password then Flush Privileges first.
mysql> FLUSH PRIVILEGES;

Now try to set password again
mysql> update user set password=PASSWORD("testpassword") where User='root'; mysql>quit
Now root's password has been set to testpassword.

Restart mysql and try to log-in as root.

2) Have you tried to set password for root or any individual database in phpmyadmin or using command line? If you have done this, now you are trying to open phpmyadmin or try to log-in using command line and you are getting this error.

Solution : Put the same password in config file of phpmyadmin for same user.

By default root password is blank in config file and it looks like this.
$cfg['Servers'][$i]['user'] = 'root'; $cfg['Servers'][$i]['password'] = '';
Now set your password here and it will look like this.
$cfg['Servers'][$i]['user'] = 'root'; $cfg['Servers'][$i]['password'] = 'password-goes-here';
Restart mysql.

Operate mysql from command line

Use mysql using command line :

First install mysql client using command line.
apt-get install mysql-client or yum install mysql-client according to your Linux distribution.

Now go on mysql prompt using
$ mysql -h 127.0.0.1

If you want to log-in with root using command line
$ mysql -h 127.0.0.1 -u root

Root's password is blank. If it asks for password, do not write anything, just press Enter key.

$ mysql -h 127.0.0.1 -u root -p


Do not try to log-in with password protected user using command line.

If you try to log-in with password protected user, it gives following error :
ERROR 1045 (28000): Access denied for user 'admin'@'localhost' (using password: YES) 

phpmyadmin error : Script timeout passed

phpMyAdmin: Script timeout passed, if you want to finish import, please resubmit same file and import will resume.





If you are not able to upload large sql files in phpmyadmin because of this error, you need to increase your execution time.

Open config file of your phpmyadmin.

If you are using xampp, you will find the config file inside phpmyadmin directory. complete path : /opt/lampp/phpmyadmin/config.inc.php
If you are using apache2 or httpd
complete path : /usr/share/phpmyadmin/config.inc.php

Add following line in the config file.
$cfg['ExecTimeLimit'] = 0;

Restart Apache & mysql both.

Alternate Solution :
Continue Uploading : If you do not have access to server, you can continue uploading the same gzip, sql, zip, or bzip2 file then the data import will pick up where it left off.

Import mysql database using command line in Linux

Upload mysql database command :

If you want to import a mysql database using command line, here is the command.
$ mysql -h host-name -u username -p'password' "database_name" < "path/of/the/sql/file"

where 
hostname is name of your host. If you upload on your system it can be localhost or 127.0.0.1. If you would upload on server it would be IP of the server.
username is username of mysql user
password is password of mysql user, Remember there is no space between -p & single quote (')
database_name is name of your database in which you want to upload the sql file

Examples:

a) If you want to upload file on your localhost, the command would be (if root has no password)
$ mysql -h 127.0.0.1 -u root "database_name" < /path/of/the/sql/file

b) If you want to upload file on other system of same LAN, the command would be
$ mysql -h 192.168.1.7 -u john -p'johnathan' "webpro" < /home/user/documents/webpro.sql

where 192.168.1.7 - IP of other system of same LAN
john is username of mysql user
johnathan is his password
webpro is database name

NOTE :
Please create database first before running above command otherwise it would give error "ERROR 1049 (42000): Unknown database 'webpro'"

Increase import file size limit in phpmyadmin

Upload large size sql files in phpmyadmin :




1) Open file php.ini

2) Search parameters upload_max_filesize.

3) Increase the file size limit for the parameter.
upload_max_filesize=512M
Now you can upload sql files up to 512 MB in phpmyadmin.
In this example I have set limit 512 MB, you can set according to your requirement.

4) Increase file size limit for post_max_size and memory_limit too.
     It should be equal or greater than upload_max_filesize
post_max_size=1024M memory_limit=1024M

5) Restart Apache.



There is a instruction in php.ini that "Maximum amount of memory a script may consume (128MB)" but when you will increase the memory_limit size greater than 128M, you will see that you are not getting any error and large file has been uploaded successfully.

NOTE :
The first things to check are the values of upload_max_filesize, memory_limit and post_max_size in the php.ini configuration file. All of these three settings limit the maximum size of data that can be submitted and handled by PHP. post_max_size and memory_limit need to be equal or larger than upload_max_filesize.

Be careful to set memory_limit size. If it exceeds the limit, the phpmyadmin does not start. It shows blank page.

If you are not able to upload small size sql file (in-kb/mb) because of change in  memory limit. Make it again 128 MB and Restart Apache


Solution :2
If you are still facing problem while uploading large size sql files, upload them using command line. To see this method, click here .

You should see this post too. Click here.



phpmyadmin error : #1153 - Got a packet bigger than 'max_allowed_packet' bytes

 phpmyadmin- Got a packet bigger than 'max_allowed_packet' bytes

 


Error in phpmyadmin while uploading sql file :

1) Open file my.cnf 
path of the file : /opt/lampp/etc/my.cnf

2) Set value max_allowed_packet=128M under attribute [mysqld] 
    If parameter is not there, add it under attribute [mysqld] .
[mysqld] max_allowed_packet=128M

3) Restart XAMPP.

phpmyadmin - Fatal error : allowed memory size of bytes exhausted

Fatal error: Allowed memory size of bytes exhausted (tried to allocate bytes)




If you are not able to open phpmyadmin because of this error, follow the steps to remove it.

1) Open file php.ini
xampp : /opt/lampp/etc/php.ini
apache2 (Debian) : /etc/php5/apache2/php.ini 
httpd (RHEL) : /etc/php.ini

2) Search parameter memory_limit. By default it's value is 128M.
    Increase the value according to size of parameters upload_max_filesize and post_max_size.
memory_limit = 512M

3) Restart Apache.

There is a instruction in php.ini that "Maximum amount of memory a script may consume (128MB)" but when you will increase the memory size greater than 128M, you will see yourself that error is gone.


NOTE :
The first things to check are the values of upload_max_filesizememory_limit and post_max_size in the php.ini configuration file. All of these three settings limit the maximum size of data that can be submitted and handled by PHP. post_max_size and memory_limit need to be equal or larger than upload_max_filesize.

Be careful to set memory_limit size. If it exceeds the limit, the phpmyadmin does not start. It shows blank page.

You have increased the memory limit but you still get this error frequently. Restart the Apache will solve your problem. If you are working on live Linux server and it does not restart frequently. You might get this error too often, make a Crontab for daily Apache-Restart will solve your problem.

ERROR 2006 (HY000) at line ##: MySQL server has gone away


 MySQL server has gone away : Error while uploading sql file in phpmyadmin




1) Open file my.cnf 
xampp : /opt/lampp/etc/my.cnf
apache2 (Debian) : /etc/mysql/my.cnf 
httpd (RHEL, CentOS) : /etc/my.cnf

2) Set value max_allowed_packet=128M under attribute [mysqld] 
    If parameter is not there, add it under attribute [mysqld] .
[mysqld] max_allowed_packet=128M

3) Restart mysql.

Access forbidden : New XAMPP security concept : XAMPP - phpmyadmin error

Solution :
Access forbidden :  New XAMPP security concept error in phpmyadmin after new XAMPP installation





1) Open the file /opt/lampp/etc/extra/httpd-xampp.conf

2) Search text #New XAMPP security concept in the file and replace Deny from all to Allow from all under #New XAMPP security concept

3) Add line Require all granted in <Directory "/opt/lampp/phpmyadmin">
 Now it looks like this.
<Directory "/opt/lampp/phpmyadmin">     AllowOverride AuthConfig Limit     Require all granted     Order allow,deny     Allow from all     </Directory>

4) Restart XAMPP

If you are still getting problem

Solution : 2
Run following command
$ sudo /opt/lampp/lampp security
Set password for everything.
Restart XAMPP and try again.



User Status in Linux - Check user is locked or unlocked

If you are using CentOS or RHEL :

If you want to check your user's account status,Run following command :
$ passwd -S "username"
Change the text username with your username whose status you want to check.
If your user is locked, it shows
(Password locked.)
If your user is unlocked, it shows
(Password set, MD5 crypt.)
List of all locked accounts :
$ awk -F: '{ system("passwd -S " $1)}' /etc/passwd | grep " LK "
List of all unlocked accounts :
$ awk -F: '{ system("passwd -S " $1)}' /etc/passwd | grep " PS "

If you are using Debian or Ubuntu :

If you are using Debian or Ubuntu and your user is locked, it shows letter L with your username.

If you are using Debian or Ubuntu and your user is unlocked, it shows letter P with your username
List of all locked accounts :
$ awk -F: '{ system("passwd -S " $1)}' /etc/passwd | grep " L "
List of all unlocked accounts :
$ awk -F: '{ system("passwd -S " $1)}' /etc/passwd | grep " P "

Note :
If you are using CentOS 7 and you run following command to list all users with status
passwd -S -a
It gives following error
passwd: bad argument -a: unknown option
 

Use following command to list all users with their status
awk -F: '{ system("passwd -S " $1)}' /etc/passwd

Error 530 : Login incorrect

There can be several reasons of this error. Please try following solutions.

1) If you are using vsftpd, check this attribute in your vsftpd.conf
     local_enable=YES
     Value of the attribute should be YES otherwise you will not be able to log-in as a user. Restart the vsftpd after any change in configuration file.

Path of the vsftpd.conf :
/etc/vsftpd.conf  (if You are using debian or ubuntu)
/etc/vsftpd/vsftpd.conf (if you are using centOS, RHEL or fedora)

2) Check your username in the file ftpusers.
If your username is in the list, you can't log into your ftp account. This file contains the list of usernames who should not log-in. Remove your username from the list and save the file. Now try to log into your ftp account.

Path of the the file ftpusers :
/etc/ftpusers  (if You are using debian or ubuntu)
/etc/vsftpd/ftpusers (if you are using centOS, RHEL or fedora)

3) Open file /etc/passwd and search your username there. If your username's line ends with /sbin/nologin (centos or RHEL) or /usr/sbin/nologin (debian or ubuntu), it means user can not log in. Change the text /sbin/nologin (centos or RHEL) or /usr/sbin/nologin (debian or ubuntu) into  /bin/bashSave the file. Now User will be able to log-in.
Name of the shell should be defined for your username in the file /etc/passwd as well as it should be in the list in file /etc/shells.

4) User should not be locked by root. If user is locked, use following command to unlock. Now try to log-in.
$ sudo passwd -u username

5) If you are using CentOS or RHEL, the port 21 should be defined in the file /etc/sysconfig/iptables. Add following line in the file and restart iptables.
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT

6) Not very funny but check once your username, password & host name carefully. :-)
Related Posts Plugin for WordPress, Blogger...