Bash Commands

From wiki.adammorgan.org
Jump to: navigation, search

SEE ALSO: Samba Config for Linux

Basic stuff

Shutdown

Reboot immediately

shutdown -r now

Shutdown immediately

shutdown -h now

Get Information

Show Linux Distribution and version

cat /etc/*-release

Check if CentOS is x86 or x64 (run the following command, and if "lm" is present it's x64)

grep flags /proc/cpuinfo

Searching

Find file by name

find / -name central.php

Find file containing text string (search recursively through sub-directories)

grep -r "16.132" /etc/bacula/conf.d/

Navigation

Show current directory

pwd

Move up one directory

cd ../

File Manipulation

List files (with details)[1]

ls -l
Output:
total 28268
-rw-r--r-- 1 fog  fog   4834144 Oct 10 11:31 2_6_35_7KS
-rw-r--r-- 1 root root 12872000 Jan 15 12:51 3_8_8core
drwxr-xr-x 2 fog  fog      4096 Oct 10 11:31 backup
-rw-r--r-- 1 fog  root 11227312 Nov 20  2012 bzImage
-------------------------------------------------------
Note:
- Field 1: Permissions (owner, group, others)
- Field 2: links
- Field 3: owner
- Field 4: group

Change file ownership

chown -c {newowner} {filename}
Ex: chown -c fog 3_8_8core

Make a copy of a file (useful for backing-up config files before editing them)

cp demofile demofile.bak

Copy all files and directories in dev recursively to subdirectory bak:

cp -R dev bak

Rename a file from "foo" to "bar"

mv /tmp/foo /tmp/bar

Delete file

rm abc.txt

Delete directory (all files and folders recursively)

rm -rf /home/data/2000

Download file from internet (note: if unsuccessful, ensure your DNS is configured!)

wget http://folk.uio.no/trondham/software/files/check_openmanage-3.7.11.tar.gz

Create .tar.gz archive of a directory (copies it into a .tar archive, then compresses with gzip)

tar cvzf archive_name.tar.gz dirname/

Extract .tar

tar -xvf file.tar

Extract .tar.gz

tar -xzvf file.tar.gz

Scripts

Run a bash script

sh file.sh

Services

Ref: http://www.ss64.com/bash/chkconfig.html List all services

/sbin/chkconfig --list
OR SIMPLY:
chkconfig

Optional) check to see if service is already in chkconfig (otherwise you can't set it to start at boot)

/sbin/chkconfig --list sshd

Set service to start at boot (ex: "sshd")

/sbin/chkconfig sshd on

Turn nscd off in runlevels 2, 3, 4, and 5 (these are the default runlevels affected)

chkconfig nscd off

Turn nscd off in runlevels 3, 4, and 5

$ sudo chkconfig --level 345 nscd off

Turn apache on or off (CentOS)

/etc/init.d/httpd start
/etc/init.d/httpd stop

Stop iptables

# /etc/init.d/iptables save
# /etc/init.d/iptables stop

Start iptables

# /etc/init.d/iptables start

Network

NetworkManager

  • Gnome's built-in network mgmt utility, enabled by default with LiveCD install. It's recommended to use only one utility to manage network interfaces.
  • nmcli - command-line tool for controlling NetworkManager (included in NetworkManager package) (not great for configuration, better for monitoring)

Remove NetworkManager and configure CentOS networking by command-line

  • Uses RHEL-native "network" service, and simple networkscripts that are manually editable.

FTP

LFTP

lftp is a pretty good FTP client for when you're working in the shell. It's native on at least some distros.

  • To enter lftp shell:
lftp
  • Exit shell:
exit
  • To download file:
lftp -e 'set net:timeout 10; get yourfile.mp4; bye' -u user,password ftp.foo.com

CURL

curl is another common FTP client.

Webserver stuff

  • To find the username a webserver uses:
    • create a new file "whoami.php" containing the following:
      • <?php
      • echo shell_exec('whoami');
      •  ?>
    • Common default webserver usernames include "apache", "apache2", and "www-data".
  • If installing/configuring a script (eg. Wordpress, GLPI, etc) and you get warning about file permissions, try making the webserver user the owner of that directory:
    • chown [-R] USERNAME DIRECTORY
    • "-R" does this recursively

Package Management

yum (RHEL/CentOS 5.x or above)

List all installed packages (one screen at a time)

yum list installed | more

Search for installed package "httpd"

yum list installed httpd

Search for available packages across all installed repos

yum search [keyword]

Display info about an installed package

yum info [packagename]

Location of yum's repository definitions (one file for each installed repo)

cd /etc/yum.repos.d

Location of yum's config file

/etc/yum.conf [3]

Group commands

https://thevagabondgeek.com/centos-yum-groupinstall-basics/

Updating php above 5.3 in CentOS 6 using webtatic repo

http://www.shayanderson.com/linux/centos-5-or-centos-6-upgrade-php-to-php-54-or-php-55.htm

yum repo management

To find all repositories currently "known" (enabled and disabled):

yum repolist all

Disable a repo:

edit the repo file, and set "enabled=0" for each entry in the file

Force yum to search or install from a specific repo: ("remi" in this case

yum --disablerepo="*" --enablerepo="remi" list available
yum --disablerepo="*" --enablerepo="remi" search php

rpm

Search for installed package "httpd"

rpm -qa | grep httpd*

Install a package

rpm -Uvh foo-1.0-1.i386.rpm

dpkg (debian)


List installed packages

dpkg -l
dpkg -l | more

List installed packages whose names start with "sqlite"

dpkg -l | grep sqlite*

aptitude (ubuntu)

To get just the packages which were expressly installed (not just installed as dependencies), you can run
aptitude search '~i!~M'

MySQL

  • enable logging for this session:
SET GLOBAL general_log =1