Linux基础知识与命令

"linux"

Posted by yueLng on 2015-02-09

这里记录常用的一些linux命令,以备不时之需。

常用的命令

1
2
3
4
5
6
7
sudo cp xxxx /xxxx #移动文件
sudo mv xxxx xxxxx #为文件改名
sudo tar zxvf xxx.tar #解压文件夹
sudo unrar xxxx.rar #解压文件夹
sudo chmod 775 xxxx #给文件或文件夹加权限
sudo chown -R hadoop:hadoop ./hadoop #修改文件的归属
sudo apt-get remove iptables

防火墙建议直接删除

Ubuntu下安装jdk

首先找到jdk包,我用的是jdk1.7.0_80
接下来的步骤是上传,移动,解压,配置环境
配置环境变量

1
2
3
4
#set jdk environment
export JAVA_HOME=/usr/local/java/jdk1.7.0_80
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

配置环境变量后使用以下命令,使配置生效

1
source /etc/profile

使用java -version查看版本
如果遇到权限问题,在/bin/java和/bin/javac加上以下命令

1
sudo chmod 775 /bin/java

Ubuntu下安装tomcat

一般情况下解压就可以直接用,如果遇到以下信息

  • Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
  • At least one of these environment variable is needed to run this program
    则新建文件 tomcat/bin/setenv.sh ,catalina.out也需要新建。
    后台接收request.getparameter需要在设置useBodyEncodingForURI=”true”
    1
    2
    3
    <Connector port="80" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" useBodyEncodingForURI="true" />
1
2
3
4
#!/bin/bash
export JAVA_HOME=/usr/local/java/jdk1.7.0_80/
export CATALINA_OUT=/var/log/tomcat/catalina.out
export CATALINA_PID=/var/log/tomcat/catalina.pid

使用以下命令运行或关闭tomcat

1
2
3
sudo bin/startup.sh
sudo bin/shutdown.sh
sudo bin/catalina.sh run

unbuntu下安装mysql

1
2
3
4
5
6
7
8
9
sudo apt-get install mysql-server mysql-client
mysql -u root -p
sudo start mysql #手动的话这样启动
sudo stop mysql #手动停止
#三种方法
sudo restart mysql
sudo service mysql start
sudo /etc/init.d/mysql start
pgrep mysqld #查看进程号

修改/etc/mysql/my.cnf文件将bind 127.0.0.1注释。
在mysql5.6中my.cnf的位置更改为/etc/mysql/mysql.conf.d/下的 mysqld.cnf

1
ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL server

如果要远程访问mysql有以下两种方法,这里推荐第二种方法
1。 改表法。可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”localhost”改称”%”

1
mysql -u root -pvmwaremysql>use mysql;mysql>update user set host = '%' where user = 'root';mysql>select host, user from user;
  1. 授权法。例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。
1
2
3
4
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.10.40.54' IDENTIFIED BY '123456' WITH GRANT OPTION;

localhost、127.0.0.1与本机IP地址的区别

localhost也叫local ,正确的解释是:本地服务器
127.0.0.1在windows等系统的正确解释是:本机地址(本机服务器)
他们的解析通过本机的host文件,windows自动将localhost解析为127.0.0.1

localhot(local)是不经网卡传输!这点很重要,它不受网络防火墙和网卡相关的的限制。
127.0.0.1是通过网卡传输,依赖网卡,并受到网络防火墙和网卡相关的限制。
本机IP 也是通过网卡传输的,依赖网卡,并受到网络防火墙和网卡相关的限制。
但是本机IP与127.0.0.1的区别是:
127.0.0.1 只能通过本机访问
本机IP 通过本机访问也能通过外部访问

Ubuntu开机自启与服务脚本

  1. 加入/etc/rc.loal实现开机自启
    1
    root@ubuntu :~# vim /etc/rc.loal

在exit 0 之前添加如下内容:

1
/usr/local/apache-tomcat-7.0.54/bin/startup.sh

  1. 通过启动脚本创建一个服务
    首先,自己要先写一个tomcat.sh,里面实现start、stop、restart等操作。文件内容如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #!/bin/sh
    #
    # description: Auto-starts tomcat
    # processname: tomcat
    case "$1" in
    start)
    sh /usr/local/apache-tomcat-7.0.54/bin/startup.sh
    ;;
    stop)
    sh /usr/local/apache-tomcat-7.0.54/bin/shutdown.sh
    ;;
    restart)
    sh /usr/local/apache-tomcat-7.0.54/bin/shutdown.sh
    sh /usr/local/apache-tomcat-7.0.54/bin/startup.sh
    ;;
    status)
    ;;
    *)
    ;;
    esac
    exit 0

执行以下指令,把tomcat创建为服务:

1
2
3
4
cd /etc/init.d
update-rc.d tomcat defaults 99
chkconfig --add tomcat
chkconfig --level 2345 tomcat on

通过执行以下指令,可以开启服务

1
service tomcat start

如果要卸载服务,则执行以下指令

1
2
cd /etc/init.d
update-rc.d -f tomcat remove

参考资料

1. ubuntu设置自定义程序开机启动