John's Linux page

From ProgClub
Revision as of 04:21, 15 August 2011 by John (talk | contribs) (Added standard IO section)
Jump to: navigation, search

Hi there, I'm John. I just wanted a page where I could document various Linux things that I bump into. This is that page. Thank you ProgClub. :)

System

Determining which Debian/Ubuntu release your are running

$ lsb_release -r

Determining which Unix you are running

$ uname

Or,

$ uname -a

Environment

Configuring vim as your editor

Sometimes all you need is:

$ export EDITOR=/usr/bin/vim

Which works for svn, for example. Add it to your ~/.profile file to have it set for all login sessions.

Other times you need to run

$ update-alternatives --config editor

And then select vim from the list. This is what you do to configure your visudo editor.

Configuring your locale

$ sudo /usr/sbin/locale-gen en_AU.UTF-8
$ sudo /usr/sbin/update-locale LANG=en_AU.UTF-8

User and group management

Adding a user

To add a new user on a linux system:

# useradd username
# passwd username

To have the home directory created from '/etc/skel' use the 'adduser' script instead:

# adduser username

Adding a user to a group

To add an existing user to an existing group:

# gpasswd -a username group

e.g. to add user 'jj5' to the 'sudo' group:

# gpasswd -a jj5 sudo

Alternatively you can use adduser, passing the username and group:

# adduser username group

e.g. to add user 'sclaughl' to the 'staff' group:

# adduser sclaughl staff

Finding which user your are logged in as

To determine which user you are running as enter the command:

$ whoami

Finding which groups you are a member of

To find which groups you are a member of:

$ groups

or

$ groups username

Where 'username' is the username of the user you are querying, e.g.:

$ groups jj5

Finding who else is logged in to the system

To see who else is logged in,

$ who

Disk management

Checking available disk space

$ df -h

File management

Creating a symbolic link

$ ln -s /path/to/target link-name

Creating a hard-link

$ ln /path/to/target file-name

Changing the owner of a file

$ chown user:group <files>

E.g.

$ chown jj5:staff README
$ chown root:root *

To apply recursively into sub-directories use -R,

$ chown -R root:root /etc/*

Changing file permissions

Object codes
User Group Other
u g o
Permission codes
Read Write Exectue
r w x
4 2 1
Numeric codes
0 None
1 Execute
2 Write
3 Write, Execute
4 Read
5 Read, Execute
6 Read, Write
7 Read, Write, Execute

See Numeric Mode in Action.

$ chmod <user numeric code><group numeric code><other numeric code> <files>
$ chmod <object codes>+|-<permission codes> <files>

E.g.

$ chmod 600 my-private-file
$ chmod go-rwx my-private-file
$ chmod u+rw my-private-file
$ chmod +x my-script

File searching

Finding a file with a particular name

$ find -iname "*some-part-of-the-file-name*"

Will start searching from the current directory, so maybe

$ cd /

first. For a case-sensitive search:

$ find -name "*eXaCT CaSE*"

Finding a file with particular content

To search in /etc/ for a file with particular content:

$ grep -R "search-string" /etc/*

Job control

Stopping a running process

Press Ctrl+Z to stop a running process.

Listing current jobs and their status

$ jobs

Resuming a stopped job in the backgroud

To resume a stopped process in the background

$ bg %1

where '1' is the job number reported by bash when you pressed Ctrl+Z (or ran 'jobs').

Resuming a stopped job in the foreground

To resume a stopped process in the foreground

$ fg %1

where '1' is the job number reported by bash when you pressed Ctrl+Z (or ran 'jobs').

Killing a stopped job

To kill a job

$ kill %1

where '1' is the job number reported by bash when you pressed Ctrl+Z (or ran 'jobs').

Debian/Ubuntu package management

Showing list of installed packages

# dpkg --get-selections

Searching for installed package

# dpkg --get-selections | grep package-name

or

# aptitude search package-name

Showing which files are installed as part of a package

# dpkg -L package-name

Installing a package

# apt-get install package-name

Uninstalling a package

# apt-get remove package-name

Networking

Pinging with particular packet size

$ ping -M do -s <packet size in bytes> <host>

E.g.

$ ping -M do -s 1400 charity.progclub.org

Setting MSS for a particular IP address on a particular interface

# ip route add <host> dev <interface> advmss <packet size>

E.g.

# ip route add 10.0.0.1 dev eth0 advmss 1400

Dropping configured MMS for a particular IP address

# ip route flush <host>

E.g.

# ip route flush 10.0.0.1

IPTables

Applying firewall rules

For configuration info see this article.

$ sudo vim /etc/iptables.test.rules
$ sudo /sbin/iptables -F
$ sudo /sbin/iptables-restore < /etc/iptables.test.rules
$ sudo iptables -L
$ sudo -s
# iptables-save > /etc/iptables.up.rules
# exit

IPSec

Disabling IPSec

# setkey -FP

Pluggable Authentication Modules (PAM)

Links

SSH

Configuring SSH key login

On the client machine generate a key-pair (if necessary, check for existing ~/.ssh/id_rsa.pub):

$ ssh-keygen -t rsa

Copy the public key from the client to the server:

$ scp ~/.ssh/id_rsa.pub user@example.org:

Configure the authorized keys on the server:

$ ssh user@example.org
$ mkdir ~/.ssh
$ chmod go-w .ssh
$ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
$ rm ~/id_rsa.pub

Tunneling over SSH

For example, connecting a remote MySQL server to the localhost:

$ ssh -L 3306:localhost:3306 jselliot@ssh.progsoc.org

If the machine you want to connect to is not the localhost of the machine you're ssh'ing to,

 $ ssh -L 3306:muspell.progsoc.uts.edu.au:3306 ssh.progsoc.uts.edu.au

The -L stanza is localport:remotehost:remoteport where localport is a port on your machine, forwarded to remoteport on remotehost.

Tunneling over SSH with PuTTY

See Connecting to the MySQL database remotely (via an SSH Tunnel)

  • run putty.exe
  • Connection -> SSH -> Tunnels
    • Port forwarding: source port to 3306
    • destination: 127.0.0.1:3306
    • check Local
    • click Add

Enabling verbose SSH logging

To see what's going on with your ssh connections,

$ ssh -v user@host

Or

$ ssh -vv user@host

Standard IO

cat EOF

$ cat > output <<EOF
> text
> EOF
$ cat output
text

Screen

Creating a new screen or reconnecting to a detached screen

$ screen -R

Detaching a screen

$ screen -D

Reconnecting to screen

$ screen -D
$ screen -R

I have a script in ~/bin/reconnect like so,

#!/bin/bash
screen -D
screen -R

This will detach your last screen, and reconnect it on the current terminal.

Write

Talking to other users on the system

write is a unix command for talking to other users on the system. To use write:

1. SSH to <username>@<hostname> and login with your username and password.

2. Issue the following command to find out who is logged onto the system:

$ who

3. Issue the following command to talk to a specific user:

$ write <username>

4. Enter the message you'd like to send the user, followed by Ctrl+C to send. Press Ctrl+D to cancel.

Date

Reporting the time on the server

$ date

Reporting UTC time

$ date --utc

Getting the date in yyyy-MM-dd-hhmmss format

$ date="`date +%F-%H%M%S`"

Getting the year in four digits

$ year="`date +%Y`"

Getting the month in two digits

$ month="`date +%m`"

Getting the day of the month in two digits

$ day="`date +%d`"

MySQL

Dumping a MySQL database

You can dump the database into a file using:

$ mysqldump -h hostname -u user --password=password databasename > filename

Loading a MySQL database from a dump file

You can create a database using:

$ echo create database databasename | mysql -h hostname -u user -p

You can restore a database using:

$ mysql -h hostname -u user --password=password databasename < filename

Creating a MySQL user

# mysql -h localhost -u root --password=<password>
mysql> create user 'username'@'localhost' identified by '<password>';

Granting all MySQL user permissions

# mysql -h localhost -u root --password=<password>
mysql> grant all privileges on dbname.* to user@host;

Apache

Maintaining .htaccess passwords

To add or modify the password for a user:

$ htpasswd /etc/apache2/passwd username

Configuring PHP session timeout in .htaccess

For a session timeout of 9 hours:

php_value session.cookie_lifetime 32400
php_value session.gc_maxlifetime 32400

Disabling PHP magic quotes in .htaccess

php_flag magic_quotes_gpc Off

Requiring HTTP Auth in .htaccess

AuthType Basic
AuthName "Speak Friend And Enter"
AuthUserFile /home/jj5/.htpasswd
Require valid-user

Restarting Apache

The hard way

$ sudo /etc/init.d/apache2 restart

The graceful way (avoids dropping active connections)

$ sudo apache2ctl graceful

PHP

Including a file relative to the including file

require_once( dirname( __FILE__ ) . '/relative/path/to.php' );

Enabling error reporting

 error_reporting( E_ALL | E_STRICT );
 ini_set( 'display_errors', 'On' );

Setting an error handler

set_error_handler( "error_handler", E_ALL | E_STRICT );
function error_handler( $error_code, $error_message, $error_file, $error_line, $error_context ) {
  // ...
}

BASH scripting

For a primer on bash scripting see TFM: Erotic Fantasy: /bin/sh Programming.

Telling a script to run in bash

The first line of the file should be:

#!/bin/bash

Checking if a command-line argument was not passed in

if [ "$1" = "" ]; then
  echo "Missing parameter 1.";
  exit 1;
fi

Checking command exit status

cd /my/path
if [ "$?" -ne "0" ]; then
  echo "Cannot change dir.";
  exit 1;
fi

Checking if a directory doesn't exist

if [ ! -d "/my/dir" ]; then
  mkdir /my/dir
fi

Deleting old backups

To keep only the latest five backups:

rm `ls -t | awk 'NR>5'` 2> /dev/null

Changing into the script's directory

cd `dirname $0`